Search Unity

[SOLVED] Load animations from Asset Bundles

Discussion in 'Scripting' started by Ony, Nov 18, 2009.

  1. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    I've made an asset bundle containing some animations for a character (I believe I got it built correctly but not totally sure yet).

    Can someone give me a quick code example that will load all the animation clips from an asset bundle into my existing character during run-time?

    If it's not possible to load all of them without knowing what they're called, then code showing how to load one named clip from the bunch would be fine as well.

    I've been trying all kinds of things and for some reason this is failing me. The documentation is a little sparse for this so if you can at all provide some sample code I would really appreciate it.

    Thanks in advance.
     
    Crossway likes this.
  2. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,977
    Got it figured out:

    This technique allows you to have a whole bunch of animations contained in asset bundles, but not have to have them all in your character at once when the game launches. You can chose which animation bundles to load as you need them.

    First, make the asset bundle out of a prefab that has your character containing all the animations you want in the bundle.

    Next, use the code below to load those animations into the character during the game.

    Code (csharp):
    1.  
    2. var theCharacter : GameObject;
    3.  
    4. function LoadAnimations()
    5. {
    6.     var www = new WWW ("http://myserver/myBundle.unity3d");
    7.     yield www;
    8.  
    9.     var Animations : Object[] = www.assetBundle.LoadAll(Animation);
    10.  
    11.     var n = 0;
    12.     var loadThis : String;
    13.  
    14.     for(var animFile in Animations)
    15.     {
    16.         loadThis = Animations[n].name.ToString();
    17.         theCharacter.animation.AddClip(Animations[n].animation.clip, loadThis);
    18.  
    19.         n ++;
    20.     }
    21. }
     
  3. sarasa89

    sarasa89

    Joined:
    Jan 1, 2013
    Posts:
    5
    thanks alot :)
    this really helped me
     
    Ony likes this.
  4. Regretful123

    Regretful123

    Joined:
    Nov 5, 2014
    Posts:
    18
    Have this code changed with new features Unity offers? Or is this really the only solution to load custom animation from asset bundle? I need to find a way to use custom animation in the animator...
     
    Crossway likes this.
  5. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Hi did you solve this?