Hello Guest

Author Topic: Create and Edit tk2dSprite at Runtime!  (Read 8117 times)

fezon aimed

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Create and Edit tk2dSprite at Runtime!
« 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! :)
« Last Edit: January 31, 2014, 07:03:03 am by fezon aimed »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #1 on: January 31, 2014, 11:23:25 am »
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

fezon aimed

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #2 on: February 03, 2014, 10:34:41 am »
Thanks for the help, Can you provide some C# script examples for tk2dSprite creation from resourses?

Fezon...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #3 on: February 03, 2014, 10:53:44 am »
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.

fezon aimed

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #4 on: February 03, 2014, 11:58:49 am »
Thankx for help! Done....

fezon aimed

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #5 on: February 04, 2014, 10:19:02 am »
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;
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #6 on: February 04, 2014, 10:21:06 am »
you need to create 2 game objects and sprites on both. you can only have 1 sprite per game object.

fezon aimed

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Create and Edit tk2dSprite at Runtime!
« Reply #7 on: February 04, 2014, 10:36:55 am »
GOt it.. And Thanx Again  ;)