Unity3d移动端性能优化

 以下条目主要针对移动端,大部分也适合PC端

 一、Profiler的使用

1.性能优化应贯穿项目开发的全过程,如需求(性能能不能达到要求)、设计、开发、测试等。不能开发结束才优化性能。

2.官方推荐的优化工具是【Window】->【Profiler】

 
3.确认瓶颈是CPU-bound(Gfx.WaitForCommands)还是CPU-bound(Gfx.WaitForPresent)

4.内存分析使用Memory Profiler 

 

二、内存优作

5.减少内存收集的影响(GC)

        A.字符串

        在 C# 中,字符串是引用类型,而不是值类型。减少不必要的字符串创建或操作。避免解析基于字符串的数据文件,例如 JSON 和 XML;以 ScriptableObjects 或 MessagePack 或 Protobuf 等格式存储数据。如果您需要在运行时构建字符串,请使用 StringBuilder 类。

        B.Unity 函数调用

        请注意,某些函数会创建堆分配。缓存对数组的引用,而不是在循环中间分配它们。此外,利用某些避免产生垃圾的函数;例如,使用 GameObject.CompareTag 而不是手动将字符串与 GameObject.tag 进行比较(返回一个新字符串创建垃圾)。

        C.装箱

        避免传递值类型变量代替引用类型变量。这会创建一个临时对象,随之而来的潜在垃圾(例如,int i = 123; object o = i)将值类型隐式转换为类型对象。

        D.协程

        虽然 yield 不会产生垃圾,但会创建一个新的WaitForSeconds 对象确实如此。缓存并重用 WaitForSeconds 对象,而不是在 yield 行中创建它。

        E.LINQ 和正则表达式

        这两者都会从幕后拳击中产生垃圾。如果性能有问题,请避免使用 LINQ 和正则表达式。
6.使用Use incremental GC拆分 GC 工作量 

三、代码优化

7.将不必要的逻辑移出 Update、LateUpdate、FixedUpdate。因为每帧都要执行这些函数。

8.如果确实需要使用更新,请考虑每 n 帧运行一次代码。 

9.去掉空的函数,比如Start、Update、LateUpdate、FixedUpdate。

10.去掉调试输出信息,特别是Update、LateUpdate、FixedUpdate中的调试输出Debug.Log。

11.避免在运行时添加组件AddComponent

12.缓存游戏对象和组件

        避免在 Update 方法中调用GameObject.Find、GameObject.GetComponent 和 Camera.main,应该在 Start 中调用它们并缓存结果。
        错误的方式:
                 
        正确的方式:
                 
13.使用对象池,而不是频繁地创建和销毁对象。

14.使用ScriptableObjects来保存参数。 

四、工程设置

15.关闭或降低Accelerometer Frequency


16.关闭Player Setting中不需要的项,如Auto Graphics API、Target Architectures

17.关闭Quality setting中不需要的项

18.关闭Physics中不需要的项,如Auto Simulation、Auto Sync Transforms.

19.如果你做的不是游戏,而是普通的app,去掉Android Game选项。

 

五、资源优化

20.Hierarchy中的物体层级不要太多。

21.将多个操作能合并的合并。

        比如,用Transform.SetPositionAndRotation代替Transform.SetPosition、Transform.SetRotation,用GameObject.Instantiate(prefab, parent, position, rotation)代替GameObject.Instantiate(prefab)、Transform.SetParent、Transform.SetPosition、Transform.SetRotation。
22.关闭Vsysc(Project Settings > Quality)

23.正确导入Texture(messh类似)

        A.降低Max Size

         
        B.图片尺寸为2的整数次方

        C.使用图集

        D.关闭可读写选项

        E.关闭不必要的Mip Maps

        F.使用纹理压缩,iOS和Android一般为ATSC

24.静止不动的物体勾选在static

25.少使用动态光源

26.关闭阴影


27.在移动物体中使用Light Probes


28.使用LOD(Level of Detail)

29.使用Occlusion Culling遮挡剔除

30.可以不使用移动设备默认分辨率,可使用Screen.SetResolution(width, height, false)降低分辨率来提升性能。

31.相机数量不能太多

32.尽量使用简单的shader,或者尽可能简化shader。

33.尽量不用透明材质

34.当需要在代码获取材质时,使用Renderer.sharedMaterial 而不是Renderer.material。Renderer.material将复制一份材质对象。

35.考虑到发热和电池续航能力,选择适当帧率(Application.targetFrameRate),Unity移动端的默认帧率为30fps。 

六、UI优化

36.将复杂UI拆分到多个Canvas中,将动静UI元素放在不同的Canvas中。

37.关闭(disable)看不见的UI元素。例如有的UI被弹出的UI遮挡了。

38.如果Canvas只是显示不需要交互,请去掉GraphicRaycaster组件。

39.如果一个UI元素不需要交互,将不要勾选Raycast Target

40.尽量不用Layout Groups,可能自己写代码控制。

41.避免使用大的列表或Grid views。

42.Canvas尽量使用Screen Space – Overlay 模式。


参考资料(Optimize your mobile game performance: Tips on profiling, memory, and code architecture from Unity’s top engineers | Unity Blog

This book is designed to help you create applications and content that make the best use of Unity on mobile platforms, especially those with Mali GPUs. Chapter 1 Introduction This chapter introduces the Arm Guide for Unity Developers Optimizing Mobile Gaming Graphics. Chapter 2 Optimizing applications This chapter describes how to optimize applications in Unity. Chapter 3 Profiling your application This chapter describes profiling your application. Chapter 4 Optimization lists This chapter lists a number of optimizations for your Unity application. Chapter 5 Real time 3D art best practices: Geometry This chapter highlights some key geometry optimizations for 3D assets. Geometry optimizations can make a game both more efficient, and achieve the overall goal of getting your game to perform better on mobile platforms. Chapter 6 Real time 3D art best practices: Texturing This chapter covers several texture optimizations that can help your games to run more smoothly and look better. Chapter 7 Real time 3D art best practices: Materials and shaders This chapter covers multiple different material and shader optimizations that can help your games to run more efficiently and look better. Chapter 8 Advanced graphics techniques This chapter lists a number of advanced graphics techniques that you can use. Chapter 9 Virtual Reality This chapter describes the process of adapting an application or game to run on virtual reality hardware, and some differences in the implementation of reflections in virtual reality. Chapter 10 Advanced VR graphics techniques This chapter describes various techniques that you can use to improve the graphical performance of your Virtual Reality application. Chapter 11 Vulkan This chapter describes Vulkan and how you enable it. Chapter 12 Arm Mobile Studio This chapter describes the Graphics Analyzer and the Streamline tool.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值