Click here to load reader

Extending the Unity Editor Extended

  • View
    22.233

  • Download
    6

Embed Size (px)

Text of Extending the Unity Editor Extended

  • 1. Extending the Unity EditorUnity Editor Ex

2. Extending the Unity Editor EXTENDED! Transform ScriptableWizardEditorWindow 2/23 3. Extending the Unity Editor EXTENDED! GUI(Layout)EditorGUI(Layout) 3/23 4. Extending the Unity Editor EXTENDED! (1/3)Editorusing UnityEngine; // using UnityEditor; // [CustomEditor(typeof(Hoge))] // Hogepublic sealed class HogeEditor : Editor{ // public override void OnInspectorGUI() { }}[CustomEditor(typeof(Transform))] // public sealed class CustomTransformEditor : Editor{ public override void OnInspectorGUI() {4/23 5. Extending the Unity Editor EXTENDED! (2/3)OnInspectorGUI()DrawDefaultInspector() public override void OnInspectorGUI() {DrawDefaultInspector(); // }serializedObject public override void OnInspectorGUI() {// foreach (SerializedProperty p in serializedObject.GetIterator()){ EditorGUILayout.PropertyField(p);} } 5/23 6. Extending the Unity Editor EXTENDED! (3/3)GUILayoutEditorGUILayout 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);} } 6/23 7. Extending the Unity Editor EXTENDED! Window APARTURE Cutscene Editor7/23 8. Extending the Unity Editor EXTENDED! (1/4)ScriptableWizardUnityusing UnityEngine; // using UnityEditor; // public sealed class MaterialReplacer : ScriptableWizard // ScriptableWizard{ public Material replaceTo; // public[MenuItem("Tools/Replace Material...")] // static void OpenWindow(){ ScriptableWizard.DisplayWizard("Replace Material", "Apply");}void OnWizardCreate(){// }}8/23 9. Extending the Unity Editor EXTENDED! (2/4)OnWizardCreate()using System.Linq; // Linq... void OnWizardCreate() // 1Create {if (replaceTo == null) // return;var renderers = from r in Selection.gameObjectswhere r.renderer != nullselect r.renderer;foreach (Renderer r in renderers) r.sharedMaterial = replaceTo; }Apply9/23 10. Extending the Unity Editor EXTENDED! (3/4)EditorWindowusing UnityEngine; // using UnityEditor; // public sealed class MaterialReplacer : EditorWindow // EditorWindow{ [MenuItem("Tool/Replace Material...")] // static void OpenWindow() {EditorWindow.GetWindow(true, "Replace Material"); }void OnGUI(){// OnGUI()}}10/23 11. Extending the Unity Editor EXTENDED! (4/4)OnGUI()using System.Linq; // Linq... static Material targetMaterial = null; void OnGUI() // ScriptableWizar {targetMaterial = EditorGUILayout.ObjectField( "Replace To", targetMaterial, typeof(Material), false) as Material;// GUI.enabled = (targetMaterial != null) && (Selection.gameObjects.Length > 0);if (GUILayout.Button("Apply")){ var renderers = from r in Selection.gameObjects where r.renderer != null select r.renderer; foreach (Renderer r in renderers)r.sharedMaterial = targetMaterial; Close();}GUI.enabled = true; }11/23 12. Extending the Unity Editor EXTENDED! UnityOnPostprocessAllAssetsstatic12/23 13. Extending the Unity Editor EXTENDED! AssetPostprocessorOnPostprocessModel()using UnityEngine; // using UnityEditor; // public sealed class CustomModelImporter : AssetPostprocessor{ public override int GetPostprocessOrder() {return 0; // }void OnPreprocessModel(){ // if (assetPath.Contains("@"))(assetImporter as ModelImporter).importMaterials = false;}}13/23 14. Extending the Unity Editor EXTENDED! GizmosHandlesEditor.OnPreviewGUI() 14/23 15. Extending the Unity Editor EXTENDED! (1/3)EditorOnSceneGUISceneViewUndocumentedonSceneGUIDelegateOnSceneGUI void OnSceneGUI() {Vector3 pos = (target as Hoge).transform.position;float size = HandleUtility.GetHandleSize(pos); // Handles.DrawSolidDisc(pos, Vector3.up, size); } static HogeEditor() // 1static {SceneView.onSceneGUIDelegate += view => { Handles.DrawLine(Vector3.zero, Vector3.up);}; } 15/23 16. Extending the Unity Editor EXTENDED! (2/3)hideFlagsHideFlags.HideAndDontSaveExecuteInEditMode// [ExecuteInEditMode()]public sealed class GameViewUpdater : MonoBehaviour{ void OnGUI() {// }}... // OnEnable void OnEnable() {GameObject go = new GameObject();go.hideFlags = HideFlags.HideAndDontSave;go.AddComponent(); }16/23 17. Extending the Unity Editor EXTENDED! (3/3)HasPreviewGUItrueOnPreviewGUI public override bool HasPreviewGUI () {return true; } public override void OnPreviewGUI(Rect r, GUIStyle bg) {GUILayout.Label("Label");GUILayout.Button("Button"); }17/23 18. Extending the Unity Editor EXTENDED! Window 18/23 19. Extending the Unity Editor EXTENDED! (1/3)MenuItem/static [MenuItem("Tools/MyMenu")] // MenuItem static void MyMenuItem() // { }19/23 20. Extending the Unity Editor EXTENDED! (2/3)MenuItemContextMenuMenuItemstaticContextMenu[MenuItem("CONTEXT/Hoge/MyMenu")] // Hogestatic void MyMenu(){}...public class Hoge : MonoBehaviour // { [ContexMenu("MyMenu")] void MyMenu() { }}20/23 21. Extending the Unity Editor EXTENDED! (3/3) // void Callback(Object obj) {(obj as Hoge).transform.position = Vector3.zero } // public override void OnPreviewGUI(Rect r, GUIStyle background) {// var evt = Event.current;if (evt.type == EventType.ContextClick && r.Contains(evt.mousePosition)){ var menu = new GenericMenu(); menu.AddItem(new GUIContent("Reset Position"), false, Callback, target); menu.ShowAsContext(); evt.Use(); // }DrawDefaultInspector(); }21/23 22. Extending the Unity Editor EXTENDED! 22/23 23. Extending the Unity Editor EXTENDED! 23/23

Search related