Unity 打印log时,使用StringBuilder比String好,String会产生垃圾,使用StringBuilder能避免String产生垃圾

using System;

using System.Diagnostics;

using System.Reflection;

using System.Text;

using UnityEngine;

public class Log

{

// private static StringBuilder _builder = new StringBuilder(1024);

public static void I(string tag, string format, params object[] args)

{

UnityEngine.Debug.Log(buildString(tag, format, args));

}

复制代码
public static void W(string tag, string format, params object[] args)
{
    UnityEngine.Debug.LogWarning(buildString(tag, format, args));
}

public static void E(string tag, string format, params object[] args)
{
    UnityEngine.Debug.LogError(buildString(tag, format, args));
}

private static StringBuilder buildString(string tag, string format, params object[] args)
{
    StringBuilder _builder = new StringBuilder(1024);
    _builder.Clear();
    _builder.Append("Snake_");
    _builder.Append(tag);
    _builder.Append(": ");
    if (args != null && args.Length > 0)
    {
        _builder.AppendFormat(format, args);
    }
    else
    {
        _builder.Append(format);
    }

    return _builder;
}

}

namespace Banma.Unity.Log

{

public static class Xlog

{

private const string c_str_project_name = "Snake";

复制代码
    public static void Log(this MonoBehaviour mono, string _str_log, bool _b_is_error = false)
    {
        // 获取传入实例所属类的名称
        string className = mono.GetType().Name;

        // 获取当前执行的函数的名称
        string methodName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name;
        if (_b_is_error)
        {
            UnityEngine.Debug.LogError($"【{c_str_project_name}】/【{className}】/【{methodName}】:){_str_log}");
        }
        else
        {
            UnityEngine.Debug.Log($"【{c_str_project_name}】/【{className}】/【{methodName}】:){_str_log}");
        }
    }

    public static void Log(string _str_log, bool _b_is_error = false)
    {
        StackTrace stackTrace = new StackTrace();
        StackFrame[] stackFrames = stackTrace.GetFrames();

        if (stackFrames.Length >= 2)
        {
            // 获取调用堆栈的第一个方法(当前方法是第二个)
            StackFrame callerFrame = stackFrames[1];
            MethodBase callerMethod = callerFrame.GetMethod();
            Type callerType = callerMethod.DeclaringType;

            if (_b_is_error)
            {
                // 打印调用类和函数的信息
                UnityEngine.Debug.LogError($"【Snake】/【{callerType.Name} 】/【{callerMethod.Name}】:){_str_log}");
            }
            else
            {
                // 打印调用类和函数的信息
                UnityEngine.Debug.Log($"【Snake】/【{callerType.Name} 】/【{callerMethod.Name}】:){_str_log}");
            }
        }
    }
}

}

相关推荐
一个笔记本4 小时前
godot log | 修改main scene
游戏引擎·godot
nnsix6 小时前
Unity PicoVR开发 实时预览Unity场景 在Pico设备中(串流)
unity·游戏引擎
一只一只12 小时前
Unity之UGUI Button按钮组件详细使用教程
unity·游戏引擎·ugui·button·ugui button
神米米14 小时前
Maya快速安装UE4 布料权重绘制插件PhysX导出apx
游戏引擎·ue4·maya
WarPigs15 小时前
Unity阴影
unity·游戏引擎
一只一只15 小时前
Unity之Invoke
unity·游戏引擎·invoke
技术小甜甜17 小时前
【Godot】【入门】信号系统从 0 到 1(UI/玩法彻底解耦的通用写法)
ui·游戏引擎·godot
技术小甜甜18 小时前
【Godot】【入门】节点生命周期怎么用(避免帧循环乱写导致卡顿的范式)
游戏引擎·godot
tealcwu18 小时前
【Unity踩坑】Simulate Touch Input From Mouse or Pen 导致检测不到鼠标点击和滚轮
unity·计算机外设·游戏引擎
ThreePointsHeat18 小时前
Unity WebGL打包后启动方法,部署本地服务器
unity·游戏引擎·webgl