Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Assigning overrideClip of AnimatoOverrideController.clips at Design Time

Discussion in 'Immediate Mode GUI (IMGUI)' started by v2-Ton-Studios, Apr 17, 2016.

  1. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    I'm trying to write an extension that "duplicates" an AnimatorOverrideController. Here's what I'm after.

    Given a specified AnimatorOverrideController.

    1, Go through all the AnimationClips in the override controller and create new empty clips e.g. original clips are Goblin_Idle, Goblin_Attack, new clips are Bat_Idle, Bat_Attack.

    2, For each of the clips in AnimationClips replace the current override with the new override, so Goblin_Idle --> Bat_Idle, and so on.

    I can accomplish (1), but setting clips.overrideClip doesn't work. Is overrideClip setable?

    I would really like to automate this as I have 50-60 different animations per character/enemy. Duplicating them by hand and then dragging them into the override slots also by hand sucks :).

    Big TIA!

    Code (csharp):
    1.  
    2. AnimationClipPair[] clips = _animatorToDup.clips;
    3.  
    4. string fullPath = AssetDatabase.GetAssetPath (_animatorToDup);
    5. string path = fullPath.Substring (0, fullPath.LastIndexOf ("/") + 1);
    6.  
    7. for (int i = 0; i < clips.Length; i++)
    8. {
    9.     AnimationClip clip = new AnimationClip();
    10.  
    11.     string org = clips[i].originalClip.name;
    12.     string newName = _prefix + org.Substring (org.IndexOf ("_") + 1);
    13.  
    14.     AssetDatabase.CreateAsset (clip, path + newName + ".anim");
    15.  
    16.     // this doesn't work
    17.     clips[i].overrideClip = clip;
    18.  
    19.     // neither does this
    20.     clips[i].overrideClip = (AnimationClip)AssetDatabase.LoadAssetAtPath(path + newName + ".anim", typeof(AnimationClip));
    21. }
    22.  
     
    Last edited: Apr 17, 2016
  2. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    Is this not the right forum for the above question? If not, which is? Thanks.
     
  3. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    Same problem here.
    I want to automatically create override controllers from a script, but setting the override clip just does nothing.
    It doesn't apply the changes somehow... whats going on ?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You need to set back the array into the overridecontroller

    Code (CSharp):
    1.  
    2. AnimationClipPair[] clips = _animatorToDup.clips;
    3.  
    4. // setup your override
    5. clips[i].overrideClip = clip;
    6.  
    7. _animatorToDup.clips = clips;
    8.  
    This is due to a limitation for property as array and how unity map them to c++ code.
    This is mainly why in 5.6 we did refactor this API to remove exactly this confusion.
    https://docs.unity3d.com/560/Documentation/ScriptReference/AnimatorOverrideController-clips.html is now obsolete.

    you need to use the following function which are allocation free
    https://docs.unity3d.com/560/Docume.../AnimatorOverrideController.GetOverrides.html
    and
    https://docs.unity3d.com/560/Docume...nimatorOverrideController.ApplyOverrides.html
     
  5. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    That did the trick, awesome!

    Good to know that in 5.6 there will be methods for this (and the deeper problem is prevented).

    Thanks!
     
  6. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    I'd totally given up on this every working. Thanks for reply a nice mid-week present :D.
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    On a side note, next time try to post animation issue into the animation forum.
    We can't look at all threads in all forums, there is too many new threads each day
     
  8. v2-Ton-Studios

    v2-Ton-Studios

    Joined:
    Jul 18, 2012
    Posts:
    238
    Interesting... I didn't see this as an animation issue, as my goal was to write an editor extension rather than say work with an animation directly.