PS:适合对UnityEditor类有一些了解的童鞋阅读,我没有完全研究透这套东西,暂时先学到哪写到哪吧。
*所有的Editor相关代码,必须存在于Assets目录下的任意路径的名为Editor文件夹内。U3D会自动为这些文件生成编辑器工程。
重要的类
Editor,EditorWindow
GUILayout,EditorGUILayout
GUIUtility,EditorGUIUtility
Handles,Event
架构:Editor,EditorWindow
Editor类和EditorWindow类都继承自同一个基类:ScriptableObject,因此他们都可以针对某种脚本类来进行操作。
Editor类只能定制针对脚本的扩展,从脚本内容在Inspector里的显示布局,到变量在Scene视图的可视化编辑
EditorWindow主要是扩展编辑器的功能,不必针对某种脚本(虽然可以做到),而且它有独立的窗口,使用OnGUI函数来绘制2D的UI。
2D显示:GUILayout,EditorGUILayout
GUILayout类用来绘制各种2D控件,比如按钮,文本,可编辑文本,可折叠的列表,以及对它们的分组和排版。这些控件既可以绘制在Editor类里,也可以绘制在EditorWindow里。
EditorGUILayout类和GUILayout类似,但是提供更多的预定义控件,而且只能在Editor下才可以使用。
自定义2D控件:GUIUtility,EditorGUIUtility
GUIUtility类用来实现自定义的2D对象。
EditorGUIUtility类和GUIUtility类似,针对Editor下使用的GUI进行定制。
3D显示:Handles
Handles类提供各种场景视图里的显示,包括绘制线、面、扇形、方块、圆锥、3D空间的按钮(所谓按钮,其实就是个响应区域,你要是信U3D的就囧了),还有一些预定义的操作控件,比如三向箭头,旋转球,自由旋转等等,这些操作针对的不是物体,而是数值,因此除了可以控制脚本对象所在的GameObject,也可以控制某些成员变量,本质上都是在控制变量数值,因此场景扩展这个类肯定要大显身手。
输入:Event
Event类用来获取输入相关的信息,Event指的是InputEvent,包括鼠标位置,键盘按下的KEYCODE,各种命令键(Windows的Windows键和Mac的Command键),Shift,CapsLocks之类,还有U3DEditor里的CommandName,比如 Copy,SeletecAll,Save这种菜单命令。这个信息在OnGUI,OnInspector,OnSceneGUI里都可以使用。
Editor类
使用
using UnityEditor;
[Custom(typeof(TypeName))]
class YourEditorClass : Editor
该类可以自定义对脚本类在Inspector里的视图,重要的事件如下:
OnEnable
脚本所在对象被选中时触发,当编辑器开始自动重新编译时,对象会重新被选择一次而触发此消息。
OnDisable
脚本所在对象被取消选中时触发,当编辑器开始自动重新编译时,对象会重新被选择一次而触发此消息,脚本移除时也会触发此消息。
OnDestroy
脚本从对象上移除时触发
上述三个时机,适合初始化和反初始化,一些重要的对象都可以在这里获取并缓存出来,比如 target对象(正在处理的脚本实例)等。
注意:实际测试时,并不是Disable或Destroy之后,就没有代码在执行了。所以干活的代码在引用缓存下来的变量之前,先判断下是否为空,否则会报异常。这会影响脚本保存变量实例的值。不过这个问题很好查,只需要保证“脚本被移除时无异常,重新添加脚本时也无异常”这个测试用例就可以了。
OnInspectorGUI
定制脚本在Inspector里的显示方式,基本上配合GUILayout可以写出十分复杂的定制界面。有时候Inspector太小或不想和其他组件一起显示时,可以使用EditorWindow来做这个定制界面。
OnSceneGUI
定制脚本在Scene里的表现,比如可以将脚本内一个怪物AI的”警戒半径“变量,用很直观的圆盘来表达出来,也有很多别的表现和操作,这些表现和操作都是通过GUILayout和Handles类来完成的。
OnInspectorGUI范例代码:
using UnityEngine; using UnityEditor; using System.Collections; using System; class UIResource : MonoBehaviour { } [CustomEditor(typeof(UIResource))] class UIManifestCreator : Editor { private UIResource _script; private static bool _toggle_all = true; private GameObject _rootObject; public void OnEnable() { var script = (UIResource)(serializedObject.targetObject); if (script != null) { _script = script; _rootObject = script.gameObject; } else { //这是什么情况; Console.Error.WriteLine("tell uncle kun!"); } } public void OnDisable() { var script = (UIResource)(serializedObject.targetObject); if (script == null) { // 这种情况是脚本对象被移除了; } else { // 这种情况是编译脚本导致的重刷; } _script = null; _rootObject = null; } public void OnDestroy() { } public override void OnInspectorGUI() { GUILayout.BeginHorizontal(); { if (GUILayout.Button("Save")) { EditorApplication.SaveCurrentSceneIfUserWantsTo(); } if (GUILayout.Button(_toggle_all ? "Untoggle All" : "Toggle All")) { _toggle_all = !_toggle_all; } } GUILayout.EndHorizontal(); EditorGUILayout.LabelField("", GUILayout.Width(150)); Repaint(); } }
using UnityEngine; using UnityEditor; using System.Collections; using System; public class SceneTag : MonoBehaviour { public float shieldArea = 5; } [CustomEditor(typeof(SceneTag))] public class SceneEditor : Editor { void OnSceneGUI() { SceneTag scene = target as SceneTag; Handles.color = Color.blue; Handles.Label(scene.transform.position + Vector3.up * 2, scene.transform.position.ToString() + "\nShieldArea: " + scene.shieldArea.ToString()); Handles.color = Color.red; Handles.DrawWireArc(scene.transform.position, scene.transform.up, -scene.transform.right, 180, scene.shieldArea); scene.shieldArea = Handles.ScaleValueHandle(scene.shieldArea, scene.transform.position + scene.transform.forward * scene.shieldArea, scene.transform.rotation, 1, Handles.ConeCap, 1); //comment by kun 2014.2.18 // GUI相关的绘制需要在Handles的绘制之后,否则会被覆盖掉; // 使用Handles.BeginGUI会导致无法旋转摄像机,原因不详; GUILayout.BeginArea(new Rect(Screen.width - 100, Screen.height - 80, 90, 50)); //Handles.BeginGUI(new Rect(Screen.width - 100, Screen.height - 80, 90, 50)); try { float a = float.Parse(GUILayout.TextField(scene.shieldArea.ToString())); scene.shieldArea = a; } catch (System.Exception ex) { } if (GUILayout.Button("Reset Area")) scene.shieldArea = 5; //Handles.EndGUI(); GUILayout.EndArea(); } }