unity編輯器 ---SceneView視窗自定義
阿新 • • 發佈:2018-11-20
SceneView視窗自定義 是通過Handles類 控制柄來自定義介面,通過HandleUtility 控制柄工具輔助。
Handles控制柄到底是什麼?
直接上圖就明白了:
如上圖箭頭所指的,我們都可以通過Handles控制柄來畫出來。所謂的Handles控制柄就是我們選擇的物體後在場景裡的控制點。
直接上程式碼小測試
首先加一個空指令碼目的是掛在的物體上,然後自定義指令碼視窗
using System.Collections.Generic; using UnityEngine; public class HandleTest : MonoBehaviour { }
然後將此指令碼掛在到任意物體上
然後定義此指令碼視窗
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; [CustomEditor(typeof(HandleTest))] public class HandleTestEditor : Editor { GameObject gameObject; Transform _transformPoint; Transform transformPoint { get { if (!_transformPoint) { _transformPoint = new GameObject("transformPoint").transform; } return _transformPoint; } } float cylinderSize = 1; Camera camera; Camera _cameraTemp; Camera cameraTemp { get { if (!_cameraTemp) { _cameraTemp = new GameObject("cameraTemp").AddComponent<Camera>(); } return _cameraTemp; } } private void OnEnable() { gameObject = ((HandleTest)target).gameObject; camera = Camera.main; } private void OnSceneGUI() { drawCylinder(); drawShere(); lookCamera(); sceneComponent(); } Vector3 point; private void lookCamera() { point = Handles.PositionHandle(point, Quaternion.identity); transformPoint.position = point; gameObject.transform.LookAt(transformPoint); } /// <summary> /// 畫圓柱 /// </summary> private void drawCylinder() { Handles.color = Color.red; Handles.CylinderHandleCap(0, gameObject.transform.position + new Vector3(4, 0, 0), gameObject.transform.rotation, cylinderSize, EventType.Repaint); } private void sceneComponent() { Handles.BeginGUI(); GUILayout.BeginArea(new Rect(100,100,500,500)); GUILayout.Button("按鈕"); GUILayout.EndArea(); Handles.EndGUI(); } /// <summary> /// 畫圓 /// </summary> private void drawShere() { Handles.color = Color.green; Handles.SphereHandleCap(0, gameObject.transform.position +new Vector3(0, 5, 0), gameObject.transform.rotation, cylinderSize, EventType.Repaint); } public override void OnInspectorGUI() { base.OnInspectorGUI(); cylinderSize = EditorGUILayout.FloatField(cylinderSize); } }
效果如下:
官網對此類已經做了詳細說明
我只附上一些效果做一下筆記
Handles.PositionHandle 位置控制柄
Handles.RotationHandle 旋轉控制柄
Handles.ScaleHandle 縮放控制柄
Handles.RadiusHandle 半徑控制柄
還有其它繪製圖形,檢視官網就可以了