Hello Guest

Author Topic: How to set SpriteCollection in runtime  (Read 3768 times)

grinay

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
How to set SpriteCollection in runtime
« on: June 04, 2013, 02:20:59 pm »
I create Game object

GameObject levelButton = new GameObject("LevelButton",new Type[]{typeof(tk2dSprite),typeof(tk2dButton)});

and i want set the exist sprite collection. Collection in folder Assets/Sprites/GameElements.prefab

How can i do it?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to set SpriteCollection in runtime
« Reply #1 on: June 04, 2013, 02:46:57 pm »
You should read up about resources folders in Unity before proceeding.

1. Put the sprite collection data object (i.e. Assets/Sprites/GameElements/GameELements Data/GameElements.prefab) into a resources folder.
2. Load it in using tk2dSpriteCollectionData data = Resources.Load("GameElements", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
3. Get the sprite from levelButton -
  tk2dSprite sprite = levelButton.GetComponent<tk2dSprite>();
  sprite.SetSprite( data, "nameofsprite" );

grinay

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to set SpriteCollection in runtime
« Reply #2 on: June 04, 2013, 02:59:14 pm »
Thank. It work.
Only i use SwitchCollectionAndSprite
level.GetComponent<tk2dSprite>().SwitchCollectionAndSprite(data,0);
« Last Edit: June 04, 2013, 03:01:28 pm by grinay »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to set SpriteCollection in runtime
« Reply #3 on: June 04, 2013, 03:04:19 pm »
Thats fine, though SwitchCollectionAndSprite has been renamed to SetSprite in tk2d 2.0. Something to keep in mind if you update.