- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
![Rank: 8](static/image/common//star_level3.gif) ![Rank: 8](static/image/common//star_level3.gif)
- 纳金币
- -1
- 精华
- 11
|
In general, some good practices: have a global script attached to an Empty game object where you will put all your 'static' variables you use repetitively elsewhere in the game, for example In a file GLOB.js Code: static timenow:float=0; static count=0; static globalrotate:float=0; function FixedUpdate(){ timenow = Time.realtimeSinceStartup; count+=1;if (count>10) count=0; globalrotate=Mathf.Sin(Time.deltaTime); }
Now if you need to check the absolute time spent between 2 calls to an Update, use GLO.timenow. If you want to***n a complex operation every 10 time an Update is called, check GLOB.count If you need to rotate a bunch of turrets then use globalrotate, you will save a lot of CPU cycles. Code: function Update(){ if (count==0) { var touch:Vector2=iPhone.GeTouch(0).position; } } is slow and should be Code: var touch:Vector2; function Update(){ if (count==0) { touch=iPhone.GeTouch(0).position;} } Some task do not need to be called every Update, for example : the enemy aim, the player IA every and the animations logic will be called every x updates. enemyanim.js in the Update function : Code: if (GLO.count%3){ //***cute the enemy aim at your player every 3 Update } if (GLO.count%5){ //***cute player IA every 5 Update } Another little one, I don't know if Unity's compiler is doing this kind of optimization but it is always better to use multiplications instead of divisions. Code: x=x/2 is much slower than x=x*0.5 |
|