Extending the Unity Editor

  • View
    44.293

  • Download
    0

Embed Size (px)

Text of Extending the Unity Editor

  • 1. Extending the Unity EditorUnity Editor

2. Extending the Unity Editor UnityGUI (1/2) GUISkin ViewportScreen PC/MacDynamic 2/30 3. Extending the Unity Editor UnityGUI (2/2) OnGUI()OnGUI() void OnGUI() {if (GUILayout.Button("Next Level")){ Application.LoadLevel("Level2");}if (GUILayout.Button("Quit")){ Application.Quit();} } OnInspectorGUI()OnGUI()3/30 4. Extending the Unity Editor GUIGUILayout (1/2) GUI Rect void OnGUI() {GUI.Button(new Rect(10, 10, 100, 50), "Button1");GUI.Button(new Rect(50, 50, 100, 50), "Button2");GUI.Button(new Rect(90, 90, 100, 50), "Button3");text = GUI.TextArea(new Rect(200, 10, 100, 100), text); }4/30 5. Extending the Unity Editor GUIGUILayout (2/2) GUILayout BeginHorizontal void OnGUI() {GUILayout.BeginHorizontal(); // GUILayout.BeginVertical();//GUILayout.Button("Button1");GUILayout.Button("Button2");GUILayout.Button("Button3"); GUILayout.EndVertical(); text = GUILayout.TextArea(text, GUILayout.Width(200), GUILayout.Height(60));GUILayout.EndHorizontal(); }5/30 6. Extending the Unity Editor EditorGUIEditorGUILayout (1/2) GUIGUILayoutEditorGUIEditorGUILayoutEditor GUIEditorGUIEditorGUIButtonGUI public override void OnInspectorGUI() {GUILayout.BeginHorizontal();position = EditorGUILayout.Vector3Field("Position", position);if (GUILayout.Button("Reset")) position = Vector3.zero;GUILayout.EndHorizontal(); } 6/30 7. Extending the Unity Editor EditorGUIEditorGUILayout (2/2) EditorGUILayoutGUIGUIEditorGUILayout FlexibleSpace()GUILayout.ExpandWidth()7/30 8. Extending the Unity Editor (1/5) TransformCamera using UnityEngine; // using UnityEditor; // [CustomEditor(typeof(Transform))] // Transform public sealed class CustomTransformEditor : Editor // Editor {public override void OnInspectorGUI() // { ... 8/30 9. Extending the Unity Editor (2/5) using UnityEngine; public class Hoge : MonoBehaviour {public Vector3 velocity; }VectorTransform 9/30 10. Extending the Unity Editor (3/5) Project(Assets)EditorMonoBehaviourEditorusing UnityEngine; // using UnityEditor; // [CustomEditor(typeof(Hoge))] // Hogepublic sealed class HogeEditor : Editor{ public override void OnInspectorGUI() {DrawDefaultInspector(); // }}10/30 11. Extending the Unity Editor (4/5) EditorGUILayoutVector3Field using UnityEngine; using UnityEditor; [CustomEditor(typeof(Hoge))] public sealed class HogeEditor : Editor {public override void OnInspectorGUI(){ // target Hoge hoge = target as Hoge; // targetObject Vector3 v = EditorGUILayout.Vector3Field("Velocity", hoge.velocity); if (v != hoge.velocity) // {Undo.RegisterUndo(hoge, "Velocity Change"); // hoge.velocity = v;EditorUtility.SetDirty(target); // }} }11/30 12. Extending the Unity Editor (5/5) Unity Undo.RegisterUndo() Ctrl+Z Ctrl+Y12/30 13. Extending the Unity Editor (1/2) static Vector3 copyBuffer = Vector3.zero; public override void OnInspectorGUI() {Hoge hoge = target as Hoge;Vector3 v = EditorGUILayout.Vector3Field("Velocity", hoge.velocity);EditorGUILayout.BeginHorizontal(); // if (GUILayout.Button("Reset", EditorStyles.miniButton)) // v = Vector3.zero;GUILayout.Space(GUILayoutUtility.GetAspectRect(1).width / 3);if (GUILayout.Button("Copy", EditorStyles.miniButtonLeft)) copyBuffer = v;if (GUILayout.Button("Paste", EditorStyles.miniButtonRight)) v = copyBuffer;EditorGUILayout.EndHorizontal();if (v != hoge.velocity){ Undo.RegisterUndo(hoge, "Velocity Change"); hoge.velocity = v; EditorUtility.SetDirty(target);} } 13/30 14. Extending the Unity Editor (2/2) Unity EditorStyles 14/30 15. Extending the Unity Editor EditorGUILayout.MinMaxSlider() EditorGUI.ProgressBar() 15/30 16. Extending the Unity Editor (1/2) newUnitynewstaticOnEnable()16/30 17. Extending the Unity Editor (2/2) OnInspectorGUI()OnGUI()OnInspectorGUI()(Repaint()) OnSceneGUI()EditorOnSceneGUI() 17/30 18. Extending the Unity Editor (1/3) () 18/30 19. Extending the Unity Editor (2/3) () [CustomEditor(typeof(Hoge))] public sealed class HogeEditor : Editor {Hoge hoge = null;void OnEnable(){ hoge = target as Hoge;}void OnSceneGUI(){ Vector3 root = hoge.transform.position; // Vector3 cap = root + hoge.velocity; // Handles.color = Color.magenta;// Handles.DrawLine(root, cap);// Quaternion rot = Quaternion.LookRotation(hoge.velocity); // float size = HandleUtility.GetHandleSize(cap); // Handles.ArrowCap(0, cap, rot, size);// }static Vector3 copyBuffer = Vector3.zero;public override void OnInspectorGUI()19/30 20. Extending the Unity Editor (3/3) () 20/30 21. Extending the Unity Editor 21/30 22. Extending the Unity Editor (1/5) Build SettingAnimationDock using UnityEngine; // using UnityEditor; // public sealed class MaterialReplacer : EditorWindow // EditorWindow {[MenuItem("Tool/Replace Material...")] // static void OpenWindow() // { 22/30 23. Extending the Unity Editor (2/5) Floating public sealed class MaterialReplacer : EditorWindow {[MenuItem("Tool/Replace Material...")]static void OpenWindow() // static{ EditorWindow.GetWindow(true, "Replace Material");} } 23/30 24. Extending the Unity Editor (3/5) Dockable public sealed class MaterialReplacer : EditorWindow {[MenuItem("Tool/Replace Material...")]static void OpenWindow() // static{ EditorWindow.GetWindow(false, "Replace Material");} }Dock24/30 25. Extending the Unity Editor (4/5) OnGUI()OnGUI() [MenuItem("Tool/Replace Material...")] static void OpenWindow() {EditorWindow.GetWindow(false, "Replace Material"); } static Material targetMaterial = null; void OnGUI() {targetMaterial = EditorGUILayout.ObjectField( "Material", targetMaterial, typeof(Material), false) as Material;GUI.enabled = (targetMaterial != null) && (Selection.gameObjects.Length > 0);if (GUILayout.Button("Replace!")){ foreach (var renderer in from go in Selection.gameObjects select go.renderer)if (renderer != null) renderer.sharedMaterial = targetMaterial;}GUI.enabled = true; } 25/30 26. Extending the Unity Editor (5/5) Replace 26/30 27. Extending the Unity Editor (1/4) SelectionSelection27/30 28. Extending the Unity Editor (2/4) UndoUndoUndo.RegisterUndo28/30 29. Extending the Unity Editor (3/4) EditorUtilityFileUtilEditorUtilityYes/NoFileUtil PlayerSettingsEditorUserBuildSettings29/30 30. Extending the Unity Editor (4/4) System.IO.StreamWritergmcs.rspSystem.Diagnostics.ProcessUnityWWW30/30