unity打印日志的函数Debug.Log
但是这样会有一个问题,某些c#打印过于频繁 ,我作为一个开发者不关心他的模块日志。但是我没办法去给他把日志删除了,这样没有道德。于是想着写这样个工具,它不太完美 ,它只能指定我们需要那些cs文件的日志 整体它长这样
比如我现在调试角色的跳跃,角色的跳跃是个很复杂的过程,有起跳阶段,上升阶段1 2 3 ,最高阶段 ,下落阶段123 落体阶段等等等等 这里面相关的参数很多,需要很多判定,需要看日志
如下图就是看移动组件的日志 。上面filter是一个list 我们可以加内容进去 加了之后需要显示的日志模块就会出现这个模块,然后选中它就是看它所属的一些cs日志了。
LogHelper.cs代码如下
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public interface ILog
{
public void Log(string str);
}
[System.Serializable]
public class LogCS
{
[SerializeField] public int index;
[SerializeField] public List<string> csNameList = new List<string>();
}
public class LogHelper : MonoBehaviour
{
public static LogHelper Instance;
private void Awake()
{
GameObject.DontDestroyOnLoad(this.gameObject);
}
void Start()
{
Instance = this;
}
public void Init()
{
runtimeMap.Clear();
for (int i = 0; i < map.Count; i++)
{
if (!runtimeMap.ContainsKey(map[i].index))
{
runtimeMap.Add(map[i].index, new List<string>());
}
runtimeMap[map[i].index] = map[i].csNameList;
}
}
[Label("过滤标签集合")] public string[] filter = new string[] { };
[HideInInspector]
public int currentIndex = 0;
[HideInInspector]
public List<string> filterCs = new List<string>();
[HideInInspector]
[SerializeField]
public List<LogCS> map = new List<LogCS>();
[HideInInspector]
public Dictionary<int, List<string>> runtimeMap = new Dictionary<int, List<string>>();
}
#if UNITY_EDITOR
[CustomEditor(typeof(LogHelper))]
public class LogHelperEditor : Editor
{
private int lastIndex = 0;
public LogHelper self
{
get
{
return target as LogHelper;
}
}
SerializedProperty filterCs;
public void OnEnable()
{
filterCs = serializedObject.FindProperty("filterCs");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
EditorGUI.BeginChangeCheck();
lastIndex = self.currentIndex;
self.currentIndex = EditorGUILayout.Popup("需要显示的日志模块", self.currentIndex, self.filter);
if (EditorGUI.EndChangeCheck())
{
Save(lastIndex);
self.filterCs.Clear();
Debug.Log((self.currentIndex < self.map.Count).ToString());
if (self.currentIndex < self.map.Count)
{
Debug.Log(self.map[self.currentIndex].index);
for (int i = 0; i < self.map[self.currentIndex].csNameList.Count; i++)
{
self.filterCs.Add(self.map[self.currentIndex].csNameList[i]);
}
}
}
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(filterCs, new GUIContent("过滤脚本名(不带后缀)"));
if (EditorGUI.EndChangeCheck())
{
Save(self.currentIndex);
}
serializedObject.ApplyModifiedProperties();
}
void Save(int index)
{
if (self.map.Count > (index))
{
Debug.Log("self.map.Count > (index) " + self.map.Count + "--" + index);
self.map[index].csNameList.Clear();
for (int i = 0; i < self.filterCs.Count; i++)
{
self.map[index].csNameList.Add(self.filterCs[i]);
Debug.Log("self.map add" + index + " Count == " + self.map[index].csNameList.Count);
}
}
else
{
Debug.Log("self.filterCs.Count == " + self.filterCs.Count);
var ls = new LogCS();
self.map.Add(ls);
ls.index = index;
for (int i = 0; i < self.filterCs.Count; i++)
{
ls.csNameList.Add(self.filterCs[i]);
}
}
self.Init();
}
}
#endif
然后具体日志答应在一个静态类中 我叫DLog方法 。建议就是在所有的cs文件中 实现ILog接口 这个接口就一个方法 Log 然后实现类的Log方法
public static class CommonUtils
{
public static void DLog(this ILog log, object str)
{
string fileName = log.GetType().Name;
var ins = LogHelper.Instance;
bool showLog = false;
var index = LogHelper.Instance.currentIndex;
if (LogHelper.Instance.runtimeMap.ContainsKey(index))
{
var list = LogHelper.Instance.runtimeMap[index];
for (int i = 0; i < list.Count; i++)
{
if (fileName.ToLowerInvariant() == list[i].ToLowerInvariant())
{
showLog = true;
break;
}
}
}
else
{
showLog = true;
}
if (showLog)
Debug.Log(str);
}
}