Last active
August 7, 2018 13:53
-
-
Save BlueRyth/8aa27eb7f5725263dcee to your computer and use it in GitHub Desktop.
Unity3D: Sync the Animation window to an Animator without requiring hierarchy selection (4.6.x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditorInternal; | |
/// <summary> | |
/// Class to synchronize the view of the Animator window without requiring hierarchy selection | |
/// </summary> | |
class SyncAnimationControllerTool | |
{ | |
/// <summary> | |
/// Synchronizes the view of the Animator window with an Animator from a GameObject, without | |
/// requiring selection in the hierarchy, or switching out of the Game view. | |
/// </summary> | |
/// <param name="go">Parent game object of Animator</param> | |
static public void Sync(GameObject go) | |
{ | |
// Get the anim control tool type, then the window | |
if (animCtrlToolType == null) | |
{ | |
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
Assembly editorGraphAssembly = Array.Find<Assembly>(assemblies, a => a.GetName().Name == "UnityEditor.Graphs"); | |
Module editorGraphModule = editorGraphAssembly.GetModule("UnityEditor.Graphs.dll"); | |
Type[] types = editorGraphModule.GetTypes(); | |
animCtrlToolType = Array.Find<Type>(types, t => t.FullName == "UnityEditor.Graphs.AnimatorControllerTool"); | |
animatorController = animCtrlToolType.GetProperty("animatorController"); | |
resetLayerExpansionArray = animCtrlToolType.GetMethod("ResetLayerExpansionArray", BindingFlags.NonPublic | BindingFlags.Instance); | |
previewObject = animCtrlToolType.GetField("m_PreviewObject", BindingFlags.NonPublic | BindingFlags.Instance); | |
} | |
// Don't open a new window if it's not already open | |
UnityEngine.Object[] windows = Resources.FindObjectsOfTypeAll(animCtrlToolType); | |
if (windows.Length > 0) | |
{ | |
// Set active AnimatorController | |
UnityEngine.Object animCtrlWnd = windows[0]; | |
Animator animator = go.GetComponentInChildren<Animator>(); | |
AnimatorController animCtrl = AnimatorController.GetEffectiveAnimatorController(animator); | |
object currentCtrl = pi.GetValue(animCtrlWnd, null); | |
if (currentCtrl != animCtrl) | |
{ | |
previewObject.SetValue(animCtrlWnd, animator); | |
animatorController.SetValue(animCtrlWnd, animCtrl, null); | |
resetLayerExpansionArray.Invoke(animCtrlWnd, null); | |
} | |
} | |
} | |
static Type animCtrlToolType = null; | |
static PropertyInfo animatorController = null; | |
static MethodInfo resetLayerExpansionArray = null; | |
static FieldInfo previewObject = null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment