public SynchronizeObject(Animator animator) { this.animator = animator; gameObject = animator.gameObject; propertyModifications = PrefabUtility.GetPropertyModifications(gameObject); transformPoseSave = new TransformPoseSave(gameObject); blendShapeWeightSave = new BlendShapeWeightSave(gameObject); #region Clip { var saveSettings = gameObject.GetComponent <VeryAnimationSaveSettings>(); if (saveSettings != null && saveSettings.lastSelectAnimationClip != null) { if (ArrayUtility.Contains(AnimationUtility.GetAnimationClips(gameObject), saveSettings.lastSelectAnimationClip)) { clip = saveSettings.lastSelectAnimationClip; } } if (clip == null) { var ac = EditorCommon.GetAnimatorController(animator); if (ac != null && ac.layers.Length > 0) { var state = ac.layers[0].stateMachine.defaultState; if (state != null) { if (state.motion is UnityEditor.Animations.BlendTree) { Action <UnityEditor.Animations.BlendTree> FindBlendTree = null; FindBlendTree = (blendTree) => { if (blendTree.children == null) { return; } var children = blendTree.children; for (int i = 0; i < children.Length; i++) { if (children[i].motion is UnityEditor.Animations.BlendTree) { FindBlendTree(children[i].motion as UnityEditor.Animations.BlendTree); } else { clip = children[i].motion as AnimationClip; } if (clip != null) { break; } } blendTree.children = children; }; FindBlendTree(state.motion as UnityEditor.Animations.BlendTree); } else { clip = state.motion as AnimationClip; } } } if (clip != null) { var owc = animator.runtimeAnimatorController as AnimatorOverrideController; if (owc != null) { clip = owc[clip]; } } } } #endregion animator.enabled = false; //In order to avoid the mysterious behavior where an event is called from "UnityEditor.Handles: DrawCameraImpl", it is invalid except when updating }
public SynchronizeAnimation() { synchronizeObjects = new List <SynchronizeObject>(); Action <GameObject> AddGameObject = (go) => { Func <Transform, bool> CheckHideFlags = null; CheckHideFlags = (t) => { if ((t.gameObject.hideFlags & (HideFlags.HideAndDontSave | HideFlags.NotEditable)) != 0) { return(false); } if (t.parent != null) { return(CheckHideFlags(t.parent)); } else { return(true); } }; foreach (var animator in go.GetComponentsInChildren <Animator>(true)) { if (animator == null || animator == vaw.animator) { continue; } if (!animator.gameObject.activeInHierarchy || !animator.enabled) { continue; } if (!CheckHideFlags(animator.transform)) { continue; } #region Clip AnimationClip clip = null; { var saveSettings = animator.GetComponent <VeryAnimationSaveSettings>(); if (saveSettings != null && saveSettings.lastSelectAnimationClip != null) { if (ArrayUtility.Contains(AnimationUtility.GetAnimationClips(animator.gameObject), saveSettings.lastSelectAnimationClip)) { clip = saveSettings.lastSelectAnimationClip; } } if (clip == null) { var ac = EditorCommon.GetAnimatorController(animator); if (ac != null && ac.layers.Length > 0) { var state = ac.layers[0].stateMachine.defaultState; if (state != null) { if (state.motion is UnityEditor.Animations.BlendTree) { Action <UnityEditor.Animations.BlendTree> FindBlendTree = null; FindBlendTree = (blendTree) => { if (blendTree.children == null) { return; } var children = blendTree.children; for (int i = 0; i < children.Length; i++) { if (children[i].motion is UnityEditor.Animations.BlendTree) { FindBlendTree(children[i].motion as UnityEditor.Animations.BlendTree); } else { clip = children[i].motion as AnimationClip; } if (clip != null) { break; } } blendTree.children = children; }; FindBlendTree(state.motion as UnityEditor.Animations.BlendTree); } else { clip = state.motion as AnimationClip; } } } if (clip != null) { var owc = animator.runtimeAnimatorController as AnimatorOverrideController; if (owc != null) { clip = owc[clip]; } } } } if (clip == null) { continue; } #endregion synchronizeObjects.Add(new SynchronizeObject(animator, clip)); } foreach (var animation in go.GetComponentsInChildren <Animation>(true)) { if (animation == null || animation == vaw.animation) { continue; } if (!animation.gameObject.activeInHierarchy || !animation.enabled) { continue; } if (!CheckHideFlags(animation.transform)) { continue; } #region Clip AnimationClip clip = null; { var saveSettings = animation.GetComponent <VeryAnimationSaveSettings>(); if (saveSettings != null && saveSettings.lastSelectAnimationClip != null) { if (ArrayUtility.Contains(AnimationUtility.GetAnimationClips(animation.gameObject), saveSettings.lastSelectAnimationClip)) { clip = saveSettings.lastSelectAnimationClip; } } if (clip == null) { clip = animation.clip; } } if (clip == null) { continue; } #endregion synchronizeObjects.Add(new SynchronizeObject(animation, clip)); } }; #if UNITY_2018_3_OR_NEWER if (PrefabStageUtility.GetCurrentPrefabStage() != null) { var scene = PrefabStageUtility.GetCurrentPrefabStage().scene; foreach (var go in scene.GetRootGameObjects()) { AddGameObject(go); } } else #endif { for (int i = 0; i < SceneManager.sceneCount; i++) { var scene = SceneManager.GetSceneAt(i); foreach (var go in scene.GetRootGameObjects()) { AddGameObject(go); } } } }