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

How to dynamically create animation controllers in scripts

Discussion in 'Scripting' started by rmccabe82, Nov 26, 2014.

  1. rmccabe82

    rmccabe82

    Joined:
    Nov 26, 2014
    Posts:
    1
    Hi,

    I need to be able to assign animation to a selection of models at runtime.

    In the Unity IDE when I have a humanoid model fbx (we'll call this avatar.fbx) and a humanoid animation fbx file with no model, just the animation (we'll call this movement.fbx), I can quite easily get apply the movement to the avatar using an animation controller.

    However, I am using a Kinect to generate fbx files at runtime and drop them to a directory that Unity watches. I want to then apply the animation.fbx file that's been created to the avatar.fbx (as I would have done through the IDE with the animation controller) but I cant seem to find the code to do this.

    Has anyone dynamically imported animations while a game is running and applied them to a model before?

    For helpfulness, I've attached a document with the steps I take to do this through the Unity IDE. These are what I need to mimic in a script.

    Thanks for the pointers in advance!!
     

    Attached Files:

  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I looked into this a little over a year ago and it wasn't possible at that time.
     
  3. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    Does anyone know if Unity is planning to allow runtime creation of the Animation Controller in Unity 5? I'd like to be able to do this too. A lot of work is being done around Mecanim at the moment for Unity 5 so I'd love to see this exposed.
     
  4. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
  5. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
  6. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    That link isn't for runtime creation. That's editor time.
     
  7. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    I haven't try to build with that code yet, but I am pretty sure that calling that method will create what is required. Did you try it out yourself? Did it say that it doesn't compile if not in #if UNITY_EDITOR? It works perfectly in editor by the way.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's in the UnityEditor namespace which is excluded from builds so it's not a viable option for runtime controller creation.
     
    CWolf likes this.
  9. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    As @KelsoMRK mentioned, due to it being in the UnityEditor namespace it does not get included in a standalone build.
     
  10. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    Oh, I understand. I have many namespaces by default when creating `.cs` file so I didn't notice it being in UnityEditor. It's strange that they just don't expose it for some reason. I know that new versions of unity are going to have some additional functionality for animations. Also, there will be a new animator tool if you watch a keynote - Kinematica. Maybe it will expose this API.
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Menion-Leah likes this.
  12. PixelLifetime

    PixelLifetime

    Joined:
    Mar 30, 2017
    Posts:
    90
    Also, there will be a access to animation stream and ...
    - a few seconds later in the video after input system.
     
  13. CWolf

    CWolf

    Joined:
    Oct 24, 2011
    Posts:
    106
    The original thread discussion was create, not change.
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    Oh, right, didn't spot that this was a strange necro post. My bad.
     
  15. Pacosmico

    Pacosmico

    Joined:
    Jan 16, 2014
    Posts:
    14
    For example, I am need literally an empty Animator Controller (because Animator.RecordPlayback won't work if no Controller is assigned). Since I can't just "new RuntimeAnimatorController" I must create in the project one, and then assign a reference to it, wich is annoying because I don't want to be worrying that every single instance of the scripts wich need this empty controller has to have a pointer.
    I mean, I will end up doing some awful editor script for something so silly!

    So... feel ya bro/sis
     
    OfficialHermie and Sprarage like this.