From 2530588fe07cf31ed7de97146cd7d04a6968a69b Mon Sep 17 00:00:00 2001 From: bd_ <64174065+bdunderscore@users.noreply.github.com> Date: Wed, 30 Nov 2022 12:42:57 -0800 Subject: [PATCH] Fix animator cloning creating duplicate MonoScript objects (#116) Closes: #115 --- .../Editor/AnimatorMerger.cs | 25 +++++++++++++++++-- .../Editor/RenameParametersHook.cs | 5 +++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs b/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs index 3e4962be..46f98330 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs @@ -231,11 +231,32 @@ namespace nadena.dev.modular_avatar.core.editor { if (original == null) return null; + // We want to avoid trying to copy assets not part of the animation system (eg - textures, meshes, + // MonoScripts...), so check for the types we care about here switch (original) { - case Texture _: - case Material _: + // Any object referenced by an animator that we intend to mutate needs to be listed here. + case Motion _: + case AnimatorController _: + case AnimatorState _: + case AnimatorStateMachine _: + case AnimatorStateTransition _: + case StateMachineBehaviour _: + break; // We want to clone these types + + // Leave textures and script definitions alone + case Texture2D _: + case MonoScript _: return original; + + // Also avoid copying unknown scriptable objects. + // This ensures compatibility with e.g. avatar remote, which stores state information in a state + // behaviour referencing a custom ScriptableObject + case ScriptableObject _: + return original; + + default: + throw new Exception($"Unknown type referenced from animator: {original.GetType()}"); } if (cloneMap == null) cloneMap = new Dictionary(); diff --git a/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs b/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs index d4c16d3b..3515b090 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs @@ -205,7 +205,10 @@ namespace nadena.dev.modular_avatar.core.editor var queue = new Queue(); // Deep clone the animator - controller = Util.DeepCloneAnimator(controller); + if (!Util.IsTemporaryAsset(controller)) + { + controller = Util.DeepCloneAnimator(controller); + } var parameters = controller.parameters; for (int i = 0; i < parameters.Length; i++)