Unity3D中脚本语言选择的问题

 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. }  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值