栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

Unity中Instantiate实例化物体卡顿问题的解决

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity中Instantiate实例化物体卡顿问题的解决

本文实例为大家分享了Unity中Instantiate实例化物体卡顿问题的解决方法,供大家参考,具体内容如下

一、前言

当在执行多次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查看性能

发现在实例化物体的那一帧产生了3.8MB的GC,而正常来说每帧的GC不能超过2KB,产生如此高的GC在移动端会导致内存溢出从而崩溃闪退。更可怕的是这一帧用时1519.24毫秒也就是1.5秒所以程序在此帧会出现卡顿现象

二、解决方法

卡顿或程序崩溃的原因就是在某一帧中产生了大量的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);
   }
  }
 }
}

三、协程中几种yield reutrn的执行顺序

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");
 }
}

经过测试,得出以下结论

  • WaitForFixedUpdate在一帧的FixedUpdate后Update前调用
  • WaitForNull和Wait0在一帧的Update后LateUpdate前调用
  • WaitForEndframe在会在一帧的LateUpdate后调用

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121734.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号