Unity3d基础学习第13天

目录

    • 绘图工具
      • 实例1
      • 实例2
      • 实例3 射线
    • 屏幕射线
      • 实例1

绘图工具

    private Color beginColor; //系统初始颜色,方便用完返回

实例1

    void OnDrawGizmos()//绘图工具,挂在即可执行,不写逻辑代码,只写图案绘制
	{
		beginColor = Gizmos.color; //保存初始颜色
		//在原点,长度为1的蓝色的立方体
		Gizmos.color = Color.blue;
		Gizmos.DrawCube (Vector3.zero,Vector3.one);

		Gizmos.color = Color.red;
		Gizmos.DrawWireCube(Vector3.forward,Vector3.one);

		Gizmos.color = Color.green;
		Gizmos.DrawSphere (Vector3.back, 0.5f);

		Gizmos.color = Color.black;
		Gizmos.DrawWireSphere(Vector3.left, 0.5f);

		Gizmos.color = Color.white;
		Gizmos.DrawLine(new Vector3(1,1,1),new Vector3(1,1,2));
	
	}

实例2

   void OnDrawGizmosSelected()//只有选中挂在有脚本的游戏对象才会被执行
	{
		Debug.Log ("selected");
	}


注:
vector3.forward 自身坐标的前方
transform.forward 世界坐标系下的前方

实例3 射线

    ray = new Ray (transform.position, transform.forward);// 发射一条射线,起点和终点
	if (Physics.Raycast(ray,out hit ,10))
	{
			Debug.Log (hit.transform.name);
	}

屏幕射线

   if (Input.GetMouseButtonDown(0)) 
   {
			ray = Camera.main.ScreenPointToRay (Input.mousePosition); //画出射线,从摄像机发出,进过屏幕上鼠标点击的点
			if (Physics.Raycast(ray,out hit )) 
			{
				//if (hit.transform.name = "terrion") 
				//{
				//调用角色移动脚本
				//}
				Debug.Log (hit.transform.name);

			}
	}
    //予以辅助,看到射线
    void OnDrawGizmos()
	{
		Gizmos.color = Color.blue;
		Gizmos.DrawRay (ray.origin,ray.direction *1000);
	}

实例1

    public class SectorGizmos : MonoBehaviour
    {

	private Vector3  v1= new Vector3 (1,0,0);
	private Vector3  v2 = new Vector3 (2,2,2);

	private Color begincolor;
	[Range(0,360)]
	public int angle;
	private int radius = 10;
	private Vector3 v = Vector3.zero; //起始边

    //void Start () 
    //	{
    //		Vector3 v3 = v1 + v2;  //三维向量 +  三维向量 = 三维向量
    //		Debug.Log (v3);
    //Vector3 v4 = Quaternion.Euler (new Vector3(0,90,0)) * v1; //四元数 * 三维向量 = 三维向量
    //将三维向量进行旋转操作,四元素那一个轴有值,就在这个方向上旋转多少度
    //}
	void OnDrawGizmos()
	{
		begincolor = Handles.color;
		Handles.color = Color.red;
		int beginangle = angle / 2;
		v = Quaternion.Euler (new Vector3 (0, -beginangle, 0)) * transform.forward; //起始边

		Handles.DrawSolidArc (transform.position,Vector3.up,v,angle,radius);

	}
   }

你可能感兴趣的:(Unity3d)