• 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");
    		});
    	}
    }
    

      效果如下:

  • 相关阅读:
    关于异常处理解决
    多态
    类的继承和接口
    关于数组的应用知识
    String类型的字符串的知识点
    关于类的一些思想
    一些小程序的代码
    关于Java的一些基础了解
    将string类型的数字参数求和的小程序
    【【洛谷P2678 跳石头】——%%%ShawnZhou大佬】
  • 原文地址:https://www.cnblogs.com/jbw752746541/p/9436404.html
Copyright © 2020-2023  润新知