2D Toolkit Forum

2D Toolkit => Support => Topic started by: lofl on June 12, 2013, 03:38:56 am

Title: Can I redefine SpriteCollection at runtime?
Post by: lofl on June 12, 2013, 03:38:56 am
Hi,

I am going to emigrate from another sprite system.

That code is creating all sprite at runtime.

I want to create sprite with another rect of the texture used by created SpriteCollection.

But, when creating the first sprite, the information on the sprite created later is unknown.

Can I add new rect to created SpriteCollection?



Thnaks.
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: unikronsoftware on June 12, 2013, 10:20:46 am
You can to the sprite collection data object. Check tk2dRuntimeSpriteCollection.cs for how to create sprites from rects in a texture. You can use that to manipulate your runtime sprite collection in any way. Just remember - the sprite IDs should never change. So if you're adding one, increase the size of the array by all means, but when deleting the first out of the full array, don't shift everything up, just clear the first entry.
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: Tina on June 12, 2013, 09:32:27 pm
Hi,
I'm currently working in a 2d game in the newest version of the 2d toolkit and I would like to know how I create a sprite animator at runtime.  I’m developing a game where depending on a random number a different animation has to be displayed.
Currently I’m using this code:
public tk2dSpriteAnimator anim;
GameObject two = new GameObject ();
anim = two.AddComponent<tk2dSpriteAnimator>();
The problem with this code is that Unity shows this error:
Sprite not found attached to tk2dSpriteAnimator.
I will appreciate your help!
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: unikronsoftware on June 12, 2013, 10:38:39 pm
No need to post the same thing in 3 threads. Its been answered here:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1871.0.html
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: lofl on June 13, 2013, 07:07:10 am
Thanks for reply.

I set tk2dSpriteCollectionData.spriteDefinitions to new tk2dSpriteDefinition[] referring to tk2dRuntimeSpriteCollection.cs.

But NullReferenceException occurred.

I think it will go if spriteNameLookupDict can be set to null, but it is private...
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: unikronsoftware on June 13, 2013, 10:18:49 am
Well you do have the source :)
Just reset it, and you should be good to go. Or don't use names, just use spriteIds and you won't need the dict at all.
Title: Re: Can I redefine SpriteCollection at runtime?
Post by: lofl on June 14, 2013, 08:56:11 am
Thank you so much.
I'll try it.