在Unity腳本開發中,常常使用Debug.Log或print來打印調試信息,這倆有什么區別?
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Rotate : MonoBehaviour { 6 public float speed = 100.0f; 7 // Use this for initialization 8 void Start () { 9 Debug.Log("Start"); 10 print("OK"); 11 } 12 13 // Update is called once per frame 14 void Update () { 15 transform.Rotate(speed, 0, 0); 16 } 17 }
在VS2017中,光標定位到print上,按F12轉到定義,只能轉到MonoBehavior的聲明中去:
public static void print(object message);
可見,print是父類的一個靜態方法,可以直接使用。
此時,需要找到Unity項目下"F:\DoWork\Unity3D\NewUnityProject\Library\ScriptAssemblies\Assembly-CSharp.dll"。
Unity編譯C#腳本生成中間語言都存在在Assembly-CSharp.dll文件中,如果以后你在網上下載一個不錯的Unity工程,想看下它的腳本寫的什么,就找這個文件。
我們使用ILSpy[.NET]反編譯工具,下載解壓后,直接運行ILSpy.exe:
將Assembly-CSharp.dll拖拽到ILSpy.exe中:
可見,print是MonoBehaviour中的方法,鼠標單擊無法跳入定義,在VS2017中F12跳轉到MonoBehavior的聲明:
可見,中間語言代碼在UnityEngine.dll中,將其拖入ILSpy.exe中:
可見,print內部調用的是Debug.Log,哈哈!