2D Toolkit Forum
2D Toolkit => Support => Topic started by: gak on June 24, 2013, 12:22:06 pm
-
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:
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
-
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.
-
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.
-
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?
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 (http://unikronsoftware.com/2dtoolkit/doc/2.10/advanced/scripting_static_sprite_batcher.html).
-
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).