打包尽量别输出Log,别拼接字符串

Unity EDITOR_LOG.输出会产生GC.拼接字符串也会产生.开发时用宏log.打包时去掉宏
在这里插入图片描述

using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Text;

public class ModifyScriptEditor : EditorWindow
{
    [MenuItem("Tools/使用宏Log")]
    public static void ShowGUIMacro()
    {
        EditorWindow.GetWindow<ModifyScriptGUI>(false);
    }
}
public class ModifyScriptGUI : EditorWindow
{
    private string mModuleStr = "";
    void OnGUI()
    {
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical("Box");
        {
            mModuleStr = EditorGUILayout.TextField("模块路径:", mModuleStr);
        }
        ShowExecute();
    }
    private void GetPathForDebugs(string pPath)
    {
        mDicTxts.Clear();mCates.Clear();
        string mProject = Application.dataPath.Replace("/Assets", "");
        if (string.IsNullOrEmpty(pPath)) pPath = "Assets/Scripts";
        var guids = AssetDatabase.FindAssets("t:Script", new string[] { pPath });
        foreach (var item in guids)
        {
            var tPath = mProject + "/" + AssetDatabase.GUIDToAssetPath(item);
            var tTxt = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(item)) as TextAsset;
            if (tTxt.text.Contains("Debug"))
            {
                mDicTxts[tPath] = tTxt.text;
                mCates.Add(tPath);
            }
        }
    }
    private Vector2 mScrollViewCommon;
    private Dictionary<string, string> mDicTxts=new Dictionary<string, string>();
    private List<string> mCates = new List<string>();
    void ShowNotifyLabel(string label)
    {
        this.ShowNotification(new GUIContent(label));
    }
    private void ShowExecute()
    {
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical("Box");
        {
            GUILayout.Label(@"脚本代码的使用,默认路径(Assets/Script),执行自己跟的模块Log输出,拼接字符串也会产生GC.开发时用宏打印log.打包时去掉宏");
            if (GUILayout.Button("执行", GUILayout.Height(27)))
            {
                GetPathForDebugs(mModuleStr);
            }
            if (mCates != null && mCates.Count > 0)
            {
                EditorGUILayout.EndVertical();
                EditorGUILayout.BeginVertical("HelpBox", GUILayout.Height(250));
                {
                    mScrollViewCommon = EditorGUILayout.BeginScrollView(mScrollViewCommon);
                    for (int i = 0; i < mCates.Count; i++)
                    {
                        var tPathKey = mCates[i];
                        if (mDicTxts.ContainsKey(tPathKey) == false) continue;
                        var tValueContent = mDicTxts[tPathKey];
                        var tName = tPathKey.Split('/')[tPathKey.Split('/').Length - 1];
                        if (GUILayout.Button("-点击给引脚本加上宏-," + tName, GUILayout.Height(20)))
                        {
                            var tLines = tValueContent.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                            var sb = new StringBuilder();
                            for (int j = 0;j < tLines.Length; j++)
                            {
                                var tLine = tLines[j];
                                if (tLine.Contains("Debug"))
                                {
                                    sb.AppendLine("#if EDITOR_LOG");
                                    sb.AppendLine(tLine);
                                    sb.AppendLine("#endif");
                                }
                                else
                                {
                                    sb.AppendLine(tLine);
                                }
                            }
                            StreamWriter tReader = new StreamWriter(tPathKey, false);
                            tReader.Write(sb.ToString());
                            tReader.Close();
                            AssetDatabase.SaveAssets();
                            mCates.Remove(tPathKey);
                            ShowNotifyLabel("执行了此脚本");
                        }
                    }
                    EditorGUILayout.EndScrollView();
                }
                if (GUILayout.Button("若太多,一口气此模块全部加宏", GUILayout.Height(27)))
                {
                    for (int i = 0; i < mCates.Count; i++)
                    { 
                        var tPathKey = mCates[i];
                        if(mDicTxts.ContainsKey(tPathKey)==false)continue;
                        var tValueContent = mDicTxts[tPathKey];
                        var tLines = tValueContent.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        var sb = new StringBuilder();
                        for (int j = 0; j < tLines.Length; j++)
                        {
                            var tLine = tLines[j];
                            if (tLine.Contains("Debug"))
                            {
                                sb.AppendLine("#if EDITOR_LOG");
                                sb.AppendLine(tLine);
                                sb.AppendLine("#endif");
                            }
                            else
                            {
                                sb.AppendLine(tLine);
                            }
                            StreamWriter tReader = new StreamWriter(tPathKey, false);
                            tReader.Write(sb.ToString());
                            tReader.Close();
                            AssetDatabase.SaveAssets();
                            mDicTxts[tPathKey] = "";
                        }
                    }
                    ShowNotifyLabel("执行了此模块");
                }
            }
        }
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值