Version: 2022.1
public void RefreshTile (Vector3Int position, Tilemaps.ITilemap tilemap);

参数

position 瓦片在 Tilemap 上的位置。
tilemap 瓦片所处的 Tilemap

描述

当瓦片刷新时调用此方法。

实现此方法并对 Tilemap 上所有受影响的 tiles(包括给定位置处的瓦片)调用 Tilemap.RefreshTile 可刷新它们。这也可用于瓦片的位置会影响相邻瓦片属性的情况。

using UnityEngine;
using UnityEngine.Tilemaps;

// Tile that displays a Sprite when it is alone and a different Sprite when it is orthogonally adjacent to the same NeighourTile [CreateAssetMenu] public class NeighbourTile : TileBase { public Sprite spriteA; public Sprite spriteB;

public override void RefreshTile(Vector3Int position, ITilemap tilemap) { for (int yd = -1; yd <= 1; yd++) { Vector3Int location = new Vector3Int(position.x, position.y + yd, position.z); if (IsNeighbour(location, tilemap)) tilemap.RefreshTile(location); } for (int xd = -1; xd <= 1; xd++) { Vector3Int location = new Vector3Int(position.x + xd, position.y, position.z); if (IsNeighbour(location, tilemap)) tilemap.RefreshTile(location); } }

public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData) { tileData.sprite = spriteA; for (int yd = -1; yd <= 1; yd += 2) { Vector3Int location = new Vector3Int(position.x, position.y + yd, position.z); if (IsNeighbour(location, tilemap)) tileData.sprite = spriteB; } for (int xd = -1; xd <= 1; xd += 2) { Vector3Int location = new Vector3Int(position.x + xd, position.y, position.z); if (IsNeighbour(location, tilemap)) tileData.sprite = spriteB; } }

private bool IsNeighbour(Vector3Int position, ITilemap tilemap) { TileBase tile = tilemap.GetTile(position); return (tile != null &amp;&amp; tile == this); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961