【问题标题】:Unity: Memory overflow on Android Device by 800 GameObjectsUnity:800 个游戏对象在 Android 设备上的内存溢出
【发布时间】:2019-05-10 08:33:40
【问题描述】:

我想尝试在一个屏幕上显示许多游戏对象。如果我预加载 777 个怪物(预制件(包括文本和图像),我会得到“内存不足”-Android 设备上的异常。

我该如何处理或做得更好?我是 Unity 的新手。

谁能告诉我为什么我在 Unity-Profiler 中有大约 14k 个对象?请看下面的截图:

加载怪物:

GlobalGameVars.monsterImages = new List<Sprite>(Resources.LoadAll<Sprite>("Monsters"));

我的代码:

public class MonsterPreviewManager : MonoBehaviour
{
    public GameObject monsterPreviewPrefab;

    void Start()
    {
        LoadAllMonsters();
    }

    public void LoadAllMonsters()
    {
        List<Sprite> allMonsterImages = GlobalGameVars.monsterImages;

        if (allMonsterImages == null)
        {
            return;
        }

        GameObject newMonsterPreviewPrefab;

        foreach (Sprite sprite in allMonsterImages)
        {
            newMonsterPreviewPrefab = (GameObject)Instantiate(monsterPreviewPrefab, GameObject.FindGameObjectWithTag("MainContent").transform);

            MonsterPreview monsterPreview = newMonsterPreviewPrefab.GetComponent<MonsterPreview>();

            Image monsterImage = monsterPreview.monsterImage;

            monsterPreview.monsterName.text = sprite.name;

            var tempColor = monsterImage.color;
            tempColor.a = 1f;

            monsterImage.color = tempColor;
            monsterImage.overrideSprite = sprite;

            newMonsterPreviewPrefab.SetActive(true);
        }
    }
}

public class MonsterPreview : MonoBehaviour
{
    public Image monsterImage;
    public Text monsterName;


    void Start()
    {
        enabled = false;
    }
}

异常(ADB 调试)

05-10 10:18:04.438  2037  2134 D Unity   : Unloading 884 Unused Serialized files (Serialized files now loaded: 0)
05-10 10:18:04.486  2037  2052 D Unity   : UnloadTime: 40.446615 ms
05-10 10:18:04.521  2037  2052 D Unity   : System memory in use before: 30.3 MB.
05-10 10:18:04.530  2037  2052 D Unity   : System memory in use after: 30.4 MB.
05-10 10:18:04.530  2037  2052 D Unity   : 
05-10 10:18:04.530  2037  2052 D Unity   : Unloading 3 unused Assets to reduce memory usage. Loaded Objects now: 2259.
05-10 10:18:04.530  2037  2052 D Unity   : Total: 9.076693 ms (FindLiveObjects: 0.649769 ms CreateObjectMapping: 0.361231 ms MarkObjects: 8.016384 ms  DeleteObjects: 0.046615 ms)
05-10 10:18:04.530  2037  2052 D Unity   : 
05-10 10:18:06.975  2037  2052 D Unity   : Could not allocate memory: System out of memory!
05-10 10:18:06.975  2037  2052 D Unity   : Trying to allocate: 8388609B with 16 alignment. MemoryLabel: Texture
05-10 10:18:06.975  2037  2052 D Unity   : Allocation happened at: Line:77 in ./Runtime/Utilities/dynamic_array.h
05-10 10:18:06.975  2037  2052 D Unity   : Memory overview
05-10 10:18:06.975  2037  2052 D Unity   : 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_DEFAULT ] used: 23943538B | peak: 36890011B | reserved: 25201090B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_TEMP_JOB_4_FRAMES ] used: 0B | peak: 0B | reserved: 13631488B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_TEMP_JOB_ASYNC ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_GAMEOBJECT ] used: 2880842B | peak: 2880914B | reserved: 3274637B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_GFX ] used: 571814937B | peak: 580203611B | reserved: 571851576B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_PROFILER ] used: 1586772B | peak: 1591092B | reserved: 1650303B 
05-10 10:18:06.975  2037  2052 D Unity   : [ ALLOC_TEMP_THREAD ] used: 323064B | peak: 0B | reserved: 3768320B 
05-10 10:18:07.116  2037  2052 E Unity   : Could not allocate memory: System out of memory!
05-10 10:18:07.116  2037  2052 E Unity   : Trying to allocate: 8388609B with 16 alignment. MemoryLabel: Texture
05-10 10:18:07.116  2037  2052 E Unity   : Allocation happened at: Line:77 in ./Runtime/Utilities/dynamic_array.h
05-10 10:18:07.116  2037  2052 E Unity   : Memory overview
05-10 10:18:07.116  2037  2052 E Unity   : 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_DEFAULT ] used: 23943538B | peak: 36890011B | reserved: 25201090B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_TEMP_JOB_1_FRAME ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_TEMP_JOB_2_FRAMES ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_TEMP_JOB_4_FRAMES ] used: 0B | peak: 0B | reserved: 13631488B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_TEMP_JOB_ASYNC ] used: 0B | peak: 0B | reserved: 1048576B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_GAMEOBJECT ] used: 2880842B | peak: 2880914B | reserved: 3274637B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_GFX ] used: 571814937B | peak: 580203611B | reserved: 571851576B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_PROFILER ] used: 1586772B | peak: 1591092B | reserved: 1650303B 
05-10 10:18:07.116  2037  2052 E Unity   : [ ALLOC_TEMP_THREAD ] used: 323064B | peak: 0B | reserved: 3768320B 

【问题讨论】:

  • 手机游戏中有777个怪物?听起来很多。
  • @BugFinder 我不知道。所有图像都在 40mb 以下。如果我不在 GameObject 中渲染图像,我会得到相同的错误。
  • “allMonsterImages”的长度是多少?
  • @S.Fragkos 它的Debug.Log(allMonsterImages.Count); -> 777
  • @Bellkadse for start 你能尝试加载前 752 个资源并告诉我你是否有异常或加载正确吗?

标签: c# unity3d out-of-memory gameobject


【解决方案1】:

您应该使用 Sprite Atlas。另外,我看到您的精灵位于静态变量上,这不是一个好主意,它可能会导致一些内存泄漏并显示错误的精灵。这已经发生在我身上了。

你在评论中问我如何将这些 Sprite Atlas 随机分配给怪物对象。您应该使用this answer 来获取单个精灵。然后,您可能需要将您的怪物重命名为“monster-1”、...“monster-777”,然后编写如下内容:

void Start()
{
  AtlasLoader atlasLoader = new AtlasLoader("monsters");
  for(int i = 1; i <= atlasLoader.atlasCount(); i++) {
     int randomInt = Random.Range(1, atlasLoader.atlasCount());
     // probably check if this int isn't already used to avoid duplicates
     Sprite ball = atlasLoader.getAtlas("monster-" + randomInt);
     // Assign this ball to your monster
  }

}

当然是伪代码,我没有测试过,你需要用自己的代码替换cmets。

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多