private static void SaveEditor(SerializedProperty property) { Type componentType = property.serializedObject.targetObject.GetType(); MonoScript script = GetScript(property.serializedObject.targetObject); string scriptPath = AssetDatabase.GetAssetPath(script); string directory = Path.GetDirectoryName(scriptPath) + "/Editor/"; UnityUtils.CreateFoldersFor(directory); string path = EditorUtility.SaveFilePanel("Save " + componentType.Name + " Custom Editor", directory, componentType.Name + "Editor.cs", "cs"); if (path.IsNullOrEmpty()) { return; } path = path.ReplaceRegex("^.*/Assets/", "Assets/"); string text = string.Format(@"using UnityEditor; namespace {0} {{ // NOTE: This CustomEditor allows the ScriptableObjects referenced in {1} to be expandable in the Inspector. [CustomEditor(typeof({1}))] public class {1}Editor : UnityEditor.Editor {{ }} }}", componentType.Namespace.IsNullOrEmpty() ? "CoreUtils" : componentType.Namespace, componentType.Name); UnityUtils.CreateFoldersFor(path); File.WriteAllText(path, text); AssetDatabase.ImportAsset(path); }
private static void CreateEditorScript() { string path = AssetDatabase.GetAssetPath(Selection.activeObject); string className = Path.GetFileNameWithoutExtension(path); string classEditorName = className + "Editor"; string editorPath = GetEditorPath(path); if (!EditorUtility.DisplayDialog("Confirm script creation", string.Format("Create editor script \"{0}\" for class \"{1}\"?", editorPath, className), "OK", "Cancel")) { return; } UnityUtils.CreateFoldersFor(editorPath); // Try and extract namespace from existing script TextReader reader = new StreamReader(path); string textLine = reader.ReadLine(); string namespaceLine = null; while (textLine != null) { if (textLine.Contains("namespace")) { namespaceLine = textLine; break; } textLine = reader.ReadLine(); } reader.Close(); if (namespaceLine == null) { namespaceLine = "namespace CoreUtils.Editor {"; } // Write editor script TextWriter writer = new StreamWriter(editorPath); writer.Write(@"using CoreUtils; using CoreUtils.Editor; using UnityEditor; {0} [CustomEditor(typeof({1}))] public class {2} : Editor<{1}> {{ public override void OnInspectorGUI() {{ base.OnInspectorGUI(); }} }} }}", namespaceLine, className, classEditorName); writer.Close(); AssetDatabase.ImportAsset(editorPath); AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath <Object>(editorPath)); }