using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class TilemapController : MonoBehaviour
{
ameObject terrainObj;
Tilemap terrain;
Grid grid;
List<Sprite> sList;
public void Init()
{
grid = GameObject.FindObjectOfType<Grid>();
terrainObj = grid.transform.GetChild(0).gameObject;
terrain = terrainObj.GetComponent<Tilemap>();
sList = new List<Sprite>();
GetTerrainSpriteTypeAll(terrain, sList);
}
public static void etTerrainSpriteTypeAllG(Tilemap tilemap, List<Sprite> spriteList)
{
Tile tile;
BoundsInt bound = tilemap.cellBounds;
for (int y = bound.max.y - 1; y >= bound.min.y; --y)
{
for (int x = bound.min.x; x < bound.max.x; ++x)
{
tile = tilemap.GetTile<Tile>(new Vector3Int(x, y, 0));
if (tile != null && !spriteList.Contains(tile.sprite))
{
spriteList.Add(tile.sprite);
}
}
}
}
public Vector3Int GetPositionOnTilemap(Vector3 _pos)
{
Vector3Int pos = terrain.WorldToCell(new Vector3( _pos.x, _pos.y, _pos.z));
return pos;
}
public int GetTerrainNumbur(Vector3Int _pos)
{
int index = -1;
var tile = terrain.GetTile<Tile>(new Vector3Int(_pos.x, _pos.y, 0));
if( tile != null){
index = sList.IndexOf(tile.sprite);
}
return index;
}
}