From 9b80949e975f0c68984432b1e1e53d00737ff3ee Mon Sep 17 00:00:00 2001 From: yum Date: Tue, 18 Feb 2025 00:44:41 -0800 Subject: Bugfix: reset pre existing YOTS menus before using them --- Editor/Generate_Animator.cs | 70 ++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/Editor/Generate_Animator.cs b/Editor/Generate_Animator.cs index 648921a..e9c040e 100644 --- a/Editor/Generate_Animator.cs +++ b/Editor/Generate_Animator.cs @@ -1042,6 +1042,7 @@ namespace YOTS private static VRCExpressionsMenu GetOrCreateSubmenu(VRCExpressionsMenu parentMenu, string submenuName, string generatedDir) { + Debug.Log($"Getting or creating submenu {submenuName} under {parentMenu.name} at {generatedDir}"); if (parentMenu.controls == null) parentMenu.controls = new List(); @@ -1074,11 +1075,37 @@ namespace YOTS }; parentMenu.controls.Add(newControl); EditorUtility.SetDirty(parentMenu); + EditorUtility.SetDirty(newSubmenu); Debug.Log($"Created submenu '{submenuName}' at {submenuAssetPath}"); return newSubmenu; } + private static void InitializeSubmenu(VRCExpressionsMenu menu) + { + if (menu == null) return; + + // Clear existing controls + if (menu.controls != null) + { + // Recursively initialize any existing submenus before removing them + foreach (var control in menu.controls) + { + if (control.type == VRCExpressionsMenu.Control.ControlType.SubMenu && control.subMenu != null) + { + InitializeSubmenu(control.subMenu); + } + } + menu.controls.Clear(); + } + else + { + menu.controls = new List(); + } + + EditorUtility.SetDirty(menu); + } + private static void GenerateVRChatAssets(List toggleSpecs, string generatedDir, string existingParamsPath = null, string existingMenuPath = null) { // Create a unique list of toggle names for the parameters. @@ -1086,28 +1113,22 @@ namespace YOTS // Create or update the VRC Expression Parameters asset. VRCExpressionParameters expressionParameters; - if (!string.IsNullOrEmpty(existingParamsPath)) - { + if (string.IsNullOrEmpty(existingParamsPath)) { + expressionParameters = ScriptableObject.CreateInstance(); + } else { expressionParameters = AssetDatabase.LoadAssetAtPath(existingParamsPath); - if (expressionParameters == null) - { + if (expressionParameters == null) { Debug.LogError($"Could not load existing parameters at path: {existingParamsPath}"); return; } } - else - { - expressionParameters = ScriptableObject.CreateInstance(); - } // Merge existing parameters with new toggle parameters. var paramList = new List(); - if (expressionParameters.parameters != null) - { + if (expressionParameters.parameters != null) { paramList.AddRange(expressionParameters.parameters.Where(p => !parameters.Contains(p.name))); } - foreach (var param in parameters) - { + foreach (var param in parameters) { paramList.Add(new VRCExpressionParameters.Parameter { name = param, @@ -1138,15 +1159,24 @@ namespace YOTS mainMenu.controls.RemoveAll(c => c.name == "YOTS"); } - // Create the root YOTS submenu. - var yotsSubmenu = ScriptableObject.CreateInstance(); - yotsSubmenu.name = "YOTS"; - if (yotsSubmenu.controls == null) - yotsSubmenu.controls = new List(); - + // Create or load the root YOTS submenu string yotsSubmenuPath = Path.Combine(generatedDir, "YOTS_Submenu.asset"); - AssetDatabase.CreateAsset(yotsSubmenu, yotsSubmenuPath); - Debug.Log("Created YOTS submenu at: " + yotsSubmenuPath); + VRCExpressionsMenu yotsSubmenu; + + if (AssetDatabase.LoadAssetAtPath(yotsSubmenuPath) != null) + { + yotsSubmenu = AssetDatabase.LoadAssetAtPath(yotsSubmenuPath); + InitializeSubmenu(yotsSubmenu); + Debug.Log("Reset existing YOTS submenu at: " + yotsSubmenuPath); + } + else + { + yotsSubmenu = ScriptableObject.CreateInstance(); + yotsSubmenu.name = "YOTS"; + yotsSubmenu.controls = new List(); + AssetDatabase.CreateAsset(yotsSubmenu, yotsSubmenuPath); + Debug.Log("Created new YOTS submenu at: " + yotsSubmenuPath); + } // For each toggle, determine where its control should live based on its menuPath. foreach (var toggle in toggleSpecs) -- cgit v1.2.3