Debug.Log的妙用


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删除。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值