Hello Guest

Author Topic: Getting a tk2dSpriteCollectionData component from a known prefab.  (Read 6210 times)

gak

  • Newbie
  • *
  • Posts: 2
    • View Profile
Trying to get the static sprite batcher going via this example: http://www.unikronsoftware.com/2dtoolkit/docs/2.00/advanced/scripting_static_sprite_batcher.html

Unfortunately I'm not having much like getting the Sprite Collection.

I have one sprite collection in my project, under assets, called "Sprites", and I tried to get the component via this bit of code:

Code: [Select]
GameObject sprites = (GameObject)Resources.Load("Sprites");
tk2dSpriteCollectionData spriteCollection = sprites.GetComponent<tk2dSpriteCollectionData>();

This causes an error saying: "NullReferenceException: Object reference not set to an instance of an object", which makes me believe that getting the Sprites prefab via Resources.Load might not be the right way of accessing the sprite collection.

How would I do this? What is the usual way of accessing your sprite collection(s) via code?

Thanks, Gerald

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting a tk2dSpriteCollectionData component from a known prefab.
« Reply #1 on: June 24, 2013, 12:34:36 pm »
Do you have the sprite collection data object in a folder called resources?

Eg. with DemoSpriteCollection that is distributed with tk2d, do this:
demospritecollection/DemoSpriteCollection (tk2dSpriteCollection)
demospritecollection/SpriteCollection_Data/data (tk2dSpriteCollectionData)
needs to be changed like so:
demospritecollection/SpriteCollectino_Data/resources/data (also, rename this to something unique, please).

Now you can load data using the code you're using. If it isn't in resources, you won't be able to load it.

gak

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Getting a tk2dSpriteCollectionData component from a known prefab.
« Reply #2 on: June 26, 2013, 11:11:11 am »
I did a totally newbie thing and forgot that Resorce.load() only looks in Resources folders. Thanks for pointing that out.

All I had to do is move the generated prefab (containing the tk2dSpriteCollectionData component) into the Resources folder. When I commit any changes (via the tk2dSpriteCollection editor), it appears to update the moved prefab.

Finnegan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Getting a tk2dSpriteCollectionData component from a known prefab.
« Reply #3 on: August 19, 2013, 03:58:24 pm »
So, I'm moving all my batch-related collections to a "Resources" folder inside "scName Data" folders and loading them later. Is it a proper way to get a sprite collection before creating a batch from its sprites?

Code: [Select]
GameObject go = Resources.Load("my_sc_data_inside_resources_folder") as GameObject;
tk2dSpriteCollectionData spriteCollectionData = go.GetComponent<tk2dSpriteCollectionData>();
And then I use code from the documentation.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Getting a tk2dSpriteCollectionData component from a known prefab.
« Reply #4 on: August 19, 2013, 09:23:39 pm »
How you get it is entirely dependent on how your game is set up. If you know you're going to use these sprite collections, you could also store a direct reference to it (public tk2dSpriteCollectionData blablabla and drag a reference in there).