Hello Guest

Author Topic: Create SpriteCollection and SpriteAnimation at Runtime  (Read 4224 times)

DarkMagicCK

  • Newbie
  • *
  • Posts: 4
    • View Profile
Create SpriteCollection and SpriteAnimation at Runtime
« on: January 01, 2013, 07:04:02 am »
Seems you did not see my apologize so I post again.

Hello, this is a great question troubling me these days.

Our boss needs game level DLC feature (using Asset Bundle), so I have no choice but to create all sprites at runtime. But then I'm got some prob.
I've worked out how to create SpriteCollection at Runtime, according to this post http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,935.msg4519.html#msg4519.

Note that my Resources or AssetBundle will include the SpriteCollection and SpriteAnimation which have already been done in editor.
I'm not asking how to create collection from png file or how to create animation using SpriteAnimationFrame.
So here are my questions:

  1. Resource.Load() will create an object instance, if I use Resource create the same SpriteCollection many times, will it take many spaces. That means, if my "collection1" has a texture, and I Resource.Load("collection1") for 2 times, will unity load 2 textures into space? Or I should need a hashtable to check if a SpriteCollection is created.

  2. I do not know if I can create SpriteAnimation using Resource.Load() because the SpriteAnimationFrame is based on different SpriteCollection. So if I want to create SpriteAnimation , do I need to Resource.Load() the Collection first? Or it's just impossible?

I really suggest you to add something like tk2dGlobal, which do something like:
Code: [Select]
bool tk2dGlobal.AddSpriteCollection("name in resource folder");
bool tk2dGlobal.AddSpriteAnimation("name in resource folder");//which will automatically add the needed collections
SpriteAnimationClip tk2dGlobal.GetAnimationClipInAnimationLibrary("clip name", "anim lib name");
SpriteCollection tk2dGlobal.GetSpriteCollection("name in resource folder");
GameObject tk2dGlobal.CreateSprite(clip);

Please help me solve this problem.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create SpriteCollection and SpriteAnimation at Runtime
« Reply #1 on: January 01, 2013, 07:55:27 am »
I don't understand your question. Resources.Load will load from within the project itself, not an asset bundle.

If you want to use an asset bundle, it is pretty straightforward. Create your asset bundle, making the SpriteCollection/SpriteAnimation the main object. All dependencies must exist in there though, otherwise you'll run into issues. Once you load the asset bundle, you can simply grab assetBundle.mainAsset, cast it to SpriteCollection/SpriteAnimation and then use it directly.

To answer your question...
1. Resources.Load loads an asset, and an asset only exists once. Calling repeatedly should just return the same reference.
2. Yes you can. Unity will load dependencies. But this isn't what you want, probably. Look at asset bundle explanation above.

DarkMagicCK

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Create SpriteCollection and SpriteAnimation at Runtime
« Reply #2 on: January 01, 2013, 03:19:45 pm »
Thanks for replying. I'll make an asset bundle and have a try.