Unity3D中指令碼語言選擇的問題

來源:互聯網
上載者:User

 Unity3D支援三種指令碼語言,javaScript、C#、Bool,Bool這種語言我不熟悉、不做評論,下面就說說javaScript與C#,我本人是搞C++,javaScript與C#這二種語言以前都沒有接觸過,通過這段時間對Unity3D的瞭解,我個人比較偏向與C#,最主要的就是C#的 delegate,通過C#的delegate可以調用其他指令碼,而javaScript只能通過sendMessage這種方式,Unity3D是C++寫的,所以我估計SendMessage的方式應該是MFC的方式是一樣的,從運行速度、和代碼的結構上來講C#都是更好的選擇。

當然也不排斥javaScript,不過我是真心的不喜歡這種弱類型的語言。下面是Delegate和SendMessage之間的最佳化效能差距測試

  1. using UnityEngine;  
  2. using System.Collections;  
  3. /// <summary>   
  4. /// Delegate basic.   
  5. /// just test Delegate && SendMessage ..
      
  6. ///    
  7. /// By Chiuan 2012.8   
  8. /// </summary>   
  9. public class DelegateBasic : MonoBehaviour {  
  10.       
  11.     //define my delegate statement.
      
  12.     public delegate void MyDelegate(string arg1);  
  13.       
  14.     //create my delegate object   
  15.     public MyDelegate myDelegate;  
  16.       
  17.     //need some values to debug time spent.
      
  18.     bool isStart;  
  19.     float timeStart;  
  20.     int count;  
  21.       
  22.     bool isStartSendMessage;  
  23.       
  24.     // Use this for initialization
      
  25.     void Start () {  
  26.         myDelegate += myFunciton1;  
  27.         //myDelegate += myFunciton2;
      
  28.     }  
  29.       
  30.     // Update is called once per frame
      
  31.     void Update () {  
  32.         if(isStart )  
  33.         {  
  34.             isStart = false;  
  35.             count = 0;  
  36.             timeStart = Time.realtimeSinceStartup;  
  37.             Debug.Log("Start = " + timeStart );  
  38.             for(int i= 0; i< 50000;i++)  
  39.             {  
  40.                 if(myDelegate != null) myDelegate("");  
  41.             }  
  42.         }  
  43.           
  44.         if(isStartSendMessage)  
  45.         {  
  46.             isStartSendMessage = false;  
  47.             count = 0;  
  48.             timeStart = Time.realtimeSinceStartup;  
  49.             Debug.Log("Start = " + timeStart );  
  50.             for(int i= 0; i< 50000;i++)  
  51.             {  
  52.                 this.gameObject.SendMessage("myFunciton1","",SendMessageOptions.DontRequireReceiver );  
  53.             }  
  54.         }  
  55.     }  
  56.       
  57.       
  58.     void OnGUI()  
  59.     {  
  60.         if(GUILayout.Button("INVOKE Delegate"))  
  61.         {  
  62.             isStart = true;  
  63.         }  
  64.           
  65.         if(GUILayout.Button("SendMessage"))  
  66.         {  
  67.             isStartSendMessage = true;  
  68.         }  
  69.           
  70.     }  
  71.       
  72.     void myFunciton1(string s)  
  73.     {  
  74.         count++;  
  75.         if(count == 50000)  
  76.         {  
  77.             Debug.Log("End = " + Time.realtimeSinceStartup );  
  78.             Debug.Log("Time Spent = " + ( Time.realtimeSinceStartup - timeStart ) );  
  79.         }  
  80.     }  
  81.       
  82.     void myFunciton2(string s)  
  83.     {  
  84.         //Debug.Log("myFunciton2 " + s);
      
  85.     }  
  86.       
  87.       
  88. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.