Search Unity

The question of scripts 2D ToolKit.

Discussion in 'Scripting' started by Alien3D, Jun 22, 2012.

  1. Alien3D

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    I'm just not long ago began to study 2D ToolKit and I can not deal with switching sprites: example:
    I need that when you click on a button, the sprite is changed. How to do it, tell me please!

    P. S. I apologize for such language, translating the program "Dicter" :)
     
    Last edited: Jun 22, 2012
  2. Creslin321

    Creslin321

    Joined:
    Apr 26, 2012
    Posts:
    53
    From my experience, tk2d works through its script components, so that's what you want to do in your code.

    I don't have a detailed example, but if you wanted to change the sprite of a gameObject, you would do it through the tk2dSprite component...for example...

    Code (csharp):
    1.  
    2. //PSEUDO-CODE
    3. tk2dSprite mySprite = gameObject.GetComponent<tk2dSprite>();
    4.  
    5. //Use intellisense or something to figure out how to change the sprite with mySprite here.
    6.  
    7.  
     
  3. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    You're probably looking for SwitchCollectionAndSprite. Here is a sample of code I use to mirror an animation (I place this on a normal tk2dSprite and it constantly updates itself to whatever frame an animation is using).

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(tk2dSprite))]
    6. public class tk2dAnimationMirror : MonoBehaviour {
    7.     [SerializeField] tk2dAnimatedSprite _target;
    8.    
    9.     tk2dSprite _sprite;
    10.    
    11.     void Awake () {
    12.         _sprite = GetComponent<tk2dSprite>();
    13.     }
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         if (_target == null){
    18.             Debug.LogError("A target is required for an animation mirror!");
    19.             Destroy (this);
    20.         }
    21.         if (_target == _sprite){
    22.             Debug.LogError("An animation cannot mirror itself, please select a different target!");
    23.             Destroy (this);
    24.         }
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void LateUpdate () {
    29.         _sprite.SwitchCollectionAndSprite(_target.collection, _target.spriteId);
    30.        
    31.     }
    32. }
    33.  
     
  4. Alien3D

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    A little wrong. I have a collection of sprites, there are three pictures, how do I change them when you press the button?

    Guys, add me in Skype!
    Skype: Alien3DModeller
     
  5. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Just call SwitchCollectionAndSprite but pass the current collection - the actual function only updates the collection data if it's changed (so passing the same collection does nothing).

    Alternatively, just say

    _sprite.spriteId = // your target sprite ID here

    to update only the sprite. If you want to pass a sprite name along (string) instead of the id (int) - use

    _sprite.spriteId = _sprite.GetSpriteIdByName("Sprite Name");
     
  6. Alien3D

    Alien3D

    Joined:
    Jun 22, 2012
    Posts:
    8
    For me it is very hard! :(

    KyleStaves, you can add me to Skype?
    I have 2 days to understand the code. Can you help? I would be very grateful!