Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Load animation from Resource folder?

Discussion in 'Scripting' started by Ony, Sep 10, 2009.

  1. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,960
    I've got a character that will have a bunch of animations, and I want to load the ones I need for a given level into the character dynamically at run time using the Resource folder.

    For this example I am trying to get the "blink" animation into the character. I've read through everything I can find on this but I can't figure out how to make it work.

    I have a javascript script attached to the character which contains the following:

    Code (csharp):
    1.  
    2. var blinkClip : AnimationClip;
    3.  
    4. function Start ()
    5. {
    6.  
    7.     // blink...
    8.  
    9.     blinkClip = Resources.Load("Animations/Facial/Female/f1_facial/blink");
    10.  
    11.     animation.AddClip(blinkClip, "blink");
    12.  
    13.     blink = animation["blink"];
    14.  
    15.     blink.layer = 10;
    16.     blink.wrapMode = WrapMode.Once;
    17.     blink.enabled = true;
    18.     blink.weight = 1.0;
    19.    
    20. }
    21.  
    This doesn't work. I keep getting a "Null Reference Exception" but I have no idea what it means beyond that.

    Any help is much appreciated.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Resources.Load does not give you a clip, it gives you an object that holds data normally. You need to instantiate those data to normally get an actual unity object
     
  3. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,960
    Ok excellent, thank you, that helped. I managed to get it so the animations dynamically load into the empty animation list of the prefab when I run the game.

    For some reason I can't get any of them besides the default one to actually play when I do it this way, but I'm working on that. If I put the animations into the list manually they work fine but when I load them in from the resources they won't play, even though when the dynamic list propagates upon start it looks exactly the same as a manually filled list. I'm sure I'm just missing something obvious.

    Thanks again for the push in the right direction.
     
  4. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,960
    I ended up going in a different direction with this, but now I'm back to wondering how to do it.

    So...

    Say I have a bunch of animated FBX files in my Resources folder, and I want to dynamically grab the animations from some of those files (as needed) and load them into my skeletal character during run-time. How would I do this?

    I don't want to drag and drop them as we are going to add new animations periodically so I just want to be able to load them in as needed.

    Can someone walk me through what I need to do to accomplish this?

    Thanks!
     
  5. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    Just happened upon this when researching Resources.Load memory usage for non-GUITexture assets. Here's a snippet of code that does exactly what you're talking about:

    Code (csharp):
    1.  
    2.         var mdl : GameObject = Resources.Load("Animations/"+ animationFolder+"/" + aName);
    3.         if (!mdl) {
    4.             Debug.LogError("Missing animation asset: Animations/" + animationFolder+"/"+aName + " could not be found.");
    5.         }  else {
    6.             var aClip = mdl.animation.clip;
    7.             charAnimation.AddClip(aClip, aName);
    8.             Debug.Log(charAnimation[aName].name + " loaded from resource file " + animationFolder + "/" + aName + ". Length check: " + charAnimation[aName].length);   
    9.         }
    10.  
    charAnimation is a reference to the Animation object. Other than that, this should be pretty self-explanatory. What you should also test first is that animation-only FBX data has the exact same bone structure as the original skinned mesh. The way I often do this is to create an empty scene, instantiate the skinned mesh, then manually attach an animation clip from one of the animation-only FBX files in the Resources folder via the Inspector. Then just start the scene and see if it plays. If it doesn't, then chances are your bone structure doesn't match.
     
    TravisEpic likes this.
  6. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,960
    Thanks for the code, getluky.

    I forgot about this thread but I ended up putting everything into asset bundles instead of using the Resources folder and it works better than it would have the original way.

    I'm sure someone will find the code useful though so it's good to have here in this thread in case someone else comes across it like you did.
     
    matasoy likes this.
  7. nonehill

    nonehill

    Joined:
    Oct 29, 2014
    Posts:
    7
  8. GeckoTrader

    GeckoTrader

    Joined:
    Nov 24, 2016
    Posts:
    8
    Thanks for the awesome example!
     
  9. matasoy

    matasoy

    Joined:
    Jun 19, 2019
    Posts:
    5
    As @Ony said, Resource folder is a must.

    This is how to load an anim file.
    ResourceRequest request = Resources.LoadAsync("animation_file_path_and_name_inside_the_Resource_folder");
    AnimationClip animClip = request.asset as AnimationClip;
     
  10. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,960
    Wow this is an OLD thread, haha! Pretty sure I was using Unity 3 back when I wrote the first post in this thread, but things have obviously changed drastically since then. :) I haven't really used the Resources folder (or Unity/JavaScript) for years now, and if I'm not mistaken, I believe Unity is recommending to phase the Resources folder out altogether.
     
unityunity