UnityEditor 用Gizmos画出类似NGUI,UIPanel的矩形框
来源:互联网 发布:萨迪克哈佐维克数据 编辑:程序博客网 时间:2024/05/16 10:53
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CamDrawGizmo : Editor {
[DrawGizmo(GizmoType.Selected)]
static void DrawRect(Transform transform, GizmoType gizmoType)
{
float z = transform.position.z - Camera.main.transform.localPosition.z;
//摄像机的位置
Vector3 camPos = Camera.main.transform.position;
//transform的位置
Vector3 transformPos = transform.position;
float height = z*2.0f*Mathf.Tan(Camera.main.GetComponent<Camera>().fieldOfView/2.0f*Mathf.Deg2Rad);
float width = height * Camera.main.GetComponent<Camera>().aspect;
Vector3[] corners = new Vector3[4];
if (transform.parent != null) {
//确保GameObject移动的时候,矩形框不跟着移动,所以矩形框的中心点和Camer是对齐的,
//我现在要做的是,在矩形框的4个角上,标注出位置,这个位置指:只要在GameObject的Position中输入这个位置,
//那么GameObject就会移出画面(对于不同的Z)
//所以这个时候这4个角上的位置,要转化为父节点下的相对坐标点
//transform.parent.worldToLocalMatrix(point) == transform.parent.InverseTransformPoint(point)
//transform.parent.localToWorldMatrix.MultiplyPoint(point) == transform.parent.TransformPoint(point);
corners[0] = (transform.parent.worldToLocalMatrix.MultiplyPoint (new Vector3 (camPos.x - width / 2, camPos.y - height / 2, transformPos.z)));
corners[1] = (transform.parent.worldToLocalMatrix.MultiplyPoint (new Vector3 (camPos.x - width / 2, camPos.y + height / 2, transformPos.z)));
corners[2] = (transform.parent.worldToLocalMatrix.MultiplyPoint (new Vector3 (camPos.x + width / 2, camPos.y + height / 2, transformPos.z)));
corners[3] = (transform.parent.worldToLocalMatrix.MultiplyPoint (new Vector3 (camPos.x + width / 2, camPos.y - height / 2, transformPos.z)));
}
else{
corners[0] = new Vector3 (camPos.x - width / 2, camPos.y - height / 2, transformPos.z);
corners[1] = new Vector3 (camPos.x - width / 2, camPos.y + height / 2, transformPos.z);
corners[2] = new Vector3 (camPos.x + width / 2, camPos.y + height / 2, transformPos.z);
corners[3] = new Vector3 (camPos.x + width / 2, camPos.y - height / 2, transformPos.z);
}
//
for (int i = 0; i<4; i++) {
corners[i].z = transformPos.z;
}
Vector3[] lablePos = new Vector3[4];
lablePos[0] = new Vector3 (camPos.x - width / 2, camPos.y - height / 2, transformPos.z);
lablePos[1] = new Vector3 (camPos.x - width / 2, camPos.y + height / 2, transformPos.z);
lablePos[2] = new Vector3 (camPos.x + width / 2, camPos.y + height / 2, transformPos.z);
lablePos[3] = new Vector3 (camPos.x + width / 2, camPos.y - height / 2, transformPos.z);
for (int i = 0; i<4; i++) {
//4个角上的确切位置是相对于世界坐标系的,但是标注的string是表示的相对于父节点的坐标
Handles.Label(lablePos[i], corners[i].ToString ());
}
Gizmos.color = Color.green;
//rect的位置
Vector3 rectPos = camPos;
rectPos.z = transformPos.z;
Gizmos.DrawWireCube(rectPos, new Vector3(width, height, 0));
}
}
- UnityEditor 用Gizmos画出类似NGUI,UIPanel的矩形框
- NGUI UIPanel的Depth属性
- NGUI -- UIPanel
- NGUI:UIPanel
- NGUI -- UIPanel
- 【解决方案】NGUI - UIPanel的Clipping功能无法裁剪子UIPanel
- NGUI组件UIPanel
- 【Unity3D】【NGUI】UIPanel
- NGUI所见即所得之UIPanel
- NGUI之UIPanel
- NGUI: UIPanel控件
- NGUI所见即所得之UIPanel
- NGUI所见即所得之UIPanel
- 把检测出的人脸用椭圆和矩形框画出
- NGUI UIPanel在softClip下不起作用的问题
- NGUI核心组件之UIPanel
- NGUI核心组件之UIPanel
- NGUI控件说明(中文) UIPanel
- 集合划分问题(一)
- 三栏布局(两边固定,中间自适应)
- ESX4.1虚拟机识别U盘
- Mark!2015年11月16日,学习Unity 3D。
- Java基于smtp与pop3实现收发邮件的功能
- UnityEditor 用Gizmos画出类似NGUI,UIPanel的矩形框
- Unity3D将来时:IL2CPP——C#的变迁
- 程序员都会的 35 个 jQuery 小技巧
- 存储过程和函数
- 1037 Magic Coupon
- SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property.....
- ORACLE恢复删除表或表记录
- 在.NET开发中的单元测试工具之(2)——xUnit.Net
- MySQL添加用户、删除用户与授权