12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2871|回复: 11
打印 上一主题 下一主题

Optimizing with Unity iPhone, the first three things I’ll do…

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-11-24 20:56:00 |只看该作者 |倒序浏览


           Optimization is a bit of a black art when you’re dealing with an api or engine where you can’t profile the internals, and unity-iphone is no exception. I’ve collected here a few of tricks I’ve found that have helped me squeeze more performance out of my iphone projects.
           

            
         

           1. Do Less, (aka timeslicing)
         

           Lets be honest, it’s really easy to put lots of code in Update() or FixedUpdate() for a behavior, or have it called once a frame. However that means that no matter how fast that code is, it’s going to be  called at a rate of (hopefully) 30fps.
         

           Most of the time, you don’t need to do this. Take for example this pseudo-code of a homing script.
         





               view plain
              



               copy to clipboard
              



               print
              



               ?
              









                 public
               



               
              



                 class
               



                MyHoming : MonoBehaviour {   
              




               
            



                  
            



                public
               



               
            



                void
               



               Update() {   
            



                     
            


               // expensive targetting and not so expensive trajectory calculations in here
              


               
            



                     
            


               // currently called every frame
              


               
            



                  }   
            



              }  
            




           public class MyHoming : MonoBehaviour {

        public void Update() {
                // expensive targetting and not so expensive trajectory calculations in here
                // currently called every frame
        }
}
         

           Consider replacing it with this:
         





               view plain
              



               copy to clipboard
              



               print
              



               ?
              









                 public
               



               
              



                 class
               



                MyHoming : MonoBehaviour {   
              




               
            



                  
            



                public
               



               
            



                float
               



               targettingFrequency = 1.2f;   
            



                  
            



                public
               



               
            



                float
               



               trajectoryFrequency = 0.2f;   
            



                  
            



                public
               



               
            



                float
               



               trajectoryBlend = 1.0f;   
            



                  
            



                private
               



               Vector3 updatedTrajectory = Vector3.zero;   
            



                  
            



                private
               



               Vector3 currentTrajectory = Vector3.zero;   
            



               
            



                  
            



                public
               



               
            



                void
               



               Start()   
            



                  {   
            



                      InvokeRepeating(
            


               "DoTargetting"
              


              , targettingFrequency*Random.value, targettingFrequency);   
            



                      InvokeRepeating(
            


               "DoTrajectory"
              


              , trajectoryFrequency*Random.value, trajectoryFrequency);   
            



                      DoTargetting();   
            



                      DoTrajectory();   
            



                  }   
            



               
            



                  
            



                public
               



               
            



                void
               



               DoTargetting() {   
            



                     
            


               // search for targets here, we don't do it often because it's expensive
              


               
            



                  }   
            



               
            



                  
            



                public
               



               
            



                void
               



               DoTrajectory() {   
            



                     
            


               // trajectory isn't as expensive but it's not something we need to do every frame
              


               
            



                  }   
            



               
            



                  
            



                public
               



               
            



                void
               



               Update() {   
            



                     
            


               // Just interpolate values each frame
              


               
            



                      currentTrajectory = SetTrajectory(Vector3.Lerp(currentTrajectory, updatedTrajectory, Time.DeltaTime*trajectoryBlend);   
            



                  }   
            



              }   
            



              }  
            




           public class MyHoming : MonoBehaviour {

        public float targettingFrequency = 1.2f;
        public float trajectoryFrequency = 0.2f;
        public float trajectoryBlend = 1.0f;
        private Vector3 updatedTrajectory = Vector3.zero;
        private Vector3 currentTrajectory = Vector3.zero;

        public void Start()
        {
                InvokeRepeating("DoTargetting", targettingFrequency*Random.value, targettingFrequency);
                InvokeRepeating("DoTrajectory", trajectoryFrequency*Random.value, trajectoryFrequency);
                DoTargetting();
                DoTrajectory();
        }

        public void DoTargetting() {
                // search for targets here, we don't do it often because it's expensive
        }

        public void DoTrajectory() {
                // trajectory isn't as expensive but it's not something we need to do every frame
        }

        public void Update() {
                // Just interpolate values each frame
                currentTrajectory = SetTrajectory(Vector3.Lerp(currentTrajectory, updatedTrajectory, Time.DeltaTime*trajectoryBlend);
        }
}
}
         

            
         

           It looks a lot more complicated, but it’s really not. You’re still doing the same trajectory updates it’s just that they’re now being blended across multiple frames. I can’t stress how much performance can be saved by doing this to all of your update functions. If you haven’t done so already just do it now.
         

            
         

            
         

           2. Force garbage collection frequently.
         

           If you don’t, eventually there’ll be a huge spike when it does so automatically.
           

           Try dropping this script into an object in your scene:
         





               view plain
              



               copy to clipboard
              



               print
              



               ?
              









                 using
               



                UnityEngine;   
              




               
            





                class
               



               GarbageCollectManager : MonoBehaviour {   
            



                  
            



                public
               



               
            



                int
               



               frameFreq = 30;   
            



                  
            



                void
               



               Update()   {   
            



                     
            



                if
               



               (Time.frameCount % frameFreq == 0)   
            



                          System.GC.Collect();   
            



                  }   
            



              }  
            




           using UnityEngine;

class GarbageCollectManager : MonoBehaviour {
        public int frameFreq = 30;
        void Update()        {
                if (Time.frameCount % frameFreq == 0)
                        System.GC.Collect();
        }
}
         

           It won’t improve your fps, but it will cut down on the occasional spikes that memory management will cause.
         

            
         

            
         

           3. Triangle & Draw Call counts
         


            Stay under 7.5k tris
           

            Stay under 20 draw calls
           


            
         

           These are often preached as a gospel, I’d say it’s more of a guide to keep an eye on your art performance.
         




           If you go over 7.5k triangles, or over 20 draw calls it’s not the end of the world… you’re just
           
            likely
           
           to be bottlnecking yourself at submitting your graphics content to the graphics API.
         

           You can batch draw calls by sharing the same material across mutiple objects and also increase this further by marking objects as “static” when they’ll never move.
         

           But! Don’t forget that there are other factors not displayed here… for instance fill rate, when rendering scenes with overdraw and complex shaders.  
           
            The best way to track your total graphics performance is still through xcode instruments, and the debugger log with profiling turned on.
           


           Conclusion
         

           While these three are by no means the only tricks to getting unity to perform on an iphone, these ones are my first port of call when things start to slow. I’ve been told about a whole lot of other optimizations, such as managers instead of unity called Update functions, coroutines better than invokes… so forth but haven’t tried them yet.
         

           Anyone got any suggestions, techniques they’ve found saved them some performance that are different from the ones above?
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-3-14 23:26:19 |只看该作者
先顶上去,偶要高亮加精鸟!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-15 23:18:07 |只看该作者
响应天帅号召,顶
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-3-16 19:53:14 |只看该作者

   
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-3-17 23:28:58 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-7-2 23:26:46 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-7-29 23:25:10 |只看该作者
很有心,部分已收录自用,谢谢
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-9-24 23:25:30 |只看该作者
跑着去顶朋友滴铁
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

9#
发表于 2012-9-30 23:26:05 |只看该作者
水……生命之源……灌……
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2013-2-1 23:27:51 |只看该作者
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-18 18:14 , Processed in 0.069762 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部