Last active
June 29, 2016 20:00
-
-
Save treefortress/2c9b034e696953bd83fc to your computer and use it in GitHub Desktop.
Animated Tiles with 2dToolkit for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
//Drop this on the parent of a 2dToolkit TileMap to animate some of the child sprites. | |
//The name of each sprite must be hardcoded into the Start() fxn. | |
public class AnimateTileMapScript : MonoBehaviour { | |
public tk2dSpriteCollectionData spriteCollection; | |
tk2dTileMap tileMap; | |
List<AnimatedTileData> tileDataList; | |
void Start () { | |
tileMap = GetComponentInChildren<tk2dTileMap>(); | |
spriteCollection = tileMap.SpriteCollectionInst; | |
//Get the SpriteId's for each type of animated tile. Store the spriteId's in an | |
//array, so they can be used to animate the tiles each frame | |
List<int> shockBlocks = new List<int>(); | |
shockBlocks.Add(spriteCollection.GetSpriteIdByName("ElectricBlock0")); | |
shockBlocks.Add(spriteCollection.GetSpriteIdByName("ElectricBlock1")); | |
shockBlocks.Add(spriteCollection.GetSpriteIdByName("ElectricBlock2")); | |
List<int> lavaBlocks = new List<int>(); | |
lavaBlocks.Add(spriteCollection.GetSpriteIdByName("LavaBlock0")); | |
lavaBlocks.Add(spriteCollection.GetSpriteIdByName("LavaBlock1")); | |
lavaBlocks.Add(spriteCollection.GetSpriteIdByName("LavaBlock2")); | |
List<List<int>> animatedBlockLists = new List<List<int>>();//[shockBlocks, lavaBlocks]; | |
animatedBlockLists.Add(shockBlocks); | |
animatedBlockLists.Add(lavaBlocks); | |
//Cache all animated tiles in a list for quick lookup each frame | |
tileDataList = new List<AnimatedTileData>(); | |
for(int x = 0; x < tileMap.width; x++){ | |
for(int y = 0; y < tileMap.height; y++){ | |
//Check each tile against our list of animated tiles. | |
int currentTile = tileMap.GetTile(x, y, 0); | |
foreach(List<int> blockList in animatedBlockLists){ | |
if(blockList.IndexOf(currentTile) != -1){ | |
//Create an animationData for this tile, | |
//and cache it for quick lookups each frame | |
tileDataList.Add(new AnimatedTileData { | |
x = x, | |
y = y, | |
frames = blockList.ToArray() | |
}); | |
} | |
} | |
} | |
} | |
StartCoroutine(_AnimateSprites()); | |
} | |
IEnumerator _AnimateSprites() { | |
while(true){ | |
//Animate frame at 10fps | |
yield return new WaitForSeconds(.1f); | |
AnimatedTileData tileData; | |
for(int i = tileDataList.Count; i-->0;){ | |
tileData = tileDataList[i]; | |
//Increment animation frame of this tile | |
tileData.frameIndex++; | |
if(tileData.frameIndex > tileData.frames.Length - 1){ | |
//Loop animation | |
tileData.frameIndex = 0; | |
} | |
//Assign new tile | |
tileData = tileDataList[i]; | |
tileMap.SetTile(tileData.x, tileData.y, 0, tileData.frames[tileData.frameIndex]); | |
} | |
tileMap.Build(); | |
} | |
} | |
} | |
class AnimatedTileData { | |
public int x; | |
public int y; | |
public int frameIndex = 0; | |
public int[] frames; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is exactly what I've been looking for! Unfortunately, when I gave this a go, I couldn't get it to work.
No errors; but the tiles in question simply don't seem to animate.