[英]How to check for different tile with unity tilemaps?
Every example I've found only to do with the new tilemap system only looks for a tile using GetTile and then checks it against itself using "this". 我发现的每个仅与新的tilemap系统有关的示例都仅使用GetTile查找一个tile,然后使用“ this”对照自身进行检查。 EG.
例如。
private bool HasOtherTile(ITilemap tilemap, Vector3Int position)
{
return tilemap.GetTile(position) == this;
}
I want to be able to check for a other specific tiles instead. 我希望能够检查其他特定的图块。 Such as a WallTile.asset tile with a my
public class WallTile : TileBase
script attached I've created. 例如带有我创建的
public class WallTile : TileBase
脚本的public class WallTile : TileBase
贴。 I'm really stuck on how to do this. 我真的对如何做到这一点感到困惑。
I've tried using 我试过使用
gameObject.AddComponent(WaterTile)
TileBase tileTest = gameObject.AddComponent(WallTile);
private bool HasRoadTile(ITilemap tilemap, Vector3Int position)
{
TileBase tileTest = gameObject.AddComponent(WaterTile);
return tilemap.GetTile(position) == tileTest;
}
But this doesnt seem to work. 但这似乎不起作用。
I've also tried ScriptableObject.CreateInstance(WaterTile)
which also doesn't work. 我也尝试了
ScriptableObject.CreateInstance(WaterTile)
,它也无法正常工作。
@Sandro Figo The GetTile method doesn't seem to have an extension method for gameObject it seems. @Sandro Figo GetTile方法似乎没有gameObject的扩展方法。
It returns "T Tile of type T placed at the cell." 它返回“放置在单元格上的T类型的T Tile”。
To check for the tile type at a position: 要检查某个位置的瓷砖类型:
public bool IsTileOfType<T>(ITilemap tilemap, Vector3Int position) where T : TileBase
{
TileBase targetTile = tilemap.GetTile(position);
if (targetTile != null && targetTile is T)
{
return true;
}
return false;
}
Regarding gameobject of a tile: 关于图块的游戏对象:
Your tiles must inherit from Tile class not TileBase in order to manipulate its gameobject. 您的图块必须继承自Tile类而不是TileBase才能操作其游戏对象。
Try this: 尝试这个:
private bool HasWallTile(ITilemap tilemap, Vector3Int position)
{
return tilemap.GetTile(position).gameObject.GetComponent<WallTile>() != null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.