Unity中实现六边形规则瓦片溢出部分覆盖上层瓦片的问题求助
Unity中实现六边形规则瓦片溢出部分覆盖上层瓦片的问题求助
我正在Unity里尝试自定义规则瓦片,想实现一个核心效果:让瓦片上“延伸出去”到上方瓦片区域的部分,优先级比上方放置的瓦片更高,能显示在它们的上层。
简单来说,我的瓦片集里有些瓦片会“延伸”到上方相邻的两个六边形瓦片区域,但用规则瓦片绘制时,这些延伸部分会被上方的瓦片挡住,边缘衔接得特别生硬,完全看不到自然的溢出效果。举个具体例子:单独放置的森林瓦片,树会稍微超出六边形的顶部;当在它上方放其他瓦片时,这些树本该显示在上方瓦片的上层,实现自然融合,但现在延伸的部分直接被上方瓦片切掉了。
我试着写了一个自定义六边形规则瓦片的脚本,但没起作用,代码如下:
using UnityEngine; using UnityEngine.Tilemaps; using System.Collections.Generic; [CreateAssetMenu(fileName = "New Custom Hexagonal Rule Tile", menuName = "Tiles/Custom Hexagonal Rule Tile")] public class CustomHexagonalRuleTile : HexagonalRuleTile { public List<TileBase> possibleTiles; // List of possible tiles that share the same directional rules // Define the positions of the neighbors in a pointy top hexagonal grid private static readonly Vector3Int[] HexagonalNeighbors = new Vector3Int[] { new Vector3Int(1, 0, 0), // Right new Vector3Int(1, -1, 0), // Bottom Right new Vector3Int(0, -1, 0), // Bottom Left new Vector3Int(-1, 0, 0), // Left new Vector3Int(-1, 1, 0), // Top Left new Vector3Int(0, 1, 0) // Top Right }; public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData) { base.GetTileData(position, tilemap, ref tileData); // Check for overflow from the Top Right Vector3Int topRight = position + HexagonalNeighbors[5]; if (tilemap.GetTile(topRight) == this) { // Handle overflow from the Top Right // Randomly select a tile from the list of possible tiles if (possibleTiles != null && possibleTiles.Count > 0) { TileBase selectedTile = possibleTiles[Random.Range(0, possibleTiles.Count)]; tileData.sprite = ((Tile)selectedTile).sprite; // Set the sprite of the selected tile tileData.transform = Matrix4x4.Translate(new Vector3(0, 0.5f, 0)); // Adjust the position to show overflow } } // Check for overflow from the Top Left Vector3Int topLeft = position + HexagonalNeighbors[4]; if (tilemap.GetTile(topLeft) == this) { // Handle overflow from the Top Left // Randomly select a tile from the list of possible tiles if (possibleTiles != null && possibleTiles.Count > 0) { TileBase selectedTile = possibleTiles[Random.Range(0, possibleTiles.Count)]; tileData.sprite = ((Tile)selectedTile).sprite; // Set the sprite of the selected tile tileData.transform = Matrix4x4.Translate(new Vector3(0, 0.5f, 0)); // Adjust the position to show overflow } } } }
我感觉问题可能出在我没正确用代码定义“溢出部分”的逻辑,有没有大佬遇到过类似的情况呀?
补充说明:我特意找了张图来更清楚地展示问题:
可以看到,上方没放瓦片时,森林瓦片有延伸到上方区域的部分;但放上瓦片后,这些延伸部分就消失了,衔接得很不自然。抱歉一开始没放图😅
备注:内容来源于stack exchange,提问作者martin pedersen





