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

Unity中如何存储与处理大型整数数组以避免崩溃?

最优存储与处理方案

针对你在Unity中遇到的65000个长度为4的整数子数组导致崩溃的问题,结合只读、快速访问的需求,以下是几个优先级从高到低的方案:

1. 自定义结构体+一维数组(推荐)

将每个长度为4的子数组封装为值类型结构体,再用一维数组存储所有结构体,完全消除交错数组的内存碎片化问题:

步骤:

  • 定义存储4个整数的结构体:
public struct Int4
{
    public int a;
    public int b;
    public int c;
    public int d;

    public Int4(int a, int b, int c, int d)
    {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }
}
  • 初始化一维数组存储所有数据:
private Int4[] _int4Array;

public void LoadData(int[][] originalData)
{
    _int4Array = new Int4[originalData.Length];
    for (int i = 0; i < originalData.Length; i++)
    {
        var subArray = originalData[i];
        _int4Array[i] = new Int4(subArray[0], subArray[1], subArray[2], subArray[3]);
    }
}
  • 快速获取第n个子数组:
// 直接返回结构体,无GC分配
public Int4 GetSubArray(int n)
{
    return _int4Array[n];
}

优势:内存布局紧凑,无额外对象开销,访问速度极快,可读性好,完全避免交错数组的内存碎片化问题。

2. 扁平化一维整数数组(极致性能)

将所有子数组的元素连续存储在一个一维int数组中,通过计算索引直接访问:

步骤:

  • 初始化扁平化数组:
private int[] _flatArray;

public void LoadData(int[][] originalData)
{
    int totalLength = originalData.Length * 4;
    _flatArray = new int[totalLength];
    for (int i = 0; i < originalData.Length; i++)
    {
        // 将第i个子数组的4个元素拷贝到对应位置
        Array.Copy(originalData[i], 0, _flatArray, i * 4, 4);
    }
}
  • 快速获取第n个子数组:
// 用值元组返回,无GC分配
public (int a, int b, int c, int d) GetSubArray(int n)
{
    int startIndex = n * 4;
    return (
        _flatArray[startIndex],
        _flatArray[startIndex + 1],
        _flatArray[startIndex + 2],
        _flatArray[startIndex + 3]
    );
}

优势:内存占用最小(仅存储原始整数,无结构体额外开销),访问速度最快,适合对性能要求极高的场景。

3. ScriptableObject 存储(编辑器预处理)

如果数据是固定不变的,可将其打包为ScriptableObject资源,运行时直接加载,避免动态内存分配:

步骤:

  • 创建ScriptableObject子类:
using UnityEngine;

[CreateAssetMenu(fileName = "Int4Collection", menuName = "Data/Int4 Collection")]
public class Int4Collection : ScriptableObject
{
    public Int4[] data; // 使用前面定义的Int4结构体
}
  • 在编辑器中创建该资源文件,手动或通过工具将数据填充到data数组中
  • 运行时加载并访问:
public Int4Collection dataCollection; // 在Inspector中赋值

public Int4 GetSubArray(int n)
{
    return dataCollection.data[n];
}

优势:数据提前预处理,运行时无需初始化拷贝,内存管理更稳定,适合大型静态数据。

为什么原二维数组会崩溃?

你之前使用的int[][]是交错数组,每个子数组都是独立的Array对象,65000个小对象会导致严重的内存碎片化,加上每个Array对象的额外头信息开销,容易触发Unity内存分配失败或GC时的闪退问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 20:40:33