Unity中Instantiate實例化物體卡頓問題的解決
本文實例為大家分享了Unity中Instantiate實例化物體卡頓問題的解決方法,供大家參考,具體內容如下
一、前言
當在執(zhí)行多次Instantiate實例化物體時,會卡頓嚴重甚至在移動端會導致程序崩潰
因為Instantiate會產生大量的GC,使CPU過高,導致崩潰
下面是一段測試代碼:當我們按下按鍵時實例化100000個預制體
using UnityEngine;
public class Test : MonoBehaviour
{
public GameObject prefab;
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Generate();
}
}
private void Generate()
{
for (int i = 0; i < 100000; i++)
{
Instantiate(prefab);
}
}
}
運行后通過profiler查看性能

發(fā)現(xiàn)在實例化物體的那一幀產生了3.8MB的GC,而正常來說每幀的GC不能超過2KB,產生如此高的GC在移動端會導致內存溢出從而崩潰閃退。更可怕的是這一幀用時1519.24毫秒也就是1.5秒所以程序在此幀會出現(xiàn)卡頓現(xiàn)象
二、解決方法
卡頓或程序崩潰的原因就是在某一幀中產生了大量的GC
所以可以把一幀的操作分幀進行
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public GameObject prefab;
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(Generate());
}
}
private IEnumerator Generate()
{
int tempCount = 0;
for (int i = 0; i < 100000; i++)
{
if (tempCount <= 5000)
{
Instantiate(prefab);
tempCount++;
}
else
{
tempCount = 0;
yield return new WaitForEndOfFrame();
Instantiate(prefab);
}
}
}
}
三、協(xié)程中幾種yield reutrn的執(zhí)行順序
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
private void Start()
{
StartCoroutine(WaitForNull());
StartCoroutine(WaitForEndFrame());
StartCoroutine(Wait0());
StartCoroutine(WaitForFixedUpdate());
}
private IEnumerator WaitForNull()
{
Debug.Log("[1]WaitForNull:" + Time.frameCount);
yield return null;
Debug.Log("[2]WaitForNull:" + Time.frameCount);
}
private IEnumerator WaitForEndFrame()
{
Debug.Log("[1]WaitForEndFrame:" + Time.frameCount);
yield return new WaitForEndOfFrame();
Debug.Log("[2]WaitForEndFrame:" + Time.frameCount);
}
private IEnumerator Wait0()
{
Debug.Log("[1]Wait0:" + Time.frameCount);
yield return 0;
Debug.Log("[2]Wait0:" + Time.frameCount);
}
private IEnumerator WaitForFixedUpdate()
{
Debug.Log("[1]WaitForFixedUpdate:" + Time.frameCount);
yield return new WaitForFixedUpdate();
Debug.Log("[2]WaitForFixedUpdate:" + Time.frameCount);
}
private void Update()
{
Debug.Log("update");
}
private void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
private void LateUpdate()
{
Debug.Log("LateUpdate");
}
}
經(jīng)過測試,得出以下結論
- WaitForFixedUpdate在一幀的FixedUpdate后Update前調用
- WaitForNull和Wait0在一幀的Update后LateUpdate前調用
- WaitForEndFrame在會在一幀的LateUpdate后調用
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C#中Equals和GetHashCode使用及區(qū)別
這篇文章主要介紹了C#中Equals和GetHashCode使用及區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02
Unity編輯器資源導入處理函數(shù)OnPostprocessAudio使用案例
這篇文章主要為大家介紹了Unity編輯器資源導入處理函數(shù)OnPostprocessAudio使用案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

