2d-extras icon indicating copy to clipboard operation
2d-extras copied to clipboard

How do you get the sprites from an Animated Rule Tile at runtime?

Open TehMighty opened this issue 5 years ago • 6 comments

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??

TehMighty avatar Dec 11 '19 03:12 TehMighty

Try this:

TileAnimationData tileAnimationData;
TileBase tile = tilemap.GetTile(/* ... */);
if (tile != null) {
    tile.GetTileAnimationData(/* ... */, ref tileAnimationData);
}

johnsoncodehk avatar Dec 11 '19 09:12 johnsoncodehk

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.

TehMighty avatar Dec 11 '19 14:12 TehMighty

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;
    }
}

johnsoncodehk avatar Dec 11 '19 17:12 johnsoncodehk

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.

TehMighty avatar Dec 12 '19 02:12 TehMighty

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!

ChuanXin-Unity avatar Dec 12 '19 07:12 ChuanXin-Unity

This works fine for now. I look forward to the update.

Thanks for the help

TehMighty avatar Dec 12 '19 11:12 TehMighty