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