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

Blend Tree code example

Discussion in 'Animation' started by devotionsolutions, Sep 19, 2016.

  1. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Hello everyone,
    I've trying to find some examples of working with Blend Trees by code, without luck.
    Specifically, I'm trying to add some animation clips dynamically to a blend tree and then use some parameter in order to tweak the blend tree. Could you guys provide with an example and/or post some code?

    Thanks in advance,
     
    nonopblic likes this.
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Taken directly from our Runtime test,
    It does create a controller,
    add a parameter called BlendEvent
    Create a blend tree and setup two clip

    By default blend tree a created as Simple1D

    Code (CSharp):
    1.  
    2. var runClip = (AnimationClip)AssetDatabase.LoadAssetAtPath("Assets/Mecanim/run.fbx", typeof(AnimationClip));
    3. var strafeClip = (AnimationClip)AssetDatabase.LoadAssetAtPath("Assets/Mecanim/strafe.fbx", typeof(AnimationClip));
    4. var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/Mecanim/CanBlendToState.controller");
    5.  controller.AddParameter("BlendEvent", AnimatorControllerParameterType.Float);
    6.  
    7. UnityEditor.Animations.BlendTree blendTree;
    8. controller.CreateBlendTreeInController("BlendState", out blendTree, 0);
    9. blendTree.name = "Blend Tree";
    10. blendTree.blendParameter = "BlendEvent";
    11. blendTree.AddChild(runClip);
    12. blendTree.AddChild(strafeClip);
    13.  
     
  3. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Thanks so much for your response.
    And how can I add an exit transition to the blendTree from there?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    devotionsolutions likes this.
  5. Noixelfer

    Noixelfer

    Joined:
    Aug 2, 2018
    Posts:
    1
    I know is an old topic but does this works in the build too? or just in the Editor mode
     
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    it does only work in the editor, you cannot modify a controller at runtime
     
  7. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    My Animancer plugin would let you do this dynamically at runtime as shown in this image: .
     
  8. matasoy

    matasoy

    Joined:
    Jun 19, 2019
    Posts:
    5
    U did use unityEditor, it is not possble to use this code on runtime such as webgl or mobile publish etc.
     
  9. staotao

    staotao

    Joined:
    May 18, 2018
    Posts:
    1
    It's very helpful. but how to add a blend tree in a sub-state machine in code?