Hello Guest

Author Topic: Creating a sprite from code question  (Read 5546 times)

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Creating a sprite from code question
« on: September 18, 2013, 04:24:38 pm »
Hi,

I'm looking at ways that I can dynamically create a bunch of sprites when my game loads.  Ideally I want to be able to load a JSON or XML file from the network with a bunch of sprite configs that get dynamically instantiated into my game scene when it loads.

I've read your FAQ (http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,24.msg24.html#msg24) that mentions "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." 

This makes sense and currently seems to be the only way to handle doing what I want.  However, I'm curious if there is a way to create only 1 prefab with 1 sprite collection set (instead of having 1 prefab for every possible sprite collection) and then I can change the sprite collection using SetSprite().  To do this i would need a list of all the available sprite collections as accessible tk2dSpriteCollectionData objects.  I can not currently find a way to determine what all the available sprite collections are.

tk2dSystem seems to have a possible variable "allResourceEntries" that may be useful, but this is not accessible and I'm not sure if it is even useful for my needs.  Is there any way you can think of that would provide me with a static list of all the sprite collections?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a sprite from code question
« Reply #1 on: September 18, 2013, 11:14:31 pm »
You don't HAVE to create prefabs and change collection later, but thats one way of doing it. The other way is to store references to the sprite collection data objects, and then use that to create sprites. (AddComponent<tk2dSprite>().SetSprite( collection, "spriteName");

How you get the sprite collection data depends very much on how you want to do it. Resources.Load will let you load sprite collections / prefabs contained in "Resources" folders. Or you could also use asset bundles (Pro only), or even texture packer import (check sample #13). There isn't a global index, as it depends very much on how you want to load these assets - if you're using Resources.Load, you could simply have a text file asset as the index somewhere that you load in, or even a C# / JS file with a hard coded / automatically generated array.

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Re: Creating a sprite from code question
« Reply #2 on: September 18, 2013, 11:17:07 pm »
Interesting. I will try investigating the Resources.Load method you suggest.  Thanks for the info!