Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MSylvia/ab92f81f10dc71a2d80ebc33f25b92cd to your computer and use it in GitHub Desktop.

Select an option

Save MSylvia/ab92f81f10dc71a2d80ebc33f25b92cd 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!
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();
}}
}}
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment