[Unity优化]unity中的优化方法

来源:互联网 发布:北卡大学淘宝真的假的 编辑:程序博客网 时间:2024/04/29 01:25

1.从CPU、GPU和内存三方面进行优化:http://www.cnblogs.com/murongxiaopifu/p/4284988.html#top

2.UGUI的优化:http://www.jianshu.com/p/061e67308e5f

3.纹理压缩与OpenGL ES:http://blog.csdn.net/asd237241291/article/details/48548557            

使用平台推荐的压缩格式,比如安卓平台的ETC1和IOS平台的PVRTC


4.在UGUI中,如果UI发生重叠(即使只是重叠一部分,即使重叠的两个UI都是一样的),那么dc可能会增加!另外,文字也是占dc的!

5.去掉Mipmap以进一步降低资源包大小和内存占用


在unity中,MipMap是纹理的一个属性,如果开启,则一张1024*1024的纹理图片,它会自动生成512*512、256*256、128*128、64*64、32*32的这么多张额外的图片。主要用途是,当这个纹理离摄像机足够远时,这个纹理图片在摄像机中就会很小,这个时候为了节省性能和达到更好的显示效果,unity会自动选用之前自动生成的众多MipMap小纹理中的一张来替代最初的纹理。(即牺牲内存换取速度)

6.使用九宫格降低资源包大小


7.获取方法的执行时间(参考:http://www.xuanyusong.com/archives/4184)

using UnityEngine;using System.Collections;using System.Diagnostics;using System;public class TestTool {    private static Stopwatch sw;    public static void CalculateTime(Action action)    {        if (sw == null)        {            sw = new Stopwatch();            sw.Start();        }        else        {            sw.Reset();        }        action();        sw.Stop();        UnityEngine.Debug.Log(string.Format("total: {0} ms", sw.ElapsedMilliseconds));    }}

using UnityEngine;using System.Collections;public class Test1 : MonoBehaviour {void Start ()     {        //1        float t = Time.time;        TestMethod();        UnityEngine.Debug.Log(string.Format("total: {0} ms", Time.time - t));        //2        TestTool.CalculateTime(TestMethod);        //3        Profiler.BeginSample("TestMethod");        TestMethod();        Profiler.EndSample();}    void TestMethod()    {        for (int i = 0; i < 1000; i++)        {            GameObject.CreatePrimitive(PrimitiveType.Cube);        }    }}

在Profiler中找到第一帧,就可以看到耗时了。结果如下:




分析:

1.Time.time一帧更新一次

2.Stopwatch得出的时间和Profiler相近


0 0
原创粉丝点击