instantiate 卡顿严重_Unity中Instantiate实例化物体卡顿问题的解决
发布日期:2021-06-24 16:47:14 浏览次数:2 分类:技术文章

本文共 2279 字,大约阅读时间需要 7 分钟。

本文实例为大家分享了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后调用

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

转载地址:https://blog.csdn.net/weixin_33946505/article/details/112883985 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python播放mp3的库_python使用win32com库播放mp3文件的方法
下一篇:lin通讯从节点同步间隔场_LIN模块介绍

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月15日 01时04分19秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【英语学习】【Daily English】U08 Dating L01 She is the one for me. 2021-07-01
【英语学习】【WOTD】orthography 释义/词源/示例 2021-07-01
【英语学习】【Daily English】U08 Dating L02 What would you do if you were me? 2021-07-01
【英语学习】【WOTD】canker 释义/词源/示例 2021-07-01
【英语学习】【WOTD】polyglot 释义/词源/示例 2021-07-01
【Python】Python3.7.3 - Python命令行参数详解 2021-07-01
【英语学习】【WOTD】comminute 释义/词源/示例 2021-07-01
【英语学习】【Daily English】U09 Fashion L04 It helps if you look the part 2021-07-01
【英语学习】【WOTD】scrumptious 释义/词源/示例 2021-07-01
【英语学习】【WOTD】etiquette 释义/词源/示例 2021-07-01
【英语学习】【Daily English】U10 Education L01 Is this certificate a must? 2019-04-28
【Openstack】【Nova】开发者入门,开发工作流 2019-04-28
【英语学习】【Daily English】U10 Education L02 I'm not a pushy parent 2019-04-28
【英语学习】【Daily English】U10 Education L03 She's planning to study abroad 2019-04-28
【英语学习】【Daily English】U10 Education L04 It's never too late to learn. 2019-04-28
【英语学习】【WOTD】sashay 释义/词源/示例 2019-04-28
【Openstack】实录手动部署Openstack Rocky 双节点(3)- Glance 2019-04-28
【Openstack】实录手动部署Openstack Rocky 双节点(4)- Nova 2019-04-28
【英语学习】【WOTD】cerebral 释义/词源/示例 2019-04-28
【英语学习】【Daily English】U11 Work L01 Would you like a tour of the office? 2019-04-28