扩展Scene视图的几种方式
1) 注册SceneView.duringSceneGui委托(Unity2018及之前版本是SceneView.onSceneGUIDelegate)
#if UNITY_EDITOR using UnityEditor; public class TestSceneGUIWindow : EditorWindow { [MenuItem("MyTools/TestSceneGUIWindow")] public static void ShowWindow() { EditorWindow.GetWindow<TestSceneGUIWindow>(false, "TestSceneGUIWindow", true).Show(); } private void OnEnable() { SceneView.onSceneGUIDelegate -= OnMySceneGUI; SceneView.onSceneGUIDelegate += OnMySceneGUI; } private void OnDisable() { SceneView.onSceneGUIDelegate -= OnMySceneGUI; } private void OnMySceneGUI(SceneView sceneView) { //在这边扩展Scene视图 } } #endif
2) 自定义Editor,然后实现事件函数OnSceneGUI
#if UNITY_EDITOR using UnityEditor; [CustomEditor(typeof(Xxx))] public class TestSceneGUIEditor : Editor { private void OnSceneGUI() { //在这边扩展Scene视图 } } #endif
显示gui控件
GUILayout.BeginArea(new Rect(40, 20, 200, 200), "test"); if (GUILayout.Button("Button")) { Debug.Log($"Button Click"); } EditorGUILayout.Space(); GUILayout.BeginHorizontal(); { EditorGUILayout.SelectableLabel("Num:", GUILayout.Width(35)); EditorGUILayout.IntField(0); } GUILayout.EndHorizontal(); EditorGUILayout.HelpBox("一些提示信息", MessageType.Warning); EditorGUILayout.IntSlider(0, 0, 10); GUILayout.EndArea();
显示几何形状
弧形
var arcRadius = 5; var arcAngle = 270; Handles.color = Color.red; var pos = new Vector3(3, 0, 0); var arcStartDir = new Vector3(0, 0, 1); Handles.DrawSolidArc(pos, Vector3.up, arcStartDir, arcAngle, arcRadius); //x-z平面绘制2d弧形, 90度开始, 顺时针绘制270度 Handles.color = Color.green; pos.y += 5; arcStartDir = new Vector3(1, 0, 0); Handles.DrawSolidArc(pos, Vector3.up, arcStartDir, arcAngle, arcRadius); //x-z平面绘制2d弧形, 0度开始, 顺时针绘制270度 Handles.color = Color.blue; pos.y += 5; arcStartDir = new Vector3(1, 0, 1); Handles.DrawSolidArc(pos, Vector3.up, arcStartDir, arcAngle, arcRadius); //x-z平面绘制2d弧形, 45度开始, 顺时针绘制270度 Handles.color = Color.cyan; pos.y += 5; arcStartDir = new Vector3(1, 1, 0); Handles.DrawSolidArc(pos, Vector3.up, arcStartDir, arcAngle, arcRadius); //3d弧形, 相当于绿色的那个弧形向上拉起一点(从顶往下看, 和它是重叠的)
直线,折线
Handles.color = Color.red; var pos = new Vector3(0, 0, 0); Handles.DrawLine(pos, new Vector3(10, 10, 10)); //绘制直线(红色) Handles.color = Color.blue; var points = new Vector3[] { Vector3.zero, new Vector3(10, 10, -10), new Vector3(10, -10, -10) }; Handles.DrawPolyLine(points); //绘制折线(蓝色) Handles.color = Color.green; points = new Vector3[] { Vector3.zero, new Vector3(10, -10, 10), new Vector3(-10, -10, 10) }; Handles.DrawAAPolyLine(5, points); //宽度为5的折线(绿色, 抗锯齿)
显示操控控件
m_Position = Handles.PositionHandle(m_Position, Quaternion.identity); var pos = new Vector3(6, 0, 0); Handles.color = Color.red; Handles.Label(pos, $"文本"); float handleSize = HandleUtility.GetHandleSize(pos); EditorGUI.BeginChangeCheck(); pos.x += 3; m_Scale = Handles.ScaleHandle(m_Scale, pos, Quaternion.identity, handleSize); pos.x += 10; m_Rot = Handles.RotationHandle(m_Rot, pos); //Handles.DoRotationHandle(m_Rot, pos); pos.x += 10; m_Rot = Handles.FreeRotateHandle(m_Rot, pos, handleSize); pos.x += 8; m_Radius = Handles.RadiusHandle(Quaternion.identity, pos, m_Radius); if (EditorGUI.EndChangeCheck()) { Debug.Log($"{m_Scale}, {m_Position}, {m_Rot}, {m_Radius}"); }
参考
UnityEditor学习笔记二 - Wunsam_Chan - 博客园 (cnblogs.com)
Unity3d Editor 编辑器扩展功能详解(5)Handles - 知乎 (zhihu.com)
标签:10,arcStartDir,Vector3,Handles,视图,Scene,编辑器,pos,new From: https://www.cnblogs.com/sailJs/p/17770207.html