2D Toolkit Forum
2D Toolkit => Support => Topic started by: dustinbahr 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?
-
I ended up storing only the Sprite Animation prefab in the Resources directory and then using the code below.
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)
-
That will work fine. Resources.Load is a preferable way of doing this, and yes you only need the sprite animation in resources.
-
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.
tk2dSpriteCollectionData bgCollection = (tk2dSpriteCollectionData) Resources.Load("Bg_" + colorName);
Debug.Log(Resources.Load("Bg_" + colorName)); // returns null
This works fine:
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?
-
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 :)