Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

AnimatorControllerLayer.SetOverrideMotion isn't permanent.

Discussion in 'Animation' started by Sluggy, Apr 28, 2016.

  1. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    843
    I'm writing an editor utility that will scan a directory for some images and use that information to automatically build some animation clips and an animator controller and then fill the controller's states with the clips. For the most part it works ok but I can't find any way to programmatically set the clips used by synced layer states. I tried calling AnimatorControllerLayer.SetOverrideMotion() but it doesn't seem to stick. I can write the motion and then read it back but the asset itself seems to never change after I call AssetDatabase.SaveAssets(). I'm guessing that this only stores a temporary value that is never serialized. I am missing something here? Is there another way to accomplish this?

    Here is the relevant code
    Code (CSharp):
    1. if (layer.syncedLayerIndex == -1)
    2. {
    3.   //This layer doesn't have its own statemachine, use the base layer's fsm instead
    4.   foreach (var childState in controller.layers[layer.syncedLayerIndex].stateMachine.states)
    5.   {
    6.     if (string.Equals(childState.state.name, stateName, System.StringComparison.CurrentCultureIgnoreCase))
    7.     {
    8.       layer.SetOverrideMotion(childState.state, clip);
    9.       return;
    10.     }
    11.   }
    12. }
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    843
    Sigh. Doesn't it just figure. A whole day wasted trying figure it out and within an hour of asking I find the answer on my own.

    For the record, what I needed was AnimatorController.SetStateEffectiveMotion() using the layer index parameter overload.
     
    tecnorelvas likes this.
  3. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    Thank you. It was very useful.
     
  4. unity_pQirYwzPLiEXiw

    unity_pQirYwzPLiEXiw

    Joined:
    Nov 29, 2017
    Posts:
    2
    This bug is still not fixed today, thanks for the workaround.