diff --git a/Assets/Editor Default Resources/RicUtils/Common.uss b/Assets/Editor Default Resources/RicUtils/Common.uss new file mode 100644 index 0000000..889e909 --- /dev/null +++ b/Assets/Editor Default Resources/RicUtils/Common.uss @@ -0,0 +1,4 @@ +.hidden{ + height: 0px; + visibility: hidden; +} \ No newline at end of file diff --git a/Assets/Editor Default Resources/RicUtils/Common.uss.meta b/Assets/Editor Default Resources/RicUtils/Common.uss.meta new file mode 100644 index 0000000..053868e --- /dev/null +++ b/Assets/Editor Default Resources/RicUtils/Common.uss.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 800e293977d368d4a879fea5ace9740d +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0} + disableValidation: 0 diff --git a/Assets/Editor Default Resources/RicUtils/TabBar.uss b/Assets/Editor Default Resources/RicUtils/TabBar.uss index 410e932..80b36bb 100644 --- a/Assets/Editor Default Resources/RicUtils/TabBar.uss +++ b/Assets/Editor Default Resources/RicUtils/TabBar.uss @@ -1,8 +1,3 @@ -.hidden{ - height: 0px; - visibility: hidden; -} - .tabBar{ flex-direction: row; background-color: #1e1e1e; diff --git a/Assets/RicUtils/Editor/CustomEditor/AvailableScriptableObjectCustomEditor.cs b/Assets/RicUtils/Editor/CustomEditor/AvailableScriptableObjectCustomEditor.cs index 7745b46..0e832e1 100644 --- a/Assets/RicUtils/Editor/CustomEditor/AvailableScriptableObjectCustomEditor.cs +++ b/Assets/RicUtils/Editor/CustomEditor/AvailableScriptableObjectCustomEditor.cs @@ -6,7 +6,7 @@ using UnityEditor; using UnityEngine; -namespace RicUtils.Editor.CustomEditor +namespace RicUtils.Editor.CustomEditors { [UnityEditor.CustomEditor(typeof(AvailableScriptableObject<>), true)] [CanEditMultipleObjects] diff --git a/Assets/RicUtils/Editor/CustomEditor/CustomScriptableObjectCustomEditor.cs b/Assets/RicUtils/Editor/CustomEditor/CustomScriptableObjectCustomEditor.cs index 82eb4a2..f14af93 100644 --- a/Assets/RicUtils/Editor/CustomEditor/CustomScriptableObjectCustomEditor.cs +++ b/Assets/RicUtils/Editor/CustomEditor/CustomScriptableObjectCustomEditor.cs @@ -5,7 +5,7 @@ using UnityEditor; using UnityEngine; -namespace RicUtils.Editor.CustomEditor +namespace RicUtils.Editor.CustomEditors { [UnityEditor.CustomEditor(typeof(GenericScriptableObject), true)] [CanEditMultipleObjects] diff --git a/Assets/RicUtils/Editor/Elements.meta b/Assets/RicUtils/Editor/Elements.meta index e6ba589..eed95d1 100644 --- a/Assets/RicUtils/Editor/Elements.meta +++ b/Assets/RicUtils/Editor/Elements.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: e4df405ebfbe441499dc3604c2f2f536 +guid: ecc7a2d49f3b92449b67842c631605f1 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/RicUtils/Editor/Templates/Script-NewAvailableScriptableObject.cs.txt b/Assets/RicUtils/Editor/Templates/Script-NewAvailableScriptableObject.cs.txt index 15530e9..58749fa 100644 --- a/Assets/RicUtils/Editor/Templates/Script-NewAvailableScriptableObject.cs.txt +++ b/Assets/RicUtils/Editor/Templates/Script-NewAvailableScriptableObject.cs.txt @@ -7,6 +7,6 @@ using RicUtils.ScriptableObjects; #ROOTNAMESPACEBEGIN# public class #SCRIPTNAME# : AvailableScriptableObject<#SCRIPTABLEOBJECT#> { - + #NOTRIM# } #ROOTNAMESPACEEND# \ No newline at end of file diff --git a/Assets/RicUtils/Editor/Templates/Script-NewGenericEditorWindow.cs.txt b/Assets/RicUtils/Editor/Templates/Script-NewGenericEditorWindow.cs.txt index 5b39349..09343d7 100644 --- a/Assets/RicUtils/Editor/Templates/Script-NewGenericEditorWindow.cs.txt +++ b/Assets/RicUtils/Editor/Templates/Script-NewGenericEditorWindow.cs.txt @@ -16,17 +16,17 @@ public class #SCRIPTNAME# : GenericEditorWindow<#SCRIPTABLEOBJECT#, #AVAILABLESC protected override void DrawGUI() { - + #NOTRIM# } protected override void LoadScriptableObject(#SCRIPTABLEOBJECT# so, bool isNull) { - + #NOTRIM# } protected override void CreateAsset(ref #SCRIPTABLEOBJECT# asset) { - + #NOTRIM# } } #ROOTNAMESPACEEND# \ No newline at end of file diff --git a/Assets/RicUtils/Editor/Templates/Script-NewGenericScriptableObject.cs.txt b/Assets/RicUtils/Editor/Templates/Script-NewGenericScriptableObject.cs.txt index 3b463c3..a3aab3a 100644 --- a/Assets/RicUtils/Editor/Templates/Script-NewGenericScriptableObject.cs.txt +++ b/Assets/RicUtils/Editor/Templates/Script-NewGenericScriptableObject.cs.txt @@ -7,6 +7,6 @@ using RicUtils.ScriptableObjects; #ROOTNAMESPACEBEGIN# public class #SCRIPTNAME# : GenericScriptableObject { - + #NOTRIM# } #ROOTNAMESPACEEND# \ No newline at end of file diff --git a/Assets/RicUtils/Editor/UIElements.meta b/Assets/RicUtils/Editor/UIElements.meta new file mode 100644 index 0000000..e6ba589 --- /dev/null +++ b/Assets/RicUtils/Editor/UIElements.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e4df405ebfbe441499dc3604c2f2f536 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RicUtils/Editor/Elements/AdvButton.cs b/Assets/RicUtils/Editor/UIElements/AdvButton.cs similarity index 96% rename from Assets/RicUtils/Editor/Elements/AdvButton.cs rename to Assets/RicUtils/Editor/UIElements/AdvButton.cs index 7ef929d..2e9ed66 100644 --- a/Assets/RicUtils/Editor/Elements/AdvButton.cs +++ b/Assets/RicUtils/Editor/UIElements/AdvButton.cs @@ -4,7 +4,7 @@ using UnityEngine.Scripting; using UnityEngine.UIElements; -namespace RicUtils.Editor +namespace RicUtils.Editor.UIElements { // web* src: https://gist.github.com/andrew-raphael-lukasik/69c7858e39e22f197ca51b318b218cc7 [Preserve] diff --git a/Assets/RicUtils/Editor/Elements/AdvButton.cs.meta b/Assets/RicUtils/Editor/UIElements/AdvButton.cs.meta similarity index 100% rename from Assets/RicUtils/Editor/Elements/AdvButton.cs.meta rename to Assets/RicUtils/Editor/UIElements/AdvButton.cs.meta diff --git a/Assets/RicUtils/Editor/Elements/TabBar.cs b/Assets/RicUtils/Editor/UIElements/TabBar.cs similarity index 98% rename from Assets/RicUtils/Editor/Elements/TabBar.cs rename to Assets/RicUtils/Editor/UIElements/TabBar.cs index 0cfd16a..3ed512d 100644 --- a/Assets/RicUtils/Editor/Elements/TabBar.cs +++ b/Assets/RicUtils/Editor/UIElements/TabBar.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine.UIElements; -namespace RicUtils.Editor.Elements +namespace RicUtils.Editor.UIElements { public class TabBar { diff --git a/Assets/RicUtils/Editor/Elements/TabBar.cs.meta b/Assets/RicUtils/Editor/UIElements/TabBar.cs.meta similarity index 100% rename from Assets/RicUtils/Editor/Elements/TabBar.cs.meta rename to Assets/RicUtils/Editor/UIElements/TabBar.cs.meta diff --git a/Assets/RicUtils/Editor/Elements/TabData.cs b/Assets/RicUtils/Editor/UIElements/TabData.cs similarity index 82% rename from Assets/RicUtils/Editor/Elements/TabData.cs rename to Assets/RicUtils/Editor/UIElements/TabData.cs index 38c35b6..cd20666 100644 --- a/Assets/RicUtils/Editor/Elements/TabData.cs +++ b/Assets/RicUtils/Editor/UIElements/TabData.cs @@ -1,6 +1,6 @@ using UnityEngine.UIElements; -namespace RicUtils.Editor.Elements +namespace RicUtils.Editor.UIElements { public struct TabData { diff --git a/Assets/RicUtils/Editor/Elements/TabData.cs.meta b/Assets/RicUtils/Editor/UIElements/TabData.cs.meta similarity index 100% rename from Assets/RicUtils/Editor/Elements/TabData.cs.meta rename to Assets/RicUtils/Editor/UIElements/TabData.cs.meta diff --git a/Assets/RicUtils/Editor/Utilities/DoCreateScriptAsset.cs b/Assets/RicUtils/Editor/Utilities/DoCreateScriptAsset.cs index 35786f6..8e60714 100644 --- a/Assets/RicUtils/Editor/Utilities/DoCreateScriptAsset.cs +++ b/Assets/RicUtils/Editor/Utilities/DoCreateScriptAsset.cs @@ -13,119 +13,13 @@ public class DoCreateScriptAsset : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) { - Object o = CreateScriptAssetFromTemplate(pathName, resourceFile); + Object o = FileUtilities.CreateScriptAssetFromTemplate(pathName, resourceFile, CustomReplaces); ProjectWindowUtil.ShowCreatedAsset(o); } - private Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) - { - string content = File.ReadAllText(resourceFile); - var method = typeof(ProjectWindowUtil).GetMethod("CreateScriptAssetWithContent", BindingFlags.Static | BindingFlags.NonPublic); - return (Object)method.Invoke(null, new object[] { pathName, PreprocessScriptAssetTemplate(pathName, content) }); - } - - // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs - private string PreprocessScriptAssetTemplate(string pathName, string resourceContent) - { - string rootNamespace = null; - - if (Path.GetExtension(pathName) == ".cs") - { - rootNamespace = CompilationPipeline.GetAssemblyRootNamespaceFromScriptPath(pathName); - } - - string content = resourceContent; - - // #NOTRIM# is a special marker that is used to mark the end of a line where we want to leave whitespace. prevent editors auto-stripping it by accident. - content = content.Replace("#NOTRIM#", ""); - - // macro replacement - string baseFile = Path.GetFileNameWithoutExtension(pathName); - - content = content.Replace("#NAME#", baseFile); - string baseFileNoSpaces = baseFile.Replace(" ", ""); - content = content.Replace("#SCRIPTNAME#", baseFileNoSpaces); - - content = CustomReplaces(content); - - content = RemoveOrInsertNamespace(content, rootNamespace); - - // if the script name begins with an uppercase character we support a lowercase substitution variant - if (char.IsUpper(baseFileNoSpaces, 0)) - { - baseFileNoSpaces = char.ToLower(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); - content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); - } - else - { - // still allow the variant, but change the first character to upper and prefix with "my" - baseFileNoSpaces = "my" + char.ToUpper(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); - content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); - } - - return content; - } - protected virtual string CustomReplaces(string content) { return content; } - - private string RemoveOrInsertNamespace(string content, string rootNamespace) - { - var rootNamespaceBeginTag = "#ROOTNAMESPACEBEGIN#"; - var rootNamespaceEndTag = "#ROOTNAMESPACEEND#"; - - if (!content.Contains(rootNamespaceBeginTag) || !content.Contains(rootNamespaceEndTag)) - return content; - - if (string.IsNullOrEmpty(rootNamespace)) - { - content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceBeginTag}[ \\t]*", string.Empty); - content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceEndTag}[ \\t]*", string.Empty); - - return content; - } - - // Use first found newline character as newline for entire file after replace. - var newline = content.Contains("\r\n") ? "\r\n" : "\n"; - var contentLines = new List(content.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None)); - - int i = 0; - - for (; i < contentLines.Count; ++i) - { - if (contentLines[i].Contains(rootNamespaceBeginTag)) - break; - } - - var beginTagLine = contentLines[i]; - - // Use the whitespace between beginning of line and #ROOTNAMESPACEBEGIN# as identation. - var indentationString = beginTagLine.Substring(0, beginTagLine.IndexOf("#")); - - contentLines[i] = $"namespace {rootNamespace}"; - contentLines.Insert(i + 1, "{"); - - i += 2; - - for (; i < contentLines.Count; ++i) - { - var line = contentLines[i]; - - if (string.IsNullOrEmpty(line) || line.Trim().Length == 0) - continue; - - if (line.Contains(rootNamespaceEndTag)) - { - contentLines[i] = "}"; - break; - } - - contentLines[i] = $"{indentationString}{line}"; - } - - return string.Join(newline, contentLines.ToArray()); - } } } diff --git a/Assets/RicUtils/Editor/Utilities/EditorGUIHelper.cs b/Assets/RicUtils/Editor/Utilities/EditorGUIHelper.cs index 50762cd..af5741a 100644 --- a/Assets/RicUtils/Editor/Utilities/EditorGUIHelper.cs +++ b/Assets/RicUtils/Editor/Utilities/EditorGUIHelper.cs @@ -1,4 +1,4 @@ -using RicUtils.Editor.Elements; +using RicUtils.Editor.UIElements; using System.Collections; using System.Linq; using System.Reflection; @@ -407,5 +407,10 @@ public static TabBar AddTabBar(this VisualElement root) return tabBar; } + + public static void AddCommonStylesheet(this VisualElement root) + { + root.AddStylesheet("RicUtils/Common.uss"); + } } } diff --git a/Assets/RicUtils/Editor/Utilities/FileUtilities.cs b/Assets/RicUtils/Editor/Utilities/FileUtilities.cs new file mode 100644 index 0000000..4c51e41 --- /dev/null +++ b/Assets/RicUtils/Editor/Utilities/FileUtilities.cs @@ -0,0 +1,153 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text.RegularExpressions; +using UnityEditor; +using UnityEditor.Compilation; +using UnityEditor.ProjectWindowCallback; +using UnityEngine; + +namespace RicUtils.Editor.Utilities +{ + public static class FileUtilities + { + public static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile) + { + return CreateScriptAssetFromTemplate(pathName, resourceFile, null); + } + + public static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile, System.Func customReplace) + { + string content = File.ReadAllText(resourceFile); + var method = typeof(ProjectWindowUtil).GetMethod("CreateScriptAssetWithContent", BindingFlags.Static | BindingFlags.NonPublic); + return (Object)method.Invoke(null, new object[] { pathName, PreprocessScriptAssetTemplate(pathName, content, customReplace) }); + } + + // https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs + private static string PreprocessScriptAssetTemplate(string pathName, string resourceContent, System.Func customReplace) + { + string rootNamespace = null; + + if (Path.GetExtension(pathName) == ".cs") + { + rootNamespace = CompilationPipeline.GetAssemblyRootNamespaceFromScriptPath(pathName); + } + + string content = resourceContent; + + // #NOTRIM# is a special marker that is used to mark the end of a line where we want to leave whitespace. prevent editors auto-stripping it by accident. + content = content.Replace("#NOTRIM#", ""); + + // macro replacement + string baseFile = Path.GetFileNameWithoutExtension(pathName); + + content = content.Replace("#NAME#", baseFile); + string baseFileNoSpaces = baseFile.Replace(" ", ""); + content = content.Replace("#SCRIPTNAME#", baseFileNoSpaces); + + if (customReplace != null) + { + content = customReplace(content); + } + + content = RemoveOrInsertNamespace(content, rootNamespace); + + // if the script name begins with an uppercase character we support a lowercase substitution variant + if (char.IsUpper(baseFileNoSpaces, 0)) + { + baseFileNoSpaces = char.ToLower(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); + content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); + } + else + { + // still allow the variant, but change the first character to upper and prefix with "my" + baseFileNoSpaces = "my" + char.ToUpper(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1); + content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces); + } + + return content; + } + + private static string RemoveOrInsertNamespace(string content, string rootNamespace) + { + var rootNamespaceBeginTag = "#ROOTNAMESPACEBEGIN#"; + var rootNamespaceEndTag = "#ROOTNAMESPACEEND#"; + + if (!content.Contains(rootNamespaceBeginTag) || !content.Contains(rootNamespaceEndTag)) + return content; + + if (string.IsNullOrEmpty(rootNamespace)) + { + content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceBeginTag}[ \\t]*", string.Empty); + content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceEndTag}[ \\t]*", string.Empty); + + return content; + } + + // Use first found newline character as newline for entire file after replace. + var newline = content.Contains("\r\n") ? "\r\n" : "\n"; + var contentLines = new List(content.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None)); + + int i = 0; + + for (; i < contentLines.Count; ++i) + { + if (contentLines[i].Contains(rootNamespaceBeginTag)) + break; + } + + var beginTagLine = contentLines[i]; + + // Use the whitespace between beginning of line and #ROOTNAMESPACEBEGIN# as identation. + var indentationString = beginTagLine.Substring(0, beginTagLine.IndexOf("#")); + + contentLines[i] = $"namespace {rootNamespace}"; + contentLines.Insert(i + 1, "{"); + + i += 2; + + for (; i < contentLines.Count; ++i) + { + var line = contentLines[i]; + + if (string.IsNullOrEmpty(line) || line.Trim().Length == 0) + continue; + + if (line.Contains(rootNamespaceEndTag)) + { + contentLines[i] = "}"; + break; + } + + contentLines[i] = $"{indentationString}{line}"; + } + + return string.Join(newline, contentLines.ToArray()); + } + + public static void CreateNewScript(string defaultNewFileName, string templatePath) + { + CreateNewScript(defaultNewFileName, templatePath); + } + + public static void CreateNewScript(string defaultNewFileName, string templatePath) where T : EndNameEditAction + { + var endAction = ScriptableObject.CreateInstance(); + CreateNewScript(endAction, defaultNewFileName, templatePath); + } + + public static void CreateNewScript(T endAction, string defaultNewFileName, string templatePath) where T : EndNameEditAction + { + ProjectWindowUtil.StartNameEditingIfProjectWindowExists(icon: Path.GetExtension(defaultNewFileName) switch + { + ".cs" => EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, + ".shader" => EditorGUIUtility.IconContent("Shader Icon").image as Texture2D, + ".asmdef" => EditorGUIUtility.IconContent("AssemblyDefinitionAsset Icon").image as Texture2D, + ".asmref" => EditorGUIUtility.IconContent("AssemblyDefinitionReferenceAsset Icon").image as Texture2D, + _ => EditorGUIUtility.IconContent("TextAsset Icon").image as Texture2D, + }, instanceID: 0, endAction: endAction, pathName: defaultNewFileName, resourceFile: templatePath); + AssetDatabase.Refresh(); + } + } +} diff --git a/Assets/RicUtils/Editor/Utilities/FileUtilities.cs.meta b/Assets/RicUtils/Editor/Utilities/FileUtilities.cs.meta new file mode 100644 index 0000000..d1083b8 --- /dev/null +++ b/Assets/RicUtils/Editor/Utilities/FileUtilities.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7470229ed78b4f74eaf53d1475ef36b4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs b/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs index 0cabc37..3f86c5a 100644 --- a/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs +++ b/Assets/RicUtils/Editor/Utilities/ToolUtilities.cs @@ -106,30 +106,6 @@ public static bool TryGetActiveFolderPath(out string path) return found; } - public static void CreateNewScript(string defaultNewFileName, string templatePath) - { - CreateNewScript(defaultNewFileName, templatePath); - } - - public static void CreateNewScript(string defaultNewFileName, string templatePath) where T : EndNameEditAction - { - var endAction = ScriptableObject.CreateInstance(); - CreateNewScript(endAction, defaultNewFileName, templatePath); - } - - public static void CreateNewScript(T endAction, string defaultNewFileName, string templatePath) where T : EndNameEditAction - { - ProjectWindowUtil.StartNameEditingIfProjectWindowExists(icon: Path.GetExtension(defaultNewFileName) switch - { - ".cs" => EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D, - ".shader" => EditorGUIUtility.IconContent("Shader Icon").image as Texture2D, - ".asmdef" => EditorGUIUtility.IconContent("AssemblyDefinitionAsset Icon").image as Texture2D, - ".asmref" => EditorGUIUtility.IconContent("AssemblyDefinitionReferenceAsset Icon").image as Texture2D, - _ => EditorGUIUtility.IconContent("TextAsset Icon").image as Texture2D, - }, instanceID: 0, endAction: endAction, pathName: defaultNewFileName, resourceFile: templatePath); - AssetDatabase.Refresh(); - } - public static void AddStylesheet(this VisualElement root, params string[] styleSheets) { foreach (var sheet in styleSheets) diff --git a/Assets/RicUtils/Editor/Windows/CreateScriptableObjectEditorWindow.cs b/Assets/RicUtils/Editor/Windows/CreateScriptableObjectEditorWindow.cs index b99af4a..a5eecc7 100644 --- a/Assets/RicUtils/Editor/Windows/CreateScriptableObjectEditorWindow.cs +++ b/Assets/RicUtils/Editor/Windows/CreateScriptableObjectEditorWindow.cs @@ -10,6 +10,7 @@ using UnityEditor.Compilation; using UnityEngine; using UnityEngine.UIElements; +using static UnityEngine.GraphicsBuffer; namespace RicUtils.Editor.Windows { @@ -18,6 +19,7 @@ public class CreateScriptableObjectEditorWindow : EditorWindow private EditorContainer scriptableObjectName = new EditorContainer(); private EditorContainer availableScriptableObjectName = new EditorContainer(); private EditorContainer editorWindowName = new EditorContainer(); + private EditorContainer openInEditor = new EditorContainer(); private TextField soNameTextField; private TextField availableSoTextField; @@ -37,13 +39,10 @@ private void OnEnable() private void CreateGUI() { - { - rootVisualElement.AddLabel("Assets To Create"); - } + rootVisualElement.AddCommonStylesheet(); - { - rootVisualElement.AddSeparator(new Color32(37, 37, 37, 255)); - } + rootVisualElement.AddLabel("Assets To Create"); + rootVisualElement.AddSeparator(new Color32(37, 37, 37, 255)); soNameTextField = rootVisualElement.AddTextField(scriptableObjectName, "Scriptable Object", (old) => { @@ -84,6 +83,8 @@ private void CreateGUI() ToggleWarning(false); } + rootVisualElement.AddToggle(openInEditor, "Open in editor when files are created"); + rootVisualElement.AddButton("Create", CreateAssets); soNameTextField.Focus(); @@ -120,17 +121,17 @@ private void CreateAssets() if (!string.IsNullOrEmpty(rootNamespace)) { rootNamespace += "."; } + Object genericSoFile; + Object availableSoFile; + Object editorWindowFile; + { string defaultNewFileName = Path.Combine(path, soName + ".cs"); string templatePath = PathConstants.TEMPLATES_PATH + "/Script-NewGenericScriptableObject.cs.txt"; - var endAction = ScriptableObject.CreateInstance(); - - endAction.Action(0, defaultNewFileName, templatePath); - - //ToolUtilities.CreateNewScript(defaultNewFileName, templatePath); + genericSoFile = FileUtilities.CreateScriptAssetFromTemplate(defaultNewFileName, templatePath); } { @@ -138,13 +139,11 @@ private void CreateAssets() string templatePath = PathConstants.TEMPLATES_PATH + "/Script-NewAvailableScriptableObject.cs.txt"; - var endAction = ScriptableObject.CreateInstance(); - - endAction.scriptableObject = soName; - - endAction.Action(0, defaultNewFileName, templatePath); - - //ToolUtilities.CreateNewScript(endAction, defaultNewFileName, templatePath); + availableSoFile = FileUtilities.CreateScriptAssetFromTemplate(defaultNewFileName, templatePath, (content) => + { + content = content.Replace("#SCRIPTABLEOBJECT#", soName); + return content; + }); } { @@ -152,25 +151,36 @@ private void CreateAssets() string templatePath = PathConstants.TEMPLATES_PATH + "/Script-NewGenericEditorWindow.cs.txt"; - var endAction = ScriptableObject.CreateInstance(); - - endAction.scriptableObject = soName; - endAction.availableScriptableObject = availableSo; - - endAction.Action(0, defaultNewFileName, templatePath); - - //ToolUtilities.CreateNewScript(endAction, defaultNewFileName, templatePath); + editorWindowFile = FileUtilities.CreateScriptAssetFromTemplate(defaultNewFileName, templatePath, (content) => + { + content = content.Replace("#SCRIPTABLEOBJECT#", soName); + content = content.Replace("#AVAILABLESCRIPTABLEOBJECT#", availableSo); + return content; + }); } EditorPrefs.SetString("CustomSo", $"{rootNamespace}{soName},{dll}"); EditorPrefs.SetString("AvailableSo", $"{rootNamespace}{availableSo},{dll}"); EditorPrefs.SetString("EditorWindow", $"{rootNamespace}{editorWindow},{dll}"); + + if (openInEditor.Value) + { + AssetDatabase.OpenAsset(genericSoFile); + AssetDatabase.OpenAsset(availableSoFile); + AssetDatabase.OpenAsset(editorWindowFile); + } } private void ToggleWarning(bool visible) { - emptyFieldWarningContainer.visible = visible; - emptyFieldWarningContainer.style.height = visible ? new StyleLength(StyleKeyword.Null) : 0; + if (visible) + { + emptyFieldWarningContainer.RemoveFromClassList("hidden"); + } + else + { + emptyFieldWarningContainer.AddToClassList("hidden"); + } } [DidReloadScripts] diff --git a/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs b/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs index 35c498d..e659f80 100644 --- a/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs +++ b/Assets/RicUtils/Editor/Windows/GenericEditorWindow.cs @@ -38,6 +38,8 @@ protected virtual void OnEnable() private void CreateGUI() { + rootVisualElement.AddCommonStylesheet(); + onLoad = null; soObjectField = rootVisualElement.AddObjectField(scriptableObject, "Scriptable Object", () => {