unity3d iphone游戏开发优化

- 试试把gameobject 一个一个禁用,确定下什么最影响性能

- stats 里面有多少个 Draw calls & 三角形

- 合并网格物体

- 合并材质张数

- 不要用网格碰撞.

- Unity对带动作的模型渲染消耗比较大,如果是静态的不带动作文件的模型渲染不会出现效率问题,但是要确保只绘制屏幕内的东西

1. 尽量避免每帧处理

 

比如:
function Update() { DoSomeThing(); }
可改为每5帧处理一次:
function Update() { if(Time.frameCount % 5 == 0) { DoSomeThing(); } }
2. 定时重复处理用 InvokeRepeating 函数实现
比如,启动0.5秒后每隔1秒执行一次 DoSomeThing 函数:
function Start() { InvokeRepeating("DoSomeThing", 0.5, 1.0); }
3. 优化 Update, FixedUpdate, LateUpdate 等每帧处理的函数
函数里面的变量尽量在头部声明。
比如:
function Update() { var pos: Vector3 = transform.position; }
可改为
private var pos: Vector3; function Update(){ pos = transform.position; }
4. 主动回收垃圾
给某个 GameObject 绑上以下的代码:
function Update() { if(Time.frameCount % 50 == 0) { System.GC.Collect(); } }
 
 
5. 运行时尽量减少 Tris 和 Draw Calls
预览的时候,可点开 Stats,查看图形渲染的开销情况。特别注意 Tris 和 Draw Calls 这两个参数。
一般来说,要做到:
Tris 保持在 7.5k 以下
Draw Calls 保持在 20 以下
6. 压缩 Mesh
导入 3D 模型之后,在不影响显示效果的前提下,最好打开 Mesh Compression。
Off, Low, Medium, High 这几个选项,可酌情选取。
7. 避免大量使用 Unity 自带的 Sphere 等内建 Mesh
Unity 内建的 Mesh,多边形的数量比较大,如果物体不要求特别圆滑,可导入其他的简单3D模型代替。
8. 优化数学计算
比如,如果可以避免使用浮点型(float),尽量使用整形(int),尽量少用复杂的数学函数比如 Sin 和 Cos 等等。
1、 ArrayList的对象在使用时应该注意以下事项
使用 
arraylen = myarray. length; 
for (i=0; i< arraylen; i++) 
}
避免
for (i=0; i<myarray.length; i++) { 

2、 少用临时变量
使用
var touch:Vector2; 

function Update(){ 
if (count==0) { touch=iPhone.GeTouch(0).position;} 
避免
function Update(){ 
if (count==0) { 
var touch:Vector2=iPhone.GeTouch(0).position; 
}

3、 少用算数符号/,改为乘以相应小数
使用 
x * 0.5f 
避免
x/2

4、 动作和AI不需要每帧都执行
if (GLO.count%3){ 
// execute the enemy aim at your player every 3 Update 
if (GLO.count%5){ 
// execute player IA every 5 Update 
}

5、 Mesh部分
原文:
- do not use several materials for one mesh. I got the problem with a little model that had 2 materials. It was taking 2 time more polys! When i removed one material, it was showing at half his the size. I don't know why. Usually each material = 1 draw call, so use texture atlas! 
个人译文:
-对于一个mesh不要使用多张materials。我遇到过这样的问题是一个小模型拥有2张materials导致了2倍甚至更多的时间!而当我减少一个material的时候,时间减少了一半。

原文
- Use polygon cruncher to lower your mesh polys but even if your mesh look a little square, you can compensate this by tweaking "smoothing angle" in the importer inspector. For a highly optimized mesh, change the smoothing angle to 80 or even 120 and you mesh will look more smooth even with just a few polygons.
个人译文:
使用多边形粉碎机降低你mesh的面熟,但是虽然你的mesh看起来像一个正方形。你可以通过调整在导入区的” smoothing angle”选项来进行补偿。对于一个高优化的mesh,调整平滑角度到80甚至120度,你的mesh将看起来很配合虽然只是一点点的数量(???)

原文
- try not to use any expensive arithmetic like sin() or cos(). instead fill an array with precalculated values with x=0 to 360) sin(x) and cos(x) and use the values of this table. Of course it is in the case of you don't need precision.
个人译文
不要尝试使用任何昂贵消耗的数学公式,例如sin() 或 cos(),用预定义好的0到360度的sin(x)和cos(x)的值组成链表来替代。当然了前提是你不需要太精确。

原文
- force integer when possible. If you have a counter or check the position on the screen, declare the variable as INT, they are faster.
个人译文
尽可能的使用整形。如果你有一个检测屏幕位置的变量或者一个计数器,定义成整形会更快。

Unity3d的一些小窍门
1、防止黑屏
In script change iPhoneSettings.screenCanDarken to false. This basically does the same as:
//Objective-C call [UIApplication sharedApplication].idleTimerDisabled = YES;
It prevents the auto lock on iPhone to kick-in.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值