Unity中Debug打印信息的颜色设置

为了更好的识别打印信息,这里封装了一下打印信息的工具类,虽然Unity中已经很好的识别..但是自己还是想实现新的工具类

DebugBase脚本:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DebugBase<T> where T :new()
{
	/// <summary>
	/// 泛型单例
	/// </summary>
	static T instance;

	public static T Instance {
		get {
			if (instance == null) {
				instance = new T ();
			}
			return instance;
		}
	}

	/// <summary>
	/// 普通打印信息
	/// </summary>
	/// <param name="msg">Message.</param>
	public virtual void Log (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			Debug.Log (msg);
		}
	}

	/// <summary>
	/// 警告打印
	/// </summary>
	/// <param name="msg">Message.</param>
	public virtual void LogWarning (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			Debug.LogWarning (msg);
		}
	}

	/// <summary>
	/// 错误打印
	/// </summary>
	/// <param name="msg">Message.</param>
	public virtual void LogError (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			Debug.LogError (msg);
		}
	}
}

public class GameLog :DebugBase<GameLog>
{
	/// <summary>
	/// 重写父类Log
	/// </summary>
	/// <param name="msg">Message.</param>
	public override void Log (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			base.Log ("*LOG*<color=white>" + msg + "</color>");
		}
	}

	/// <summary>
	/// 重写父类LogWarning
	/// </summary>
	/// <param name="msg">Message.</param>
	public override void LogWarning (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			base.LogWarning ("*Warning*<color=yellow>" + msg + "</color>");
		}
	}

	/// <summary>
	/// 重写父类LogError
	/// </summary>
	/// <param name="msg">Message.</param>
	public override void LogError (string msg)
	{
		if (!string.IsNullOrEmpty (msg)) {
			base.LogError ("*Error*<color=red>" + msg + "</color>");
		}
	}
}

 

  Test脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
	Button btn;

	// Use this for initialization
	void Start ()
	{
		btn = transform.Find ("Button").GetComponent <Button> ();
		btn.onClick.AddListener (delegate() {
			GameLog.Instance.Log ("这是一个LOG");
			GameLog.Instance.LogWarning ("这是一个LogWarning");
			GameLog.Instance.LogError ("这是一个LogError");
		});
	}
}

  效果如下:

 

posted @ 2018-08-07 13:09  不够自律的人  阅读(1937)  评论(0编辑  收藏  举报