From c0582a99612a301b7e1b9f99bfe2ff763ceb2457 Mon Sep 17 00:00:00 2001 From: nekobako Date: Sat, 14 Sep 2024 10:57:03 +0900 Subject: [PATCH] fix: non-backwards-compatible changes to component initial values (#1114) * fix: init menu item settings only when added manually from inspector * fix: init menu item settings when added from some shortcuts * fix: init menu item settings when reset from context menu * fix: init merge animator settings only when added manually from inspector --- Editor/Inspector/Menu/MenuPreviewGUI.cs | 8 +++-- .../Inspector/Menu/ToggleCreatorShortcut.cs | 5 +++- Runtime/ModularAvatarMenuItem.cs | 10 +++++++ Runtime/ModularAvatarMergeAnimator.cs | 10 +++++++ Runtime/RuntimeUtil.cs | 29 ++++++++++++++++++- 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/Editor/Inspector/Menu/MenuPreviewGUI.cs b/Editor/Inspector/Menu/MenuPreviewGUI.cs index 9a2fe0ab..bd3e4e25 100644 --- a/Editor/Inspector/Menu/MenuPreviewGUI.cs +++ b/Editor/Inspector/Menu/MenuPreviewGUI.cs @@ -212,7 +212,10 @@ namespace nadena.dev.modular_avatar.core.editor var newChild = new GameObject(); newChild.name = "New item"; newChild.transform.SetParent(nodesUnder.root.transform, false); - newChild.AddComponent(); + + var mami = newChild.AddComponent(); + mami.InitSettings(); + Undo.RegisterCreatedObjectUndo(newChild, "Added menu item"); } @@ -223,13 +226,12 @@ namespace nadena.dev.modular_avatar.core.editor newChild.transform.SetParent(nodesUnder.root.transform, false); var mami = newChild.AddComponent(); + mami.InitSettings(); mami.Control = new VRCExpressionsMenu.Control() { type = VRCExpressionsMenu.Control.ControlType.Toggle, value = 1, }; - mami.isSaved = true; - mami.isSynced = true; newChild.AddComponent(); diff --git a/Editor/Inspector/Menu/ToggleCreatorShortcut.cs b/Editor/Inspector/Menu/ToggleCreatorShortcut.cs index 56105421..dfd18712 100644 --- a/Editor/Inspector/Menu/ToggleCreatorShortcut.cs +++ b/Editor/Inspector/Menu/ToggleCreatorShortcut.cs @@ -40,7 +40,10 @@ namespace nadena.dev.modular_avatar.core.editor var objToggle = toggle.AddComponent(); toggle.transform.SetParent(parent, false); - toggle.AddComponent().Control = new VRCExpressionsMenu.Control + + var mami = toggle.AddComponent(); + mami.InitSettings(); + mami.Control = new VRCExpressionsMenu.Control { type = VRCExpressionsMenu.Control.ControlType.Toggle, name = "New Toggle", diff --git a/Runtime/ModularAvatarMenuItem.cs b/Runtime/ModularAvatarMenuItem.cs index 13b88e08..e38023ee 100644 --- a/Runtime/ModularAvatarMenuItem.cs +++ b/Runtime/ModularAvatarMenuItem.cs @@ -50,6 +50,16 @@ namespace nadena.dev.modular_avatar.core public bool automaticValue; private void Reset() + { + // Init settings only when added or reset manually from the Inspector. + // Otherwise, some plugins that add this component may break in non-playmode builds. + if (RuntimeUtil.IsResetFromInspector()) + { + InitSettings(); + } + } + + internal void InitSettings() { Control = new VRCExpressionsMenu.Control(); Control.type = VRCExpressionsMenu.Control.ControlType.Toggle; diff --git a/Runtime/ModularAvatarMergeAnimator.cs b/Runtime/ModularAvatarMergeAnimator.cs index b48b4100..da3a7412 100644 --- a/Runtime/ModularAvatarMergeAnimator.cs +++ b/Runtime/ModularAvatarMergeAnimator.cs @@ -54,6 +54,16 @@ namespace nadena.dev.modular_avatar.core } private void Reset() + { + // Init settings only when added or reset manually from the Inspector. + // Otherwise, some plugins that add this component may break in non-playmode builds. + if (RuntimeUtil.IsResetFromInspector()) + { + InitSettings(); + } + } + + internal void InitSettings() { deleteAttachedAnimator = true; } diff --git a/Runtime/RuntimeUtil.cs b/Runtime/RuntimeUtil.cs index c5b6f9a3..7ff411f8 100644 --- a/Runtime/RuntimeUtil.cs +++ b/Runtime/RuntimeUtil.cs @@ -23,7 +23,7 @@ */ using System; -using System.Collections.Generic; +using System.Linq; using JetBrains.Annotations; using UnityEngine; @@ -161,5 +161,32 @@ namespace nadena.dev.modular_avatar.core { OnHierarchyChanged?.Invoke(); } + +#if UNITY_EDITOR + private static readonly Type addComponentWindowType = Type.GetType("UnityEditor.AddComponent.AddComponentWindow, UnityEditor"); +#endif + + public static bool IsResetFromInspector(int skipStackFrames = 1) + { +#if !UNITY_EDITOR + return false; +#else + var frames = new System.Diagnostics.StackTrace(skipStackFrames).GetFrames(); + + // Reset from component context menu + if (frames.Length == 1) + { + return true; + } + + // Added from Add Component button + if (frames.Any(x => x.GetMethod().DeclaringType == addComponentWindowType)) + { + return true; + } + + return false; +#endif + } } } \ No newline at end of file