Unity编辑器案例(Log日志方案)
Unity开发移动端能不能像编辑器环境下实时查看Log日志呢? 我们可以写个编辑器作为Log的Socket接收服务端,这里纯属我闲的没事干做的假设,当然移动端查看日志有很多好的方案,下面看下方案的大致实现。
private Texture[] LogBar = new Texture[] { EditorGUIUtility.FindTexture("console.infoicon") };
private Texture[] LogWarnBar = new Texture[] { EditorGUIUtility.FindTexture("console.warnicon") };
private Texture[] LogErrorBar = new Texture[] { EditorGUIUtility.FindTexture("console.erroricon") };
void DrawToolBar()
{
using (EditorGUILayout.HorizontalScope hScop = new EditorGUILayout.HorizontalScope())
{
Rect rect = hScop.rect;
GUI.backgroundColor = Color.white;
rect.height = 20;
GUI.Box(rect, "");
GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(40), GUILayout.Height(20) };
GUIStyle style = new GUIStyle();
GUIStyleState normalState = new GUIStyleState();
normalState.background = EditorGUIUtility.FindTexture("AvatarController.LayerHover");
style.onNormal = normalState;
style.onHover = normalState;
GUIStyleState btnNormalState = new GUIStyleState();
btnNormalState.background = EditorGUIUtility.FindTexture("Grid.BoxTool");
btnNormalState.textColor = Color.black;
GUIStyle btnStyle = new GUIStyle();
btnStyle.alignment = TextAnchor.MiddleCenter;
btnStyle.normal = btnNormalState;
using (EditorGUILayout.HorizontalScope hScop0 = new EditorGUILayout.HorizontalScope(options))
{
GUI.Box(hScop0.rect, "");
if (GUILayout.Button("Clear", options))
{
NetWorkManager._instance.ClearAllInfo();
}
}
GUILayout.Space(EditorGUIUtility.currentViewWidth - 180);
using (EditorGUILayout.HorizontalScope hScop1 = new EditorGUILayout.HorizontalScope(options))
{
GUI.Box(hScop1.rect, "");
bolLog = GUILayout.Toggle(bolLog, new GUIContent(NetWorkManager._instance.logInfo.Count.ToString(), LogBar[0]), style, options);
}
using (EditorGUILayout.HorizontalScope hScop2 = new EditorGUILayout.HorizontalScope(options))
{
GUI.Box(hScop2.rect, "");
bolWarnType = GUILayout.Toggle(bolWarnType, new GUIContent(NetWorkManager._instance.warnInfo.Count.ToString(), LogWarnBar[0]), style, options);
}
using (EditorGUILayout.HorizontalScope hScop3 = new EditorGUILayout.HorizontalScope(options))
{
GUI.Box(hScop3.rect, "");
bolErrorType = GUILayout.Toggle(bolErrorType, new GUIContent(NetWorkManager._instance.errorInfo.Count.ToString(), LogErrorBar[0]), style, options);
}
}
}
void DrawConsole()
{
using (var scr = new EditorGUILayout.ScrollViewScope(scrollTextPos))
{
scrollTextPos = scr.scrollPosition;
if (bolLog)
{
for (int i = 0; i < NetWorkManager._instance.logInfo.Count; i++)
{
GUILayout.Label(new GUIContent(NetWorkManager._instance.logInfo[i], LogBar[0]));
}
}
if (bolWarnType)
{
for (int i = 0; i < NetWorkManager._instance.warnInfo.Count; i++)
{
GUILayout.Label(new GUIContent(NetWorkManager._instance.warnInfo[i], LogWarnBar[0]));
}
}
if (bolErrorType)
{
for (int i = 0; i < NetWorkManager._instance.errorInfo.Count; i++)
{
GUILayout.Label(new GUIContent(NetWorkManager._instance.errorInfo[i], LogErrorBar[0]));
}
}
}
}
Socket处理部分如下所示:
public struct CMD_LOG
{
public string log;
public string stack;
public LogType type;
//public string time { get; set; }
}
public class NetWorkManager
{
private Socket socket = null;
private static NetWorkManager net = null;
public static NetWorkManager _instance
{
get
{
if (net == null)
{
net = new NetWorkManager();
}
return net;
}
}
byte[] recBuffer = new byte[1024 * 5];
public List<string> logInfo = new List<string>();
public List<string> warnInfo = new List<string>();
public List<string> errorInfo = new List<string>();
public NetWorkManager()
{
if (socket == null)
{
Debug.Log("Build Socket");
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2010);
socket.Bind(endPoint);
socket.Listen(0);
socket.BeginAccept(new AsyncCallback(AcceptCallBack), socket);
}
}
void AcceptCallBack(IAsyncResult iR)
{
Debug.Log("Accept");
Socket recClient = iR.AsyncState as Socket;
Socket handler = recClient.EndAccept(iR);
if (handler != null)
{
Debug.Log("客户端连接了");
}
handler.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, ReceiveCallBack, handler);
}
public void ClearAllInfo()
{
logInfo.Clear();
warnInfo.Clear();
errorInfo.Clear();
}
void ReceiveCallBack(IAsyncResult iR)
{
Socket recSocket = iR.AsyncState as Socket;
int read = recSocket.EndReceive(iR);
try
{
if (read > 0)
{
Debug.Log("reretrfg" + read);
string recJson = System.Text.Encoding.Unicode.GetString(recBuffer,0,read);
Debug.Log(recJson);
CMD_LOG item = JsonUtility.FromJson<CMD_LOG>(recJson);
switch (item.type)
{
case LogType.Error:
errorInfo.Add(item.log + "\n");
break;
case LogType.Assert:
logInfo.Add(item.log + "\n");
break;
case LogType.Warning:
warnInfo.Add(item.log + "\n");
break;
case LogType.Log:
logInfo.Add(item.log + "\n");
break;
case LogType.Exception:
errorInfo.Add(item.log + "\n");
break;
}
}
}
catch (Exception e)
{
Debug.Log(e);
}
recSocket.BeginReceive(recBuffer, 0, recBuffer.Length, SocketFlags.None, ReceiveCallBack, recSocket);
}
}
最后看下效果图: