先上代码:
class MyDebug : MonoBehaviour
{
static List<string> messages = new List<string>();
static List<string> names = new List<string>();
public Rect rect;
public float IntervalSize = 16;
public float ClearTime = 5;
float nowTime = 0;
public MyDebug()
{
rect = new Rect(0, 0, 100, 100);
}
void Update()
{
if (nowTime < ClearTime)
{
nowTime += Time.deltaTime;
}
else
{
messages.Clear();
names.Clear();
nowTime = 0;
}
}
void OnGUI()
{
for (int i = 0; i < names.Count; i++)
{
GUI.Box(new Rect(0, i * IntervalSize, rect.width, rect.height),names[i] + " : " + messages[i], GUI.skin.box);
}
}
public static void Add(string name, string message)
{
if (names.Contains(name) == false)
{
names.Add(name);
messages.Add(message);
}
else
{
for (int i = 0; i < names.Count; i++)
{
if (names[i] == name)
{
messages[i] = message;
break;
}
}
}
}
使用方法:
1 把脚本挂在场景里的任意对象上
2 在要输出log的代码里添加
MyDebug.Add("MyDebug", "XXXXXXXXX");