This is the first I’m getting to experiment with 2D in U4.3 It looks great but Something I took for granted in 2D Toolkit seems to be missing.
If I import a atlas sheet and use it for sprites, I imagined each would have a separate ID that would be assignable if for example I wanted to swap a sprite onClick or onTouch. In 2D Toolkit, each sprite on a sheet has a sprite ID# and so I can do this:
void Start ()
{
sprite = GetComponent<tk2dSprite>();
sprite.spriteId = 0;
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.A))
{
sprite.spriteId = 10;
}
if(Input.GetKeyDown(KeyCode.S))
{
sprite.spriteId = 5;
}
}
Is there an equivalent in the 2D system in Unity? If so I haven’t figured it out. Thanks!
If you know which sprite you’ll be using you can always setup a reference variable and set the sprite that way.
using UnityEngine;
using System.Collections;
public class SpriteChanger : MonoBehaviour {
public Sprite referenceSprite;
// Use this for initialization
void Start () {
SpriteRenderer sr = GetComponent<SpriteRenderer>();
sr.sprite = referenceSprite;
}
// Update is called once per frame
void Update () {
}
}
Thanks, Mike.
True, but I’m surprised Unity lacks a spriteID. For sprote variations over a series of sprites( hit, death, rage…), available over multiple conditions based on different events, the ability to pull by an ID number is so much more efficient. This is where 2DTK still maintains the advantage of being a mature product.
B.
I agree. I’ll do some digging in the docs to see if there is any other ways to do this tonight.