unity脚本中, print和Debug.Log本质上是相同的
在untiy脚本中,每个脚本均会继承基类MonoBehaviour,在继承基类MonoBehaviour的条件下,print和Debug.Log本质上确实是相同的。
例如:
public class Lifecycle : MonoBehaviour
{
}
原因如下:
将UnityEngine.CoreModule.dll利用ILSpy/dnSpy逆向反编译后,查看print,结果如下
public static void print(object message)
{
Debug.Log(message);
}
总结:
针对一些核心算法类,即无需挂载到模型对象中的cs文件。这种情况下,该类无需继承基类MonoBehaviour(补充:要挂在到模型对象上,必须继承基类MonoBehaviour),此时只能使用Debug.Log。而print是基类MonoBehaviour的公共静态方法,此时无法使用。
由于本质上print和Debug.Log是相同的,所以建议就算是继承基类MonoBehaviour,也最好使用Debug.Log。