最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity中Instantiate實例化物體卡頓問題的解決

 更新時間:2020年10月28日 17:05:05   作者:LLLLL__  
這篇文章主要為大家詳細介紹了Unity實現(xiàn)離線計時器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Unity中Instantiate實例化物體卡頓問題的解決方法,供大家參考,具體內(nèi)容如下

一、前言

當在執(zhí)行多次Instantiate實例化物體時,會卡頓嚴重甚至在移動端會導致程序崩潰
因為Instantiate會產(chǎn)生大量的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)在實例化物體的那一幀產(chǎn)生了3.8MB的GC,而正常來說每幀的GC不能超過2KB,產(chǎn)生如此高的GC在移動端會導致內(nèi)存溢出從而崩潰閃退。更可怕的是這一幀用時1519.24毫秒也就是1.5秒所以程序在此幀會出現(xiàn)卡頓現(xiàn)象

二、解決方法

卡頓或程序崩潰的原因就是在某一幀中產(chǎ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前調(diào)用
  • WaitForNull和Wait0在一幀的Update后LateUpdate前調(diào)用
  • WaitForEndFrame在會在一幀的LateUpdate后調(diào)用

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

华坪县| 怀安县| 上蔡县| 盐池县| 织金县| 航空| 庐江县| 纳雍县| 威海市| 望城县| 尤溪县| 噶尔县| 会理县| 阳泉市| 肇庆市| 朝阳县| 名山县| 南京市| 维西| 南投市| 阳西县| 温宿县| 大竹县| 旅游| 青州市| 沙雅县| 沁水县| 静安区| 奉化市| 滁州市| 宝清县| 三河市| 安义县| 广东省| 永城市| 金堂县| 清镇市| 梓潼县| 池州市| 白银市| 德化县|