Hello Guest

Author Topic: Basic question(s) on creating sprites during runtime  (Read 6655 times)

traemyn

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Basic question(s) on creating sprites during runtime
« on: December 11, 2012, 05:10:39 pm »
I am new to Unity and 2D Toolkit so I'll outline what I am doing below in case someone wants to point out any general Unity mistakes while answering my toolkit questions.  ;)

I am working on a card game and I have created multiple SpriteCollections and loaded the card textures into them (~60 cards per collection; maxing out the atlas size of each). Each 'card' has a unique name (unique to all SpriteCollections).

To create the sprite gameobjects during runtime, I envision the following occurring:

I have a 'finder' function that I will pass the name of the card. Inside this function it will loop through all SpriteCollections for that name and then create the sprite (gameobject) with the appropriate info, from the appropriate SpriteCollection.

Questions:
1. Is there a faster/better way to do the 'finder' function part? Am I missing some basic functionality that already exists in the SpriteCollections/2D Toolkit?

2. While trying to implement this finder function, I ran into a problem that my SpriteCollections are not showing up in the intellisense and don't seem to 'exist' so that the code can reference them. I tried GameObject.Find but that is not working. How can I reference those SpriteCollections in the code?

Thanks!

EDIT: I think one of the FAQ posts answers some of this actually.
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,24.0.html

"If you need to generate large numbers of sprites by name, simply create one prefab for your sprite collection, instantiate it, and use GetSpriteIdByName() and switch the sprite. This way, you can pool one sprite type per Sprite Collection, and instantiate them the way you need it."

I still need to know what prefab to create (since I have multiple sprite collections). Should I just create a lookup table that has a <cardname><prefab name exists in> instead of trying to search through all existing SpriteCollections?
« Last Edit: December 11, 2012, 06:47:22 pm by traemyn »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Basic question(s) on creating sprites during runtime
« Reply #1 on: December 12, 2012, 07:53:19 am »
If you looped through all spritecollections, then they will all be loaded at runtime. I don't think this is what you'd want. This functionality doesn't exist out of the box but is easy to implement.

Move the sprite collection data object (and just this object) into a subdirectory called Resources. You can now load this sprite collection by doing Resources.Load("name_with_no_ext", typeof(tk2dSpriteCollectionData));

Now that you can load at runtime, you'll need to create your index. Basically, a dictionary of sprite name -> sprite collection name (must be unique) and spriteId. A minor complication here is that Unity will not serialize dictionaries. For the index, create a scriptable object class (check the Unity docs on this) and then save it somewhere. With an editor script, add a button to select a sprite collection and add it to the index.

Once you have this you can then load the index at runtime and use it to load whatever you need subsequently.

If you can factor in the sprite collection name into the card name, then you won't need any of this index stuff. Example: if your card names are redcollection/testredcard1, then the sprite collection could be "redcollection", and the card can be resolved without any indexing whatsoever... I obviously don't know your needs and if this is possible, but its something to keep in mind.


Tina

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Basic question(s) on creating sprites during runtime
« Reply #2 on: June 12, 2013, 09:31:30 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 show this error:
Sprite not found attached to tk2dSpriteAnimator.

I will appreciate your help!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Basic question(s) on creating sprites during runtime
« Reply #3 on: June 12, 2013, 10:39:34 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