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}");
            }
        }
    }
}

}

相关推荐
weixin_409383128 小时前
godot 击败敌人后增加经验的脚本
游戏引擎·godot
海清河晏11118 小时前
数据结构 | 单链表
数据结构·unity·dreamweaver
mxwin20 小时前
Unity URP 下 MatCap 技术详解 无视光照环境的卡通与质感渲染方案
unity·游戏引擎
weixin_4093831221 小时前
godot 获取敌人位置自动发射子弹 旋转枪口
游戏引擎·godot
CDN3601 天前
游戏盾日志看不到攻击?日志开启与上报问题排查
游戏·网络安全·游戏引擎
山檐雾2 天前
OctreeNode
unity·c#·八叉树
WarPigs2 天前
Unity协程返回值的解决方案
unity·游戏引擎
WarPigs2 天前
Unity单例笔记
unity·游戏引擎
风酥糖3 天前
Godot游戏练习01-第24节-多人游戏暂停菜单,游戏优化
游戏·游戏引擎·godot
Allen74743 天前
ComfyUI 自动化生产 3D资产 工作流笔记
图像处理·opencv·unity·自然语言处理·3d模型生成·confyui