Unity 6.0 URP中Inspector窗口UI消失:疑似关联List<PathNode>
Unity 6.0 + URP下Inspector窗口UI消失问题排查
在Unity 6.0搭配URP的开发场景中,两个关联脚本(GraphBuilder和PathNode)的Inspector窗口UI突然全部消失。经测试定位,问题疑似与PathNode类中的public List<PathNode> neighbours = new List<PathNode>();字段相关。
涉及脚本代码
GraphBuilder.cs
public class GraphBuilder : MonoBehaviour { [Tooltip("Press to Update nodeConnection")] [SerializeField] private bool update; private PathNode[] pathNodes = new PathNode[0]; private bool[,] neighbourMatrix; private void Start() { pathNodes = GetComponentsInChildren<PathNode>(); } private void OnValidate() { /* if(update) update = false; neighbourMatrix = new bool[pathNodes.Length, pathNodes.Length]; for(int i = 0; i < pathNodes.Length; i++) { for (int j = 0; j < pathNodes.Length; j++) { neighbourMatrix[i,j] = pathNodes[i].neighbours.Contains(pathNodes[j]); } } */ } private void OnDrawGizmos() { /* Gizmos.color = Color.red; for(int i = 0; i < pathNodes.Length; i++) { for (int j = 0; j < i; j++) { if (neighbourMatrix[i, j]) Gizmos.DrawLine(pathNodes[i].transform.position, pathNodes[j].transform.position); } } */ } }
PathNode.cs
[ExecuteAlways] public class PathNode : MonoBehaviour { public List<PathNode> neighbours = new List<PathNode>(); private void OnValidate() { /* foreach (PathNode node in FindObjectsOfType<PathNode>()) { if (node == this) continue; if (neighbours.Contains(node)) { if (!node.neighbours.Contains(this)) { node.neighbours.Add(this); } } else { if (node.neighbours.Contains(this)) { node.neighbours.Remove(this); } } } */ } private void OnDrawGizmos() { /* Color nodeColor = Color.blue; nodeColor.a = 0.5f; Gizmos.color = nodeColor; Gizmos.DrawSphere(transform.position, 0.5f); */ } }
排查方向与解决方案
- 循环引用导致序列化死锁:
List<PathNode>存储同类型对象,Unity序列化时可能陷入循环引用检测死锁,导致Inspector无法加载。解决方法:- 给
neighbours字段添加[NonSerialized]标记,禁止Unity自动序列化:[NonSerialized] public List<PathNode> neighbours = new List<PathNode>(); - 如果需要在Inspector中编辑该列表,改用
[SerializeReference](需Unity 2020.1+支持),注意该属性会改变序列化行为,需测试兼容性:[SerializeReference] public List<PathNode> neighbours = new List<PathNode>();
- 给
ExecuteAlways标记冲突:PathNode带有[ExecuteAlways],可能在编辑模式下触发异常导致Inspector崩溃。暂时移除该标记,测试Inspector是否恢复正常,再逐步排查编辑模式下的逻辑问题。- Unity 6.0 URP兼容性bug:Unity 6.0作为较新版本,可能存在URP与脚本序列化的兼容性问题:
- 更新Unity到最新补丁版本,修复已知bug;
- 重置URP管线设置:删除当前URP资产,重新创建默认URP管线配置;
- 脚本编译错误或缓存问题:
- 检查Console窗口是否有编译错误,排查隐性语法问题;
- 清理Unity缓存:点击
Edit > Preferences > Cache Server > Clear Cache,重启Unity; - 重新导入资源:右键点击
Assets目录,选择Reimport All。
内容的提问来源于stack exchange,提问作者Max Persson
相关产品推荐
相关产品推荐

