gpt4 book ai didi

c# - 自动填充检查器数组

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:20 25 4
gpt4 key购买 nike

我有一个二维数组(基于网格),想通过检查器填充它。给你一个关于游戏的想法

enter image description here

所以我的基础脚本包含所有这些单元格。

public class Ground : MonoBehaviour
{
public Cell[,] cells;

private void Start()
{
Debug.Log(cells);
}
}

由于管理所有这些单元并将它们分配给基础脚本这一事实需要大量工作,因此我考虑将其自动化。因此,每当我添加一个 GameObject(作为 child )并将 Cell 组件分配给它时,Ground 应该自动将此单元格添加到数组中。为此,我考虑为检查器创建一个编辑器脚本。

[CustomEditor(typeof(Ground))]
public class GroundEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

Ground ground = (Ground)target;

Dictionary<Vector2Int, Cell> cells = new Dictionary<Vector2Int, Cell>();
int maximumHorizontalLength = 0;
int maximumVerticalLength = 0;

foreach (Transform groundCell in ground.transform)
{
Cell cell = groundCell.GetComponent<Cell>();

if (cell != null)
{
Vector3 cellPosition = groundCell.position;
Vector2Int cellIndices = new Vector2Int(Mathf.RoundToInt(cellPosition.x), Mathf.RoundToInt(cellPosition.z));
cells.Add(cellIndices, cell);

if (cellIndices.x > maximumHorizontalLength)
{
maximumHorizontalLength = cellIndices.x;
}

if (cellIndices.y > maximumVerticalLength)
{
maximumVerticalLength = cellIndices.y;
}
}
}

maximumHorizontalLength++;
maximumVerticalLength++;

ground.cells = new Cell[maximumHorizontalLength, maximumVerticalLength];

for (int x = 0; x < maximumHorizontalLength; x++)
{
for (int y = 0; y < maximumVerticalLength; y++)
{
ground.cells[x, y] = cells[new Vector2Int(x, y)];
}
}
}
}

所以我想在开始游戏之前创建这个二维网格以节省资源。我不想在播放时按标签或类型搜索。而且我认为这种方法可以节省时间。

不幸的是,当开始游戏时,Debug.Log 打印 null。我希望用这一行填充它 ground.cells = new Cell[maximumHorizo​​ntalLength, maximumVerticalLength]; 我错过了什么吗?这可能吗?

最佳答案

多维数组是not supported by the Unity serializer ,因此您需要重组您希望如何在每个单元格上存储数据。一种选择是创建一个派生自 MonoBehaviour 的新自定义类来存储此数据并将其放置在场景中的某个位置:

[System.Serializable]
public class CellData : MonoBehaviour
{
[System.Serializable]
public class CellReference
{
public int xPos;
public int yPos;
public Cell cell;

public CellReference(Vector2Int posVal, Cell celVal)
{
xPos = posVal.x; yPos = posVal.y; cell = celVal;
}
}

public List<CellReference> cellReferences = new List<CellReference>();
}

然后可以通过检查器通过对 Ground 和 GroundEditor 类进行以下更改来填充它:

public class Ground : MonoBehaviour
{
public CellData cells;

private void Start()
{
Debug.Log(cells);
}
}

[CustomEditor(typeof(Ground))]
public class GroundEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

if (GUILayout.Button("Refresh Cell References"))
{
Ground ground = (Ground)target;

ground.cells.cellReferences = new List<CellData.CellReference>();

foreach (Transform groundCell in ground.transform)
{
Cell cell = groundCell.GetComponent<Cell>();

if (cell != null)
{
Vector3 cellPosition = groundCell.position;
Vector2Int cellIndices = new Vector2Int(Mathf.RoundToInt(cellPosition.x), Mathf.RoundToInt(cellPosition.z));

ground.cells.cellReferences.Add(new CellData.CellReference(cellIndices, cell));
}
}
}
}
}

将 CellsData 脚本连接到您放置的 Ground 脚本,每次在检查器中按下“刷新单元格引用”按钮时,这将生成一个新的单元格列表。

关于c# - 自动填充检查器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com