Skip to content

Instantly share code, notes, and snippets.

@LotteMakesStuff
Last active March 4, 2025 17:13
Show Gist options
  • Select an option

  • Save LotteMakesStuff/cb63e4e25e5dfdda19a95380e9c03436 to your computer and use it in GitHub Desktop.

Select an option

Save LotteMakesStuff/cb63e4e25e5dfdda19a95380e9c03436 to your computer and use it in GitHub Desktop.

Revisions

  1. @GenericEvilBusiness GenericEvilBusiness revised this gist Jan 22, 2017. 1 changed file with 0 additions and 0 deletions.
    Binary file added example.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  2. LotteMakesStuff revised this gist Jan 22, 2017. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions CustomInspectorCreator.cs
    Original file line number Diff line number Diff line change
    @@ -18,14 +18,17 @@ static void CreateInsptorEditorClass()
    [MenuItem("Assets/Create/Custom Inspector", priority = 81, validate = true)]
    static bool ValidateCreateInsptorEditorClass()
    {
    Object obj = Selection.objects[0];
    string path = AssetDatabase.GetAssetPath(obj);

    if (!path.EndsWith(".cs"))
    return false;
    if (path.Contains("Editor"))
    return false;

    foreach (var script in Selection.objects)
    {
    string path = AssetDatabase.GetAssetPath(script);

    if (script.GetType() != typeof(MonoScript))
    return false;
    if (!path.EndsWith(".cs"))
    return false;
    if (path.Contains("Editor"))
    return false;
    }

    return true;
    }
  3. LotteMakesStuff revised this gist Jan 22, 2017. 1 changed file with 81 additions and 13 deletions.
    94 changes: 81 additions & 13 deletions CustomInspectorCreator.cs
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,17 @@
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Collections;

    public static class CustomInspectorCreator
    {
    [MenuItem("Assets/Create/Custom Inspector", priority = 81)]
    static void CreateInsptorEditorClass()
    {
    Object obj = Selection.objects[0];
    string assetPath = AssetDatabase.GetAssetPath(obj);

    var filename = Path.GetFileNameWithoutExtension(assetPath);
    var script = string.Format(template, filename);
    var editorFolder = Path.GetDirectoryName(assetPath) + "/Editor";

    if (!Directory.Exists(editorFolder))
    foreach (var script in Selection.objects)
    {
    Directory.CreateDirectory(editorFolder);
    BuildEditorFile(script);
    }

    File.WriteAllText(editorFolder + "/" + filename + "Inspector.cs", script);

    AssetDatabase.Refresh();
    }

    @@ -39,7 +30,50 @@ static bool ValidateCreateInsptorEditorClass()
    return true;
    }

    static string template = @"using UnityEngine;
    static void BuildEditorFile(Object obj)
    {
    MonoScript monoScript = obj as MonoScript;
    if (monoScript == null)
    {
    Debug.Log("ERROR: Cannot generate a custom inspector, Selected script was not a MonoBehavior");
    return;
    }

    string assetPath = AssetDatabase.GetAssetPath(obj);
    var filename = Path.GetFileNameWithoutExtension(assetPath);
    string script = "";
    string scriptNamespace = monoScript.GetClass().Namespace;

    if (scriptNamespace == null)
    {
    // No namespace, use the default template
    script = string.Format(template, filename);
    }
    else
    {
    script = string.Format(namespaceTemplate, filename, scriptNamespace);
    }

    // make sure a editor folder exists for us to put this script into...
    var editorFolder = Path.GetDirectoryName(assetPath) + "/Editor";

    if (!Directory.Exists(editorFolder))
    {
    Directory.CreateDirectory(editorFolder);
    }

    if (File.Exists(editorFolder + "/" + filename + "Inspector.cs"))
    {
    Debug.Log("ERROR: " +filename + "Inspector.cs already exists.");
    return;
    }

    // finally write out the new editor~
    File.WriteAllText(editorFolder + "/" + filename + "Inspector.cs", script);
    }

    #region Templates
    static readonly string template = @"using UnityEngine;
    using UnityEditor;
    using System.Collections;
    using System.Collections.Generic;
    @@ -68,4 +102,38 @@ public override void OnInspectorGUI()
    }}
    }}
    ";

    static readonly string namespaceTemplate = @"using UnityEngine;
    using UnityEditor;
    using System.Collections;
    using System.Collections.Generic;
    namespace {1}
    {{
    [CustomEditor(typeof({0}))]
    //[CanEditMultipleObjects]
    public class {0}Inspector : Editor
    {{
    void OnEnable()
    {{
    // TODO: find properties we want to work with
    //serializedObject.FindProperty();
    }}
    public override void OnInspectorGUI()
    {{
    // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
    serializedObject.Update();
    // TODO: Draw UI here
    //EditorGUILayout.PropertyField();
    DrawDefaultInspector();
    // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
    serializedObject.ApplyModifiedProperties();
    }}
    }}
    }}
    ";
    #endregion
    }
  4. LotteMakesStuff revised this gist Jan 15, 2017. No changes.
  5. LotteMakesStuff created this gist Jan 15, 2017.
    71 changes: 71 additions & 0 deletions CustomInspectorCreator.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Collections;

    public static class CustomInspectorCreator
    {
    [MenuItem("Assets/Create/Custom Inspector", priority = 81)]
    static void CreateInsptorEditorClass()
    {
    Object obj = Selection.objects[0];
    string assetPath = AssetDatabase.GetAssetPath(obj);

    var filename = Path.GetFileNameWithoutExtension(assetPath);
    var script = string.Format(template, filename);
    var editorFolder = Path.GetDirectoryName(assetPath) + "/Editor";

    if (!Directory.Exists(editorFolder))
    {
    Directory.CreateDirectory(editorFolder);
    }

    File.WriteAllText(editorFolder + "/" + filename + "Inspector.cs", script);
    AssetDatabase.Refresh();
    }

    [MenuItem("Assets/Create/Custom Inspector", priority = 81, validate = true)]
    static bool ValidateCreateInsptorEditorClass()
    {
    Object obj = Selection.objects[0];
    string path = AssetDatabase.GetAssetPath(obj);

    if (!path.EndsWith(".cs"))
    return false;
    if (path.Contains("Editor"))
    return false;


    return true;
    }

    static string template = @"using UnityEngine;
    using UnityEditor;
    using System.Collections;
    using System.Collections.Generic;
    [CustomEditor(typeof({0}))]
    //[CanEditMultipleObjects]
    public class {0}Inspector : Editor
    {{
    void OnEnable()
    {{
    // TODO: find properties we want to work with
    //serializedObject.FindProperty();
    }}
    public override void OnInspectorGUI()
    {{
    // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
    serializedObject.Update();
    // TODO: Draw UI here
    //EditorGUILayout.PropertyField();
    DrawDefaultInspector();
    // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
    serializedObject.ApplyModifiedProperties();
    }}
    }}
    ";
    }