Unity3D 场景编辑器扩展学习笔记-EditorWindow
今天可以說是有點成果了,可喜可賀~先上圖
本節(jié)內(nèi)容涉及到的類: EditorWindow, Physics, Handles, Camera, SceneView.
Editor類雖然可以使用OnSceneGUI來進行Scene視圖里的各種定制操作,但是只能針對特定類型的腳本進行。如果想實現(xiàn)種怪這種編輯器常見需求,實現(xiàn)起來超級別扭。我嘗試著基于Editor類來實現(xiàn)這個操作,發(fā)現(xiàn)除了在Inspector里鎖住當前組件的視圖外,沒什么好方法了:創(chuàng)建一個空對象,放一個特定的類,實現(xiàn)一個特定的Editor,再每次編輯場景數(shù)據(jù)時手動鎖定Inspector....想想都吐血了。
因此不可避免的尋求其他解決方案:EditorWindow
EditorWindowNamespace: UnityEditor
Parent class:?ScriptableObject
Derive from this class to create an editor window.
Create your own custom editor window that can float free or be docked as a tab, just like the native windows in the Unity interface.關(guān)于EditorWindow的使用,U3D還是有一些基本例子的,今天我要談的是關(guān)于SceneView的操作。
SceneView.onSceneGUIDelegate 該函數(shù)為定制提供了可能性,上代碼先。
using UnityEngine; using UnityEditor; using System.Collections; using System.Text;public class SceneEditorWindow : EditorWindow {RaycastHit _hitInfo;SceneView.OnSceneFunc _delegate;static SceneEditorWindow _windowInstance;[MenuItem("Window/Scene Editor #`")]static void Init(){if(_windowInstance == null){_windowInstance = EditorWindow.GetWindow(typeof(SceneEditorWindow)) as SceneEditorWindow;_windowInstance._delegate = new SceneView.OnSceneFunc(OnSceneFunc);SceneView.onSceneGUIDelegate += _windowInstance._delegate;}}void OnEnable(){}void OnDisable(){}void OnDestroy(){if (_delegate != null){SceneView.onSceneGUIDelegate -= _delegate;}}void OnGUI(){}void OnInspectorGUI(){Debug.Log("OnInspectorGUI");}static public void OnSceneFunc(SceneView sceneView){_windowInstance.CustomSceneGUI(sceneView);}void CustomSceneGUI(SceneView sceneView){Camera cameara = sceneView.camera;Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);if (Physics.Raycast(ray, out _hitInfo, 10000, -1)){//Debug.DrawRay(ray.origin, ray.direction, Color.yellow);Vector3 origin = _hitInfo.point;origin.y += 100;if (Physics.Raycast(origin, Vector3.down, out _hitInfo)){Handles.color = Color.yellow;Handles.DrawLine(_hitInfo.point, origin);float arrowSize = 1;Vector3 pos = _hitInfo.point;Quaternion quat;Handles.color = Color.green;quat = Quaternion.LookRotation(Vector3.up, Vector3.up);Handles.ArrowCap(0, pos, quat, arrowSize);Handles.color = Color.red;quat = Quaternion.LookRotation(Vector3.right, Vector3.up);Handles.ArrowCap(0, pos, quat, arrowSize);Handles.color = Color.blue;quat = Quaternion.LookRotation(Vector3.forward, Vector3.up);Handles.ArrowCap(0, pos, quat, arrowSize);//Handles.DrawLine(pos + new Vector3(0, 3, 0), pos);}}SceneView.RepaintAll();} }發(fā)現(xiàn)沒啥可講的了。。因為有之前GUILayout和Handles的知識后,其實只需要知道一個流程性的代碼就行了。
而且EditorWindow的基本使用,U3D文檔里已經(jīng)有很充分的例子。
有一點需要注意,SceneView.RepaintAll(); 根據(jù)觀察,SceneView在沒有任何操作時,FPS估計是1,鼠標移動不會觸發(fā)場景重繪,但是?onSceneGUIDelegate 確是一直在調(diào)用,所以加上這個之后,操作的反饋就變成實時的了,這會帶來效率損耗---如果不影響操作的話,不用管。
總結(jié)
以上是生活随笔為你收集整理的Unity3D 场景编辑器扩展学习笔记-EditorWindow的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3之Transition
- 下一篇: android t类型参数,androi