uniy打包到真机上后在游戏内直接查看log

把下面的LogCat.cs导入到工程中,调用LogCat.Init();
在Unity Editor换下,按空格键即可打开log窗口
在真机上,四根手指头点一下屏幕即可打开log窗口

// LogCat.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LogCat : MonoBehaviour
{
    public static void Init()
    {
        GameObject go = new GameObject("LogCat");
        var logCat = go.AddComponent<LogCat>();
    }

    void Start()
    {
        Application.logMessageReceivedThreaded += logCallBack;

        m_scrollViewRect = new Rect(0, 0, Screen.width, Screen.height * 0.95f);

        m_normalLblStyle = new GUIStyle();
        m_normalLblStyle.normal.textColor = Color.green;
        m_normalLblStyle.wordWrap = true;
        m_normalLblStyle.fontSize = 25;

        m_btnStyle = new GUIStyle();
        m_btnStyle.normal.background = CreateTexture(new Color(1, 1, 1, 0.8f));
        m_btnStyle.alignment = TextAnchor.MiddleCenter;
        m_btnStyle.fontSize = 30;


        m_normalSvStyle = new GUIStyle();
        m_normalSvStyle.normal.background = CreateTexture(new Color(0, 0, 0, 0.8f));
        m_normalSvStyle.padding = new RectOffset(10, 0, 10, 10);

        m_redLblStyle = new GUIStyle();
        m_redLblStyle.normal.textColor = Color.red;
        m_redLblStyle.wordWrap = true;
        m_redLblStyle.fontSize = 25;
    }

    private Texture2D CreateTexture(Color c)
    {
        Texture2D tex = new Texture2D(1, 1);
        tex.SetPixel(0, 0, c);
        tex.wrapMode = TextureWrapMode.Repeat;
        tex.Apply();
        return tex;
    }

    void Update()
    {
        if ((Input.GetMouseButtonDown(0) && Input.touchCount == 4) || Input.GetKeyDown(KeyCode.Space))
        {
            m_showLog = !m_showLog;
        }
        DragScrollView();
    }

    public void OnGUI()
    {
        if (!m_showLog) return;
        GUILayout.BeginArea(m_scrollViewRect);
        {
            GUILayout.Box("", GUILayout.Width(m_scrollViewRect.width), GUILayout.Height(m_scrollViewRect.height));
            GUILayout.EndArea();
        }

        GUILayout.BeginArea(m_scrollViewRect);
        {
            m_scrollViewPos = GUILayout.BeginScrollView(m_scrollViewPos, m_normalSvStyle);
            {

                GUILayout.Label(m_onlyErrLog ? m_errLogStr : m_logStr, m_onlyErrLog ? m_redLblStyle : m_normalLblStyle);
                GUILayout.EndScrollView();
            }
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Only error log:" + m_onlyErrLog, m_btnStyle, GUILayout.Height(80)))
            {
                m_onlyErrLog = !m_onlyErrLog;
            }
            GUILayout.Space(3);
            if (GUILayout.Button("Clear log", m_btnStyle, GUILayout.Height(80)))
            {
                m_logStr = "";
                m_errLogStr = "";
            }
            GUILayout.Space(3);
            if (GUILayout.Button("Close", m_btnStyle, GUILayout.Height(80)))
            {
                m_showLog = false;
                DestroyBoxColliderPanel();
            }
            GUILayout.EndHorizontal();

            GUILayout.EndArea();
        }

    }

    private void DragScrollView()
    {
#if UNITY_EDITOR
        if (Input.GetMouseButton(0))
        {
            m_deltaY = Input.GetAxis("Mouse Y");
        }

#else
        if(Input.touchCount == 1)
        {
            if(Input.touches[0].phase == TouchPhase.Moved)
            {
                m_deltaY = Input.GetAxis("Mouse Y");
            }
        }
#endif
        m_scrollViewPos.y += 20*m_deltaY;
        if (m_deltaY > 0)
        {
            m_deltaY -= 0.02f;
            if (m_deltaY < 0)
                m_deltaY = 0;
        }
        else
        {
            m_deltaY += 0.02f;
            if (m_deltaY > 0)
                m_deltaY = 0;
        }
    }

    private void logCallBack(string condition, string stackTrace, LogType type)
    {
        if (type == LogType.Error || type == LogType.Exception)
        {
            m_errLogStr += condition + "\n";
            CutLog(ref m_errLogStr);
        }
        m_logStr += condition + "\n";
        CutLog(ref m_logStr);
    }

    private void CutLog(ref string s)
    {
        var len = s.Length;
        if (len > LEN_LIMIT)
        {
            s = s.Substring(len - LEN_LIMIT, LEN_LIMIT);
        }
    }

    private bool m_showLog = false;
    private bool m_onlyErrLog = false;
    private string m_logStr = "";
    private string m_errLogStr = "";

    private float m_deltaY;

    private Rect m_scrollViewRect;
    private GUIStyle m_normalLblStyle;
    private GUIStyle m_btnStyle;
    private GUIStyle m_normalSvStyle;
    private GUIStyle m_redLblStyle;
    private Vector2 m_scrollViewPos;
    private const int LEN_LIMIT = 10000;
}

你可能感兴趣的:(unity3D)