From 811c934f407e46eced351cd3fb1fdfb16cc50ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=89=E3=81=84=E3=81=A1=E3=81=A1=E3=82=83=E3=82=93?= Date: Sat, 10 Dec 2022 03:55:17 +0900 Subject: [PATCH] Added support for AnimatorOverrideController (#139) --- .../Editor/AnimatorMerger.cs | 26 +++++++++++++++++++ .../Editor/RenameParametersHook.cs | 12 +++++++++ .../nadena.dev.modular-avatar/Editor/Util.cs | 7 +++++ 3 files changed, 45 insertions(+) diff --git a/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs b/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs index 7185925c..2d39bb8b 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/AnimatorMerger.cs @@ -36,6 +36,8 @@ namespace nadena.dev.modular_avatar.core.editor internal class AnimatorCombiner { private readonly AnimatorController _combined; + + private AnimatorOverrideController _overrideController; private List _layers = new List(); @@ -90,6 +92,20 @@ namespace nadena.dev.modular_avatar.core.editor } } + public void AddOverrideController(string basePath, AnimatorOverrideController overrideController, bool? writeDefaults) + { + AnimatorController controller = overrideController.runtimeAnimatorController as AnimatorController; + if (controller == null) return; + _overrideController = overrideController; + try + { + this.AddController(basePath, controller, writeDefaults); + } finally + { + _overrideController = null; + } + } + private void insertLayer(string basePath, AnimatorControllerLayer layer, bool first, bool? writeDefaults) { var newLayer = new AnimatorControllerLayer() @@ -260,6 +276,16 @@ namespace nadena.dev.modular_avatar.core.editor throw new Exception($"Unknown type referenced from animator: {original.GetType()}"); } + // When using AnimatorOverrideController, replace the original AnimationClip based on AnimatorOverrideController. + if (_overrideController != null && original is AnimationClip srcClip) + { + T overrideClip = _overrideController[srcClip] as T; + if (overrideClip != null) + { + original = overrideClip; + } + } + if (cloneMap == null) cloneMap = new Dictionary(); if (cloneMap.ContainsKey(original)) diff --git a/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs b/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs index 3515b090..94ecab72 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/RenameParametersHook.cs @@ -126,6 +126,12 @@ namespace nadena.dev.modular_avatar.core.editor { if (willPurgeAnimators) break; // animator will be deleted in subsequent processing + // RuntimeAnimatorController may be AnimatorOverrideController, convert in case of AnimatorOverrideController + if (anim.runtimeAnimatorController is AnimatorOverrideController overrideController) + { + anim.runtimeAnimatorController = Util.ConvertAnimatorController(overrideController); + } + var controller = anim.runtimeAnimatorController as AnimatorController; if (controller != null) { @@ -138,6 +144,12 @@ namespace nadena.dev.modular_avatar.core.editor case ModularAvatarMergeAnimator merger: { + + // RuntimeAnimatorController may be AnimatorOverrideController, convert in case of AnimatorOverrideController + if (merger.animator is AnimatorOverrideController overrideController) + { + merger.animator = Util.ConvertAnimatorController(overrideController); + } var controller = merger.animator as AnimatorController; if (controller != null) { diff --git a/Packages/nadena.dev.modular-avatar/Editor/Util.cs b/Packages/nadena.dev.modular-avatar/Editor/Util.cs index 21a608a1..54ae04ab 100644 --- a/Packages/nadena.dev.modular-avatar/Editor/Util.cs +++ b/Packages/nadena.dev.modular-avatar/Editor/Util.cs @@ -118,6 +118,13 @@ namespace nadena.dev.modular_avatar.core.editor return merger.Finish(); } + public static AnimatorController ConvertAnimatorController(AnimatorOverrideController overrideController) + { + var merger = new AnimatorCombiner(); + merger.AddOverrideController("", overrideController, null); + return merger.Finish(); + } + public static bool IsTemporaryAsset(Object obj) { var path = AssetDatabase.GetAssetPath(obj);