Unity跟随Epitome国际象棋教程开发时材质出现闪烁是什么原因
Unity国际象棋棋盘悬停材质闪烁问题修复
问题根源
- 核心原因出在射线检测的层级过滤逻辑矛盾:代码中射线仅检测
Tile层,当鼠标悬停命中瓦片后,立刻将瓦片层级改为Hover,导致下一帧射线无法命中该瓦片,触发非命中逻辑将层级改回Tile,两帧之间来回切换层级就会出现材质闪烁的现象。 - 次要问题:生成瓦片的字符串格式化符号使用错误,BoxCollider默认参数与自定义平面Mesh不匹配可能引发的碰撞检测偏差。
修复步骤
- 调整射线检测的层级掩码,同时包含
Tile和Hover两层,保证悬停后修改层级的瓦片依然能被射线检测到。 - 修正瓦片命名的字符串格式化语法错误,将圆括号改为花括号。
- 调整BoxCollider参数,使其与自定义平面Mesh完全贴合,避免额外的碰撞偏差。
修正后完整代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Chessboard : MonoBehaviour { [Header("Art")] [SerializeField] private Material tileMaterial; //Logic private const int TILE_COUNT_X = 8; private const int TILE_COUNT_Y = 8; private GameObject[,] tiles; private Camera currentCamera; private Vector2Int currentHover; private void Awake() { GenerateAllTiles(1, TILE_COUNT_X, TILE_COUNT_Y); // 初始化悬停标记为无效值 currentHover = -Vector2Int.one; } private void Update() { if (!currentCamera) { currentCamera = Camera.main; return; } RaycastHit info; Ray ray = currentCamera.ScreenPointToRay(Input.mousePosition); // 修复点1:同时检测Tile和Hover两层 if(Physics.Raycast(ray, out info, 100, LayerMask.GetMask("Tile", "Hover"))) { //Get the Indexes of the tile I've hit Vector2Int hitPosition = LookupTileIndex(info.transform.gameObject); //If we're Hovering a tile after not hovering any tiles if(currentHover == -Vector2Int.one) { currentHover = hitPosition; tiles[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover"); } //If we were already hovering a tile, change the previous one if (currentHover != hitPosition) { tiles[currentHover.x, currentHover.y].layer = LayerMask.NameToLayer("Tile"); currentHover = hitPosition; tiles[hitPosition.x, hitPosition.y].layer = LayerMask.NameToLayer("Hover"); } } else { if(currentHover != -Vector2Int.one) { tiles[currentHover.x, currentHover.y].layer = LayerMask.NameToLayer("Tile"); currentHover = -Vector2Int.one; } } } //Generate the Board private void GenerateAllTiles(float tileSize, int tileCountX, int tileCountY) { tiles = new GameObject[tileCountX, tileCountY]; for (int x = 0; x < tileCountX; x++) for (int y = 0; y < tileCountY; y++) tiles[x, y] = GenerateSingleTile(tileSize, x, y); } private GameObject GenerateSingleTile(float tileSize, int x, int y) { // 修复点2:修正字符串格式化的花括号 GameObject tileObject = new GameObject(string.Format("x:{0}, y:{1}", x, y)); tileObject.transform.parent = transform; Mesh mesh = new Mesh(); tileObject.AddComponent<MeshFilter>().mesh = mesh; tileObject.AddComponent<MeshRenderer>().material = tileMaterial; Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector3(x * tileSize, 0, y * tileSize); vertices[1] = new Vector3(x * tileSize, 0, (y + 1) * tileSize); vertices[2] = new Vector3((x + 1) * tileSize, 0, y * tileSize); vertices[3] = new Vector3((x + 1) * tileSize, 0, (y + 1) * tileSize); int[] tris = new int[] { 0, 1, 2, 1, 3, 2 }; mesh.vertices = vertices; mesh.triangles = tris; mesh.RecalculateNormals(); tileObject.layer = LayerMask.NameToLayer("Tile"); // 修复点3:调整BoxCollider参数匹配平面Mesh BoxCollider collider = tileObject.AddComponent<BoxCollider>(); collider.center = new Vector3(0.5f, 0, 0.5f); collider.size = new Vector3(1f, 0.01f, 1f); return tileObject; } //Operations private Vector2Int LookupTileIndex(GameObject hitInfo) { for (int x = 0; x < TILE_COUNT_X; x++) for (int y = 0; y < TILE_COUNT_Y; y++) if (tiles[x, y] == hitInfo) return new Vector2Int(x, y); return -Vector2Int.one; // Invalid } }
内容的提问来源于stack exchange,提问作者Zevnik823
相关产品推荐
相关产品推荐

