这个应该属于撸一个适合自己的小游戏框架的第二篇。上一篇是Ui脚本自动生成。
在完成Ui自动生成后,想到了一个问题。就是log信息的显示,因为log是开发中的重要部分。所以就准备重新整理一下适合自己的log信息部分。
需求1:编辑环境Log信息打开,正式版本log信息关闭。因为log信息也是程序执行的一部分,大量log信息也会消耗性能。
然后很简单解决。
正式发包前,在脚本中添加 Debug.unityLogger.logEnabled = false即可。据说是某个Untiy版本后才能用。
需求2:添加标签。方便查找Bug时进行过滤搜索。
很简单,自定义一个logtag,在需要加标记的地方加上即可。
需求3:打印不同颜色的log
网上有很多类似这种的方法。
Debug.Log("-><color=#FF6A6A>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#FFFFFF>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#FFFF00>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#EE6A50>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#CDC5BF>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#8E388E>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#7A7A7A>" + "我是控制台带颜色的字体" + "</color>");
Debug.Log("-><color=#00EEEE>" + "我是控制台带颜色的字体" + "</color>");
那么,我换颜色还要分颜色来封装方法吗?那就特么要类死了。
于是我们就要依托Unity的颜色库来封装这个方法了。
那么如何把Unity中的color转换成FFFFFF这种格式呢?我又找到了这种方法。
public static string ColorToHex(Color color)
{
int r = Mathf.RoundToInt(color.r * 255.0f);
int g = Mathf.RoundToInt(color.g * 255.0f);
int b = Mathf.RoundToInt(color.b * 255.0f);
string hex = string.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
return hex;
}
完美解决问题了。我们现在可以把字符设置成任意颜色了。
我本来以为完结了,但当我重新整理方法时突然发现另一个类
//
// 摘要:
// A collection of common color functions.
[NativeHeader("Runtime/Export/ColorUtility.bindings.h")]
public class ColorUtility
{
public ColorUtility();
//
// 摘要:
// Returns the color as a hexadecimal string in the format "RRGGBB".
//
// 参数:
// color:
// The color to be converted.
//
// 返回结果:
// Hexadecimal string representing the color.
public static string ToHtmlStringRGB(Color color);
//
// 摘要:
// Returns the color as a hexadecimal string in the format "RRGGBBAA".
//
// 参数:
// color:
// The color to be converted.
//
// 返回结果:
// Hexadecimal string representing the color.
public static string ToHtmlStringRGBA(Color color);
public static bool TryParseHtmlString(string htmlString, out Color color);
这个类不仅提供了Unity颜色和HTMI格式互相转换的方法,还提供了Unity颜色转带alpha通道的HTMI格式的颜色的方法。
HTMI格式转UnityColor的用法。
public static Color HtmlStringRGB(string htmlString)
{
Color color;
ColorUtility.TryParseHtmlString(htmlString,out color);
return color;
}
下面附上我的log代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyLog{
public static void Log(string info)
{
Debug.Log(info);
}
public static void LogError(string info)
{
Debug.LogError(info);
}
public static void LogWarning(string info)
{
Debug.LogWarning(info);
}
public static void LogWithTag(string info, LogTag logTag)
{
string temp = logTag.ToString();
Debug.Log(temp + ":" + info);
}
public static void LogWithColor(string info,Color color)
{
string MyColor = ColorUtility.ToHtmlStringRGB(color);
Debug.Log("-><color=#"+MyColor+">" + info + "</color>");
}
}
public enum LogTag
{
UnityTag_Info,
UnityTag_Sdk,
UnityTag_Ads,
UnityTag_Editor,
}
只是把Unity的log重新封装了一层,仍然可以通过 Debug.unityLogger.logEnabled = false 关闭。只封装了我常用的一些,这里只是抛砖引玉,其它的log方法都可以继续组合封装。当然,可以控制颜色自然也能控制大小之类的。不做赘述。
另外,有的人不喜欢通过编辑器看log,想通过屏幕看log信息的话。
也很简单,思路就是获得log信息,显示到屏幕上。至于怎么显示,不要遮挡Ui就行了。
public class LogComponent : MonoBehaviour
{
private bool IsDevelopment;
private Vector2 m_scroll = new Vector2(0, 180);
List<string> mLogs = new List<string>();
void Start()
{
GameObject.DontDestroyOnLoad(gameObject);
}
void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
void OnDisable()
{
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
string str = logString + "\r\n" + stackTrace;
string logMsg = string.Format("[{0:u}][{1}]\r\n{2}", System.DateTime.Now, "tag", str);
mLogs.Add(logMsg);
}
void ClearLog()
{
mLogs.Clear();
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 0, 180, 160));
if (GUILayout.Button("Show Log", GUILayout.Width(180f), GUILayout.Height(80f)))
{
IsDevelopment = IsDevelopment ? false : true;
}
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(180, 0, 180, 160));
if (GUILayout.Button("Clear Log", GUILayout.Width(180f), GUILayout.Height(80f)))
{
ClearLog();
}
GUILayout.EndArea();
if (!IsDevelopment)
return;
GUILayout.BeginArea(new Rect(0, 180, Screen.width, Screen.height - 180));
m_scroll = GUILayout.BeginScrollView(m_scroll, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height - 280));
GUIStyle style = new GUIStyle();
style.fontSize = 20;
GUIStyleState ss = new GUIStyleState();
ss.textColor = Color.red;
style.normal = ss;
foreach (var v in mLogs)
{
GUILayout.Label(v, style);
}
GUILayout.EndScrollView();
GUILayout.EndArea();
}
}
以上。