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
This commit is contained in:
nekobako 2024-09-14 10:57:03 +09:00 committed by GitHub
parent 3eaf8bee6d
commit c0582a9961
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 5 deletions

View File

@ -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<ModularAvatarMenuItem>();
var mami = newChild.AddComponent<ModularAvatarMenuItem>();
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<ModularAvatarMenuItem>();
mami.InitSettings();
mami.Control = new VRCExpressionsMenu.Control()
{
type = VRCExpressionsMenu.Control.ControlType.Toggle,
value = 1,
};
mami.isSaved = true;
mami.isSynced = true;
newChild.AddComponent<ModularAvatarObjectToggle>();

View File

@ -40,7 +40,10 @@ namespace nadena.dev.modular_avatar.core.editor
var objToggle = toggle.AddComponent<ModularAvatarObjectToggle>();
toggle.transform.SetParent(parent, false);
toggle.AddComponent<ModularAvatarMenuItem>().Control = new VRCExpressionsMenu.Control
var mami = toggle.AddComponent<ModularAvatarMenuItem>();
mami.InitSettings();
mami.Control = new VRCExpressionsMenu.Control
{
type = VRCExpressionsMenu.Control.ControlType.Toggle,
name = "New Toggle",

View File

@ -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;

View File

@ -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;
}

View File

@ -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
}
}
}