Unity暂停调试工具

自己写的一个小工具,在Unity输出了指定Log后暂停编辑器。在Unity工程比较大不方便定位问题时,可以通过log来触发暂停,然后单步运行,方便看到每一帧的情况。

效果如图:

可在Unity输出以指定文本开头、结尾或包含时,暂停编辑器。配置保存在Assets文件夹同目录,不用每次运行重新输入。

主要原理就是注册Unity的log事件,然后对log内容与暂停列表的数据对比,在符合时调用编辑器的API暂停。代码如下:

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace YX
{
    /// <summary>
    /// 在log输出包含指定关键字的时候暂停编辑器,方便单步执行
    /// </summary>
    public class PauseOnLog : MonoBehaviour
    {
        public enum KeyWordsType
        {
            Start,
            Contain,
            End,
        }

        [Serializable]
        public class PausePoint
        {
            public string Name;
            public string Key;
            public KeyWordsType Type;
        }

        internal class JsonWrap
        {
            public PausePoint[] KeyWords;
        }

        public PausePoint[] KeyWords = new PausePoint[0];

        private string _cacheFile = "YXPausePoint.config";

        private void Start()
        {
            if (File.Exists(_cacheFile))
            {
                try
                {
                    KeyWords = JsonUtility.FromJson<JsonWrap>(File.ReadAllText(_cacheFile,Encoding.UTF8)).KeyWords;
                }
                catch
                {
                    KeyWords = new PausePoint[0];
                }
            }
            Application.logMessageReceived += OnRecLog;
        }

        private void OnDestroy()
        {
            Application.logMessageReceived -= OnRecLog;
            File.WriteAllText(_cacheFile, JsonUtility.ToJson(new JsonWrap() { KeyWords = KeyWords },true),Encoding.UTF8);
        }

        private void OnRecLog(string condition, string stackTrace, LogType type)
        {
            if (KeyWords.Length<=0)
                return;

            for (int i = 0; i < KeyWords.Length; i++)
            {
                MatchKeyWord(condition, KeyWords[i]);
            }
        }

        private void MatchKeyWord(string log,PausePoint pp)
        {
            if (pp==null || string.IsNullOrEmpty(pp.Key))
                return;

            switch (pp.Type)
            {
                case KeyWordsType.Start:
                    if (log.StartsWith(pp.Key))
                    {
                        EditorApplication.isPaused = true;
                    }
                    break;
                case KeyWordsType.Contain:
                    if (log.Contains(pp.Key))
                    {
                        EditorApplication.isPaused = true;
                    }
                    break;
                case KeyWordsType.End:
                    if (log.EndsWith(pp.Key))
                    {
                        EditorApplication.isPaused = true;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
#endif

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值