Unity编辑器Scene窗口快捷操作
1.按住crtl,可以一个一个单位移动、缩放、旋转物体,单位距离在Edit-Snapsetting中设置,设置单位大小
2.选中物体,按住alt + 鼠标左键,可以环视目标物体
3.按住V键,可以将物体的顶点接到其他物体的顶点
如果要设置更改其他在Scene窗口中的操作,可以利用MonoBehaviour下的OnDrawGizmos或OnDrawGizmosSelectsd方法,比如:
//自定义操作对象,并且设置执行的操作
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; //声明要处理的组件类型 [CustomEditor(typeof(BoxCollider))] public class CustomCubeEditor : Editor { BoxCollider m_cube; Transform m_trans; void OnEnable() { //包含该组件的物体被选中的时候调用 m_cube = (BoxCollider)target; m_trans = m_cube.transform; } void OnSceneGUI() { //坐标位置取整 Vector3 posTemp = m_trans.localPosition; float x = Mathf.RoundToInt(posTemp.x); float y = Mathf.RoundToInt(posTemp.y); float z = Mathf.RoundToInt(posTemp.z); m_trans.localPosition = new Vector3(x, y, z); //显示坐标 Handles.Label(m_trans.position + Vector3.up * 3, m_cube.name + ":" + m_trans.position.ToString()); Handles.BeginGUI(); //规定GUI显示区域 GUILayout.BeginArea(new Rect(100, 100, 100, 100)); //GUl绘制按钮 if (GUILayout.Button("上移")) { m_trans.position += Vector3.up; } if (GUILayout.Button("下移")) { m_trans.position += Vector3.down; } if (GUILayout.Button("左移")) { m_trans.position += Vector3.left; } if (GUILayout.Button("右移")) { m_trans.position += Vector3.right; } GUILayout.EndArea(); Handles.EndGUI(); } }
//这是Mono中的方法,需要挂载使用,在Scene中每帧调用和被选中的时候每帧调用
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CustomCube : MonoBehaviour { void OnDrawGizmos() { //每帧调用 //绘制一个cube边框 Gizmos.color = Color.black; Gizmos.DrawWireCube(GetComponent<Renderer>().bounds.center, GetComponent<Renderer>().bounds.size); //绘制一条直线,指向物体正上方 Gizmos.color = Color.yellow; Gizmos.DrawLine(transform.position, transform.position + transform.up * 2); } void OnDrawGizmosSelected() { //被选中的时候每帧调用 Gizmos.color = Color.red; Gizmos.DrawWireCube(GetComponent<Renderer>().bounds.center, GetComponent<Renderer>().bounds.size); } }