You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity InventorySystem循环未完整迭代问题求助

问题分析与修复方案

问题描述

我有一个Slot类型数组slots,包含72个非空Slot对象,但执行DefinePosition方法中的循环时,迭代到第16次就停止了,对应gridArray的[1,9]位置。相关代码如下:

public class InventorySystem : MonoBehaviour
{
    [SerializeField] private Slot[] slots = new Slot[72];
    [SerializeField] private GameObject InventoryUI;

    private Slot[,] gridArray;

    // Defines the position of slots in the inventory grid
    private void DefinePosition(float panelWidth, float slotWidth, int[] panelPadding, float slotSpaceX)
    {
        float currentWidthOcupiedPerLine = 0;
        float gridWidth = panelWidth - panelPadding[0] - panelPadding[1];
        slotWidth = slotWidth + (slotSpaceX / 2);

        int numColumns = Mathf.FloorToInt(gridWidth / slotWidth);
        //int numRows = Mathf.CeilToInt((float)slots.Length / numColumns);
        int numRows = 0; // Inicializa o número de linhas como 0

        //calcules the number of  necessary rows 
        for (int i = 0; i < slots.Length; i++)
        {
            currentWidthOcupiedPerLine += slotWidth;

            if (currentWidthOcupiedPerLine >= gridWidth)
            {
                currentWidthOcupiedPerLine = 0;
                numRows++; // Incrementa o número de linhas
            }
        }

        Debug.Log(numColumns + ", " + numRows + "bau baun baun bauynbaunbaun hehe");

        // Inicializa o gridArray com as dimensões corretas
        gridArray = new Slot[numRows, numColumns];
 
        int row = 0;
        int column = -1;

        Debug.Log(slots.Length + "caucau " + "asas");

        for (int i = 0; i < slots.Length; i++)
        {
            currentWidthOcupiedPerLine += slotWidth;

            if (currentWidthOcupiedPerLine >= gridWidth)
            {
                currentWidthOcupiedPerLine = 0;
                row++;
                column = 0;
                Debug.Log(string.Format("linha {0} {1}", row, column));
                gridArray[row, column] = slots[i];
                continue;
            }

            column++;
            Debug.Log(string.Format(" {0} {1}", row, column));
            gridArray[row, column] = slots[i];
        }
    }

    private void Awake()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (slots[i].ItemInSlot == null)
            {
                slots[i].transform.GetChild(0).gameObject.SetActive(false);
            }
        }

        Debug.Log("a");
        DefinePosition(870, 90, new int[] { 10, 0 }, 5);
    }
}

注:gridArray预期为8行9列的二维数组。

问题原因

  1. 行数计算错误:原代码通过宽度累加判断行数,当最后一行总宽度未超过gridWidth时,不会触发行数递增,导致gridArray实际被初始化为7行9列(而非预期的8行)。
  2. 列索引越界:填充循环中,宽度累加逻辑会导致列索引超出数组范围(比如第16次迭代时column变为9,但数组列索引仅到8),触发索引越界异常,直接终止循环。

修复方案

1. 修正行数计算逻辑

删除原有的行数计算循环,改用直接公式计算,确保行数准确:

int numColumns = Mathf.FloorToInt(gridWidth / slotWidth);
int numRows = Mathf.CeilToInt((float)slots.Length / numColumns);

2. 简化数组填充逻辑

用更直观的计数方式控制行列索引,避免宽度计算带来的误差:

int row = 0;
int column = 0;

for (int i = 0; i < slots.Length; i++)
{
    gridArray[row, column] = slots[i];
    column++;
    // 列数达到上限时换行
    if (column >= numColumns)
    {
        column = 0;
        row++;
    }
}

完整修正后的DefinePosition方法

private void DefinePosition(float panelWidth, float slotWidth, int[] panelPadding, float slotSpaceX)
{
    float gridWidth = panelWidth - panelPadding[0] - panelPadding[1];
    slotWidth = slotWidth + (slotSpaceX / 2);

    int numColumns = Mathf.FloorToInt(gridWidth / slotWidth);
    int numRows = Mathf.CeilToInt((float)slots.Length / numColumns);

    Debug.Log($"列数:{numColumns},行数:{numRows}");

    gridArray = new Slot[numRows, numColumns];
 
    int row = 0;
    int column = 0;

    Debug.Log($"Slots总数:{slots.Length}");

    for (int i = 0; i < slots.Length; i++)
    {
        gridArray[row, column] = slots[i];
        Debug.Log($"位置:[{row}, {column}]");
        
        column++;
        if (column >= numColumns)
        {
            column = 0;
            row++;
        }
    }
}

内容的提问来源于stack exchange,提问作者Jamelaumn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:44:58