前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity编辑器案例(Log日志方案)

Unity编辑器案例(Log日志方案)

作者头像
bering
发布2019-12-03 11:35:05
9820
发布2019-12-03 11:35:05
举报
文章被收录于专栏:游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

Unity开发移动端能不能像编辑器环境下实时查看Log日志呢? 我们可以写个编辑器作为Log的Socket接收服务端,这里纯属我闲的没事干做的假设,当然移动端查看日志有很多好的方案,下面看下方案的大致实现。

代码语言:javascript
复制
 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") };
代码语言:javascript
复制
     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处理部分如下所示:

代码语言:javascript
复制
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);
    }
   

}

最后看下效果图:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档