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

Editor Script: Programmatic Creation of AnimatorOverrideControllers

Discussion in 'Scripting' started by GGeff, Apr 25, 2015.

  1. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    Using v4.6.4f1
    I am trying to do as the title suggests, creating AnimatorOverrideControllers with my editor script, but I'm having trouble figuring out how to reference the animation clips that I intend to be the overrides. Here is my current script:

    Code (CSharp):
    1.  
    2.     void OnPostprocessModel(GameObject g)
    3.     {
    4.         //Debug.Log("post " + g.name);
    5.  
    6.         //AnimationClip[] AnimClips = AnimationUtility.GetAnimationClips(go);function is broken currently
    7.         //if(AnimClips == null || AnimClips.Length <= 0)
    8.         //{
    9.         //    Debug.Log("No animation component");
    10.         //    return;
    11.         //}
    12.         //if(AnimClips.Length != 6)
    13.         //    Debug.Log("WARNING, AnimationClips Amount: " + AnimClips.Length);
    14.  
    15.         //List<AnimationClip> animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(g));
    16.         //if(animationClipList.Count == 0)
    17.         //{
    18.         //    AnimationClip[] objectList = UnityEngine.Object.FindObjectsOfType(typeof(AnimationClip)) as AnimationClip[];
    19.         //    animationClipList.AddRange(objectList);
    20.         //}
    21.         //if(animationClipList.Count == 0)
    22.         //{
    23.         //    Debug.Log("No animation clips");
    24.         //    return;
    25.         //}
    26.  
    27.         Animator anim = g.GetComponent<Animator>();
    28.         if(!anim)
    29.         {
    30.             //Debug.Log("No Animator");
    31.             return;
    32.         }
    33.         anim.applyRootMotion = false;
    34.  
    35.         AnimatorOverrideController AOC = (AnimatorOverrideController)AssetDatabase.LoadMainAssetAtPath("assets/animation/" + g.name + "AOC" + ".overrideController");
    36.         if(AOC)
    37.         {
    38.         }
    39.         else
    40.         {
    41.             int RetVal = EditorUtility.DisplayDialogComplex("Create AOC", Path.GetFileName(assetPath), "Auto AOC", "Skip", "Skip");
    42.             if(RetVal != 0)
    43.                 return;
    44.  
    45.             RuntimeAnimatorController UnitController = (RuntimeAnimatorController)AssetDatabase.LoadMainAssetAtPath("assets/animation/UnitController.controller");
    46.             if(UnitController == null)
    47.             {
    48.                 Debug.Log("No UnitController.controller ");
    49.                 return;
    50.             }
    51.  
    52.             AOC = new AnimatorOverrideController();
    53.             AOC.name = g.name + "AOC";
    54.             AOC.runtimeAnimatorController = UnitController;
    55.  
    56.             //Debug.Log("Num anim clips: " + animationClipList.Count);
    57.  
    58.             //for(int i = 0; i < animationClipList.Count; i++)
    59.             //{
    60.             //    if(animationClipList[i].name == "Idle")
    61.             //    {
    62.             //        AOC["Idle"] = animationClipList[i];
    63.             //        Debug.Log("did idle");
    64.             //    }
    65.             //    else if(animationClipList[i].name == "Walk")
    66.             //        AOC["Walk"] = animationClipList[i];
    67.             //    else if(animationClipList[i].name == "Attack")
    68.             //        AOC["Attack"] = animationClipList[i];
    69.             //    else if(animationClipList[i].name == "Die")
    70.             //        AOC["Die"] = animationClipList[i];
    71.             //    else if(animationClipList[i].name == "Fall")
    72.             //        AOC["Fall"] = animationClipList[i];
    73.             //    else if(animationClipList[i].name == "Recoil")
    74.             //        AOC["Recoil"] = animationClipList[i];
    75.             //}
    76.  
    77.             AssetDatabase.CreateAsset(AOC, "assets/animation/" + AOC.name + ".overrideController");
    78.             Debug.Log("created: " + "assets/animation/" + AOC.name + ".overrideController");
    79.         }
    80.  
    81.         anim.runtimeAnimatorController = AOC;
    82.     }    
    83.  
    84.  
    This script is nice in that it at least will create the AOC with a name matching the model as well as assigning that AOC to the Animator component, saving me quite a few steps, however, you can see my commented attempts to actually link the animationclips, all to no avail.

    Any help would be greatly appreciated.
     
  2. SignalZak

    SignalZak

    Joined:
    Nov 13, 2014
    Posts:
    12
    AnimatorOverrideController.clips returns a copy, so you modify that copy then reassign it back to AnimatorOverrideController.clips.