unity在本地editor调试很方便,但是在android和ios 上调试那叫一个蛋疼啊。。。
android的adb,xcode的控制台虽然能够调试但是效率还是不高啊,打个包机器慢的话得半个小时,再调试。。。。。。
当然还可以本地保存日志,然后查看本地日志,难道找日志文件不浪费时间?为了方便就用gui写了一个控制台,就轻松解决各端调试问题,发布时通过开关或者宏定义关掉,节省资源。代码段如下:
private string msg; //显示到控制台上的信息
private Rect windRect = new Rect(0, 0, 410, 330);
private bool showWindos = false;
public void SendLog(string s)
{
msg+=s; //需要注意的是 这里msg 可以换成stringbuilder ,到了一定长度就清掉
}
pub void ClearLog()
{
msg=string.Empty
}
void OnGUI()
{
BindQuickKey(KeyCode.Q,false);// 绑定快捷键
BindQuickKey(KeyCode.S, true);
if (showWindos)
{
GUI.DrawTexture(new Rect(windRect.x + 2, windRect.y + 2, windRect.width - 4, windRect.height - 4), Resources.Load<Texture>("consolebackground")); //换上自定义背景
windRect = GUI.Window(0, windRect, ConsoleWindow, new GUIContent("Console"));
}
}
private void BindQuickKey(KeyCode keyCode, bool show)
{
if (Event.current.keyCode==keyCode)
{
if(Event.current.alt)
{
showWindos = show;
}
}
}
//自定义窗口哦,自己随便干
private void ConsoleWindow(int id)
{
CreateTextArea();
CreateToggleGroup();
CreateDoButton();
CreateCloseButton();
CreateConsole();
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
本文只是提供了想法,方便各端调试,具体的实现因人而异,更加自己的项目来丰富完善就行,不必生搬硬套。欢迎交流留言。。。