【发布时间】:2021-05-29 03:12:47
【问题描述】:
我正在开发一个手机游戏,我需要加载一个大约 17MB 的 4D 60X40X60X40 整数数组。我目前正在读取 JSON 中的数据并使用 SimpleJSON.cs 在 Unity 中反序列化。
使用 SimpleJSON 解析字符串时会出现问题。游戏挂起,加载文件大约需要 30 秒。
我尝试过使用 IJobs,但它们不允许使用字符串(非 blittable)。
主要优先的是悬挂部分。它不允许我在用户等待 JSON 加载时向他们提供反馈。当然,减少加载时间和任何有关它的提示都会非常有帮助。
我的代码:
IEnumerator StartLoadFiles()
{
// Wait to give time for the game to load everything else
yield return new WaitForSeconds(1f);
/// First read the transition matrices. We are using TextAsset
/// because of compatibility issues with Android.
TextAsset file;
yield return file = Resources.Load(transitionPath) as TextAsset;
loadingSlider.value = 0.3f;
string transitionString = file.ToString();
loadingSlider.value = 0.5f;
/// We are using SimpleJSON.cs as of now. Maybe there is
/// something faster out there... (Here is where it hangs)
JSONNode N;
yield return N = JSON.Parse(transitionString);
loadingSlider.value = 0.7f;
}
编辑:我已经在使用协程,没有任何区别。
【问题讨论】:
-
你可以试试Array Pooling。我不确定这是否会在 4 维数组中有效地工作,但是,如果这对您的情况有任何性能提升,您可以将 4D 数组分离为 4 件 1D 数组。
-
另外,你可以使用Json.net,它支持直接从流中反序列化。 Deserializing from stream