Debug.Log
static void Log(object message);
static void Log(object message,Object context);
Debug.Log有两个重载函数,我们通常都会使用第一种,然而使用第二个重载函数会有意想不到的效果。当我们在console中点击输出的文本时,会在Hierarchy中指示参数context所代表的游戏物体。所以我们通常会使context为gameObject,以确定该信息是由哪个物体的脚本输出的。
如果参数message为string,我们还可以使用富文本来使输出的信息更显眼
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void Example() {
Debug.Log("Hello", gameObject);
Debug.Log("<color=red>Fatal error:</color> AssetBundle not found");
}
}
至于print,则完全等价于Debug.Log的第一个版本,见其声明:
using System;
public static void print (object message)
{
Debug.Log (message);
}
Debug.Log的声明为:
using System;
public static void Log (object message, Object context)
{
Debug.Internal_Log (0, (message == null) ? "Null" : message.ToString (), context);
}
可见,若message为null,就输出Null,否则就输message.ToString()
using System;
public static void Log (object message, Object context)
{
Debug.Internal_Log (0, (message == null) ? "Null" : message.ToString (), context);
}
游戏发布后,Debug.Log会对游戏性能造成一定影响,所以要尽量将用到的脚本中的所有Debug删除。