Hello Guest

Author Topic: Sprite Collection & Animation - Loadable Asset  (Read 5858 times)

dustinbahr

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 76
    • View Profile
Sprite Collection & Animation - Loadable Asset
« on: October 15, 2014, 03:20:04 am »
Hello,

We are wanting to load sprite animations at runtime.

I tested putting the sprite collection data prefab and the sprite animation prefab in the resources directory and loading those to use them. This worked great.

Then I noticed the Loadable Asset checkbox in sprite collection settings.

The manual says:
"When ticked, the asset will be loadable using tk2dSystem.LoadResourceByName<tk2dSpriteCollectionData>("name");"
"It is a great way to load assets at runtime without resorting to moving objects into resources folders."

However, when searching the forums, I see you saying that it is not officially supported.

Which method should we use?

dustinbahr

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 76
    • View Profile
Re: Sprite Collection & Animation - Loadable Asset
« Reply #1 on: October 15, 2014, 09:33:35 pm »
I ended up storing only the Sprite Animation prefab in the Resources directory and then using the code below.

Code: [Select]
public void LoadAndPlayAnimation(string animationName)
{
animator.Stop();
string animationPath = resourcesDirectory + animationName + "_Animation";
loadedAnimationLibrary = Resources.Load<tk2dSpriteAnimation>(animationPath);
animator.Library = loadedAnimationLibrary;
animator.Play(animationName);
}

This appears to have no problem. Is this a good way of approaching it.

Lastly, do I still need the Loadable Resource checkbox active for this to work properly?

(in case it matters, this is for mobile devices)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Collection & Animation - Loadable Asset
« Reply #2 on: October 15, 2014, 10:53:13 pm »
That will work fine. Resources.Load is a preferable way of doing this, and yes you only need the sprite animation in resources.

Finnegan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Sprite Collection & Animation - Loadable Asset
« Reply #3 on: March 17, 2015, 08:21:09 pm »
That will work fine. Resources.Load is a preferable way of doing this, and yes you only need the sprite animation in resources.

For some reason, Resources.Load is not working for "Loadable asset" collections.
Code: [Select]
tk2dSpriteCollectionData bgCollection = (tk2dSpriteCollectionData) Resources.Load("Bg_" + colorName);
Debug.Log(Resources.Load("Bg_" + colorName)); // returns null

This works fine:
Code: [Select]
tk2dSpriteCollectionData bgCollection = tk2dSystem.LoadResourceByName<tk2dSpriteCollectionData>("Bg_" + colorName);
So LoadResourceByName now is a preferable way for me because the other one is not available. :) Is it dangerous to use?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite Collection & Animation - Loadable Asset
« Reply #4 on: March 21, 2015, 11:24:12 pm »
Resources.Load won't work with loadable collections as it doesn't put the sprite collection into the resources folder. If you want to use resources, put it into a resources folder.
LoadResourcesByName should work, but isn't officially supported. If it works for you, use it :)