首页 > 技术文章 > 查看Unity脚本源代码

MakeView660 2020-02-02 13:27 原文

在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,哈哈! 

推荐阅读