博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Optimizing Performance in iOS Part3:Optimizing Script Performance
阅读量:6195 次
发布时间:2019-06-21

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

Optimizing Script Performance

This page gives some general hints for how to improve script performance on iOS.

Reduce Fixed Delta Time

Use a fixed delta time of 15-25 fps. You can change this in Edit->Project Settings->Time. This reduces how often FixedUpdate is called and how often the physics engine has to perform collision detection and rigidbody updates. If you are using rigidbodies for the main character, you can enable interpolation in the Rigidbody Component to smooth out low fixed delta time steps.

 

Reduce GetComponent Calls

Using GetComponent or BuiltIn component accessors can have a noticeable overhead. You can reduce it by caching a direct reference to the component.

For example:

function Update () {    transform.Translate(0, 1, 0);}

You can optimize your script to this instead:

myTransform : Transform;function Awake () {   myTransform = transform;}function Update () {    myTransform.Translate(0, 1, 0);}
 

Avoid Allocating Memory

  • Minimize your allocations in scripts.
  • Use structs. Structs do not allocate memory, instead they are placed on the stack and are passed by value which is fast.
 

Reduce the GUI

  • Don't use GUILayout, use GUI functions instead and minimize the amount of GUI on screen
  • Use MonoBehaviour.useGUILayout = false; to minimize GUI overhead
function Awake () {    useGUILayout = false;}
 
 

Use iOS Script Call Optimization

Most of the functions in UnityEngine namespace are implemented in C/C++. Calling such functions from scripts has additional performance overhead. Consider using iOS Script Call optimization in Edit->Project Settings->Player to gain several extra milliseconds per frame:

  • Slow and Safe - default mono internal call handling with exception support.
  • Fast and Exceptions Unsupported - fast mono internal call implementation, but exceptions are not supported, so use it with care. A typical scenario when it could be useful: application deals a lot with Unity Engine and does not throw any exceptions. This option could save 1-4 ms / frame.

Optimizing Garbage Collection

  • As mentioned above ,try avoiding any allocations
  • If allocations can't be avoided then there are two allocation/collection strategies:
    • small heap fast and frequent garbage collection
      This strategy might work well for games that have long action gameplay and needs smooth framerate.
      Typical heap for Unity iOS game which allocates small blocks for short period of time often is ~200 KB, garbage collection of such heap takes ~5ms (on iPhone 3G). With a 1 MB heap the duration of garbage collection increases to ~7 ms.
      Sometimes it can be useful to force garbage collection every Nth frame:
      if (Time.frameCount % 30 == 0){   System.GC.Collect();}
      But use it with care, watch internal profiler statistics, and make decisions based on them.
    • large heap slow and rare garbage collection
      This strategy might work for games that are have short to medium length smooth framerate demanding gameplay. The idea is to avoid garbage collection while gameplay is running and make garbage collections during gameplay pauses. It is good idea to preallocate some space on the heap during startup which is large enough to survive gameplay sessions yet small enough to avoid application killing by OS because of low system memory. Mono tries to expand the heap only when necessary, so sometimes it is good idea to force its expansion on startup:
      function Start() {        var tmp = new System.Object[1024];        // make allocations in smaller blocks to avoid them to be treated in a special way, which is designed for large blocks        for (var i : int = 0; i < 1024; i++)                tmp[i] = new byte[1024];        // release reference        tmp = null;}
      Later, you can force garbage collection when gameplay is paused:
      System.GC.Collect();
      Use this with care, pay attention to internal profiler statistics, and make decisions based on the actual numbers.

转载于:https://www.cnblogs.com/pengyingh/articles/2340836.html

你可能感兴趣的文章
Tomcat历史版本下载
查看>>
解决ssh连接linux服务器速度慢
查看>>
“东京八分钟”算什么? 来看看AR真正的魅力!
查看>>
Linux文件权限总结
查看>>
Raid信息丢失数据恢复及oracle数据库恢复验证方案
查看>>
视+ EasyAR全系新品AWE惊艳亮相,打通业内外技术隔阂
查看>>
联合报告称“杀人机器人”将出现,人类安全或受红色警告
查看>>
android之官方下拉刷新组件SwipeRefreshLayout
查看>>
uva10288 Coupons 【概率 分数】
查看>>
Hive之 hive架构
查看>>
windows named pipe 客户端 服务器
查看>>
字符串格式化(百分号)
查看>>
RecyclerView,CardView导入和使用(Demo)
查看>>
cisco路由器的基本硬件组成
查看>>
3、输入输出重定向、bash算术、正则表达式 学习笔记
查看>>
图像滤镜艺术---怀旧风格滤镜
查看>>
用批处理(bat)同时运行多个程序的方法
查看>>
“智慧青岛”里的海信力量
查看>>
mycelipse2014/2015激活工具和教程
查看>>
《赛迪机器人3·15报告》揭示机器人产品质量6大痛点
查看>>