Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Last active January 17, 2017 11:30
Show Gist options
  • Save mandarinx/26355b1811314cc9812b42701496cbe0 to your computer and use it in GitHub Desktop.
Save mandarinx/26355b1811314cc9812b42701496cbe0 to your computer and use it in GitHub Desktop.
Create Inspector for a MonoBehaviour

Create Inspector for a MonoBehaviour

Right click on a MonoBehaviour to create an Inspector. Not the prettiest script around, but it works.

Features

  • Puts the inspector in the same namespace as the MonoBehaviour
  • Creates an Editor folder under the same folder as the MonoBehaviour
  • Works on MonoBehaviours and ScriptableObjects.

Known issues

  • Doesn't work on classes that inherits from MonoBehaviour or ScriptableObject. Could be solved with a bit of reflection.
using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
static public class CreateNewInspector {
[MenuItem("Assets/Create/C# Inspector", false, 82)]
static public void CreateInspector() {
UnityEngine.Object obj = Selection.activeObject;
string dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6);
string assetPath = AssetDatabase.GetAssetPath(obj.GetInstanceID());
assetPath = assetPath.Substring(6);
string fullPath = dataPath + "Assets" + assetPath;
string fileName = Path.GetFileName(fullPath);
string assetDir = assetPath.Replace(fileName, "");
if (!Directory.Exists(dataPath + "Assets" + assetDir + "Editor")) {
AssetDatabase.CreateFolder("Assets" + assetDir.Substring(0,assetDir.Length-1), "Editor");
}
string className = "";
string ns = "";
string inspectorName = "";
string[] lines = File.ReadAllLines(fullPath);
foreach (string line in lines) {
if (line.Contains("namespace ")) {
ns = line.Replace("namespace", "");
ns = ns.Replace("{", "");
ns = ns.Replace(" ", "");
}
if (line.Contains("public ") &&
line.Contains("class ")) {
className = line.Replace("public", "");
className = className.Replace("class", "");
int colon = className.IndexOf(":", StringComparison.CurrentCulture);
className = className.Substring(0, colon);
className = className.Replace(" ", "");
inspectorName = className + "Inspector";
}
}
if (className == "") {
Debug.LogWarning("Cannot find classname in "+fileName);
return;
}
string indent = "";
List<string> contents = new List<string>();
contents.Add("using UnityEngine;");
contents.Add("using UnityEditor;");
contents.Add("");
if (ns != "") {
indent = " ";
contents.Add("namespace "+ns+" {");
contents.Add("");
}
contents.Add(indent + "[CustomEditor(typeof("+className+"))]");
contents.Add(indent + "public class "+inspectorName+" : Editor {");
contents.Add("");
contents.Add(indent + " void OnEnable() {");
contents.Add(indent + " }");
contents.Add("");
contents.Add(indent + " public override void OnInspectorGUI() {");
contents.Add(indent + " }");
contents.Add("");
contents.Add(indent + " void OnSceneGUI() {");
contents.Add(indent + " }");
contents.Add(indent + "}");
if (ns != "") {
contents.Add("}");
}
string inspectorPath = AssetDatabase.GenerateUniqueAssetPath("Assets" + assetDir + "Editor/" + inspectorName + ".cs");
File.WriteAllLines(inspectorPath, contents.ToArray());
AssetDatabase.Refresh();
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(inspectorPath);
}
[MenuItem("Assets/Create/C# Inspector", true, 82)]
static public bool CreateInspectorValidator() {
return Selection.activeObject is MonoScript;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment