2D Toolkit Forum
2D Toolkit => Support => Topic started by: fezon aimed on January 31, 2014, 07:00:28 am
-
Hi, I am pretty new to Unity and tk2d. I have a sprite collection named "flavorCollection" of some sort of juice flavors. How I can create a tk2dsprite at runtime from flavorCollection? If I have multiple tk2dSprites in an empty game object how i can access one of them programmatically to change image from flavorCollection if i have my script on empty game object?
Thanx in advance for help! :)
-
You can use resources.load - put the sprite collection data into a folder called resources, then use Resources.Load to load it. Its pretty much how you'd load anything dynamically in Unity.
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
-
Thanks for the help, Can you provide some C# script examples for tk2dSprite creation from resourses?
Fezon...
-
Its the same as loading anything, just Resources.Load("name of collection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
After that you just create a sprite like you normally do - check the runtime sprite collections sample for details on that part.
-
Thankx for help! Done....
-
Hi UnikronSoftware,
I have implemented sprite code as you told. It worked fine for me..But now i want to add more then one sprites with same collection. when i tried to add more then one sprite as shown below in code its just shows me jar_2. It creates both sprite in Background object. I can see in inspector view jar_1 and jar_2 are assigned to sprite and sprite_2 respectively but sorting order for both is 7 and in game view and scene view i can just see jar_2. I think jar_2 is overriding jar_1. Please help to get expected results...Thanx in advance...
//Implemented Code
GameObject flavoursprite;
void Start () {
flavoursprite = GameObject.Find("Background");
tk2dSprite sprite = flavoursprite.AddComponent<tk2dSprite>();
sprite.Collection = Resources.Load("Pouring/DialogCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
sprite.spriteId = sprite.GetSpriteIdByName("jar_1");
sprite.SortingOrder=6;
tk2dSprite sprite_2 = flavoursprite.AddComponent<tk2dSprite>();
sprite_2.Collection = Resources.Load("Pouring/DialogCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
sprite_2.spriteId = sprite.GetSpriteIdByName("jar_2");
sprite_2.SortingOrder=7;
}
-
you need to create 2 game objects and sprites on both. you can only have 1 sprite per game object.
-
GOt it.. And Thanx Again ;)