unity 性能优化

这篇博客探讨了在Unity中使用GameObject.SetActive()方法设置物体显示和隐藏是否真的会消耗性能。通过测试代码对比了在有无Start()方法情况下物体隐藏显示的性能差异,结果显示Start()方法对性能影响较大,而单纯的物体显示隐藏在没有Start()的情况下性能开销极小。同时,文章也提及了Awake()方法的性能消耗主要发生在添加组件时。
摘要由CSDN通过智能技术生成

1.设置物体显示隐藏

网上说设置物体显示隐藏(GameObject.SetActive())会消耗性能.其实这种说法是片面的,或者是错误的。

测试代码

在一个节点里初始化5000左右个物体。将物体显示和隐藏,添加标记"xxxxxxxxxxxxxxxxxxx",然后查看性能。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;

public class ProfilerTest : MonoBehaviour
{
    public GameObject gm;

    public Button bt;

    private Transform parent;
    private void initGM()
    {
        Profiler.BeginSample("xxxxxxxxxxxxxxxxxxx");  //添加标记
        float tm = Time.realtimeSinceStartup;

        for (int i = 0; i < 5000; i++)
        {
           GameObject g = new GameObject("GM",typeof(UnityFunctionTest));
            //GameObject g = new GameObject("GM");


            if (!parent)
                {
                parent = g.transform;
                }
 
            if (i % 10 =&
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值