2d-extras
2d-extras copied to clipboard
How do you get the sprites from an Animated Rule Tile at runtime?
As the title says, I'm trying to get the sprite of an animated rule tile.
ATM I get get the main sprite by: Code (CSharp):
void Start(){
tilemap = this.transform.parent.GetComponent<Tilemap> ();
}
void Update() {
tile = tilemap.GetSprite(...);
...GetComponent<SpriteRenderer> ().sprite = tile;
}
This works fine for a single sprite.
What It doesn't seem to do, is get the next sprite of the animation when it changes.
Any Ideas??
Try this:
TileAnimationData tileAnimationData;
TileBase tile = tilemap.GetTile(/* ... */);
if (tile != null) {
tile.GetTileAnimationData(/* ... */, ref tileAnimationData);
}
I'm not able to get to my project atm, because I'm at work, but if I remember right, I need to input an ITilemap to that don't I?
I'm not sure exactly how to do that.
Yes, you are right! I don't think we can get it from Tilemap APIs. You can Override the GetTileAnimationData to caching the TileAnimationData, not good but work.
public class AnimRuleTile : RuleTile
{
public override bool GetTileAnimationData(Vector3Int position, ITilemap tilemap, ref TileAnimationData tileAnimationData)
{
bool success = base.GetTileAnimationData(position, tilemap, ref tileAnimationData);
if (success)
/* save data to other where */
return success;
}
}
So I ended up modifying the StartUp() method and added instantiatedGameObject.GetComponent</*Script on the instantiated GameObject*/>().animSprites = rule.m_Sprites;
I'm not really sure where the actual animation is handled It would be nice to have a currentAnimSprite variable or something that could be exposed or something.
Sorry, there isn't a way to do so right now.
We will expose a new method for you to retrieve this in a future version of Unity!
This works fine for now. I look forward to the update.
Thanks for the help