Search Unity

How to Find Animator, backwards from a "Selection" State in the Animator window.

Discussion in 'Animation' started by JonnyHilly, May 5, 2018.

  1. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    Wring an Editor script...
    In Animator window... user has an Animator box selected, (Square box)
    But I need the animator that it is connected to...

    I can get the selected Animator state like this.... but How do I get the Parent Animator or AnimatorController please ?
    var states = Selection.GetFiltered<AnimatorState>(SelectionMode.Editable);

    Thanks in advance
    Jon
     
  2. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    749
    Some useful notes... hopefully...

    this might be useful to anyone fiddling around trying to modify Animators, states and transitions from within an Editor script...
    At first you'll end up with alot of persistence errors ( things that are not saved to the project, and are only transient)

    Need to use Animator.runTimeAnimatorControllor, then from there get the Asset from AssetDatabase, and you can edit that version of the asset, and will save. (you'll also need to mark things dirty, and create Assets for new items)

    UnityEditor.Animations.AnimatorController AnimContr = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(MyAnimator.runtimeAnimatorController));

    Note adding transitions is easily done with AddTransition... but all transitions are 'outgoing' from a state, there is no list of incoming transitions.... so its tricky to delete incoming transitions... you'll have to search the hierarchy for the transition you want. (that points to the targeted state)

    Also you probably have to mark the AnimController dirty after any update, or it won't save... You may also have to mark some (but not all) sub states or objects as dirty when changing them. Keep it in mind.

    Note:- adding to the Animator, must be done by creating or adding assets using AssetManager, otherwise again, they won't save, or you'll get an Animator in a broken state that spews errors at load time. (for example with transitions but without the state it is transitioning to)

    Also note... adding some elements... you cant add them or link to other states, unless that other state is already persistent, so you have to create the hierarchy parts in the correct order. e.g 1) state and assets 2) transitions 3) add any behaviors
     
    Malbers and nirvanajie like this.