Last active
March 4, 2025 17:13
-
-
Save LotteMakesStuff/cb63e4e25e5dfdda19a95380e9c03436 to your computer and use it in GitHub Desktop.
Editor extension that adds a tool to automagically generate boilerplate custom inspector code~ YES! Just drop it into a folder called 'Editor' and it adds a 'custom inspector' option into the Project window!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
public static class CustomInspectorCreator | |
{ | |
[MenuItem("Assets/Create/Custom Inspector", priority = 81)] | |
static void CreateInsptorEditorClass() | |
{ | |
foreach (var script in Selection.objects) | |
{ | |
BuildEditorFile(script); | |
} | |
AssetDatabase.Refresh(); | |
} | |
[MenuItem("Assets/Create/Custom Inspector", priority = 81, validate = true)] | |
static bool ValidateCreateInsptorEditorClass() | |
{ | |
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; | |
} | |
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; | |
[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(); | |
}} | |
}} | |
"; | |
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 | |
} |
@pharan ahh thanks for the report!! ♥ I had already updated the gist just before i saw that comment to support multiple file selections, that change cleaned up ValidateCreateInsptorEditorClass()
and should also totally fix that exception too. worth an upgrade :3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ValidateCreateInsptorEditorClass()
needs a check for when nothing is selected (ie, Selection.objects.Length == 0).Otherwise, line 30 throws an
IndexOutOfRangeException
whenever you just right-click on the Project panel.Other than that, I love it! And nice code form too. Very readable and self-documenting. Thanks for sharing it!
I was meaning to write this editor script for myself but I always kept putting it off.