一个Log系统,客户端运行起来,排除那些可以输出(编辑器文件)

本文介绍了一个Unity项目中实现的日志管理系统,通过菜单项快速控制不同类型的日志输出,并提供了示例代码展示如何使用该系统记录各类信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不说,直接带码!


#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;



public class MenuTools : MonoBehaviour
{
    private const string rootDir = "Tools/日志开关(目前在运行状态要在打开一次)";

    private const string channelLxf = rootDir + "/LXF";

    private const string channelLog = rootDir + "/默认日志";
    private const string channelModule = rootDir + "/模块";
    private const string channelNetWork = rootDir + "/网络";
    private const string channelBattle = rootDir + "/战斗数据";
    private const string channelResource = rootDir + "/资源";
    private const string channelAttribute = rootDir + "/属性";
    private const string channelProcess = rootDir + "/游戏流程";
    private const string channelAlert = rootDir + "/报错";
    private const string channelFile = rootDir + "/写文件";

    /// <summary> 设置开关 </summary>
    /// <param name="path"></param>
    /// <param name="def"></param>
    private static void SetFlag(string path, MaskDef def)
    {
        bool flag = Menu.GetChecked(path);
        if (flag)
        {
            LLog.Mask = BitMask.Disable(LLog.Mask, def);
            Menu.SetChecked(path, false);
        }
        else
        {
            LLog.Mask = BitMask.Enable(LLog.Mask, def);
            Menu.SetChecked(path, true);
        }
    }

    #region 开发人员

    [MenuItem(channelJc)]
    static void ChannelJc()
    {
        SetFlag(channelJc, MaskDef.Jc);
    }

    [MenuItem(channelJc, true)]
    static bool ChannelJcCheck()
    {
        Menu.SetChecked(channelJc, LLog.IsEnabled(MaskDef.Jc));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelLxf)]
    static void ChannelLxf()
    {
        SetFlag(channelLxf, MaskDef.Lxf);
    }

    [MenuItem(channelLxf, true)]
    static bool ChannelLxfCheck()
    {
        Menu.SetChecked(channelLxf, LLog.IsEnabled(MaskDef.Lxf));
        return true;
    }

    #endregion

    #region 系统日志
    //-----------------------------------------------------------------//
    [MenuItem(channelLog, false, 20)]
    static void ChannelLog()
    {
        SetFlag(channelLog, MaskDef.Log);
    }

    [MenuItem(channelLog, true)]
    static bool ChannelLogCheck()
    {
        Menu.SetChecked(channelLog, LLog.IsEnabled(MaskDef.Log));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelModule, false, 20)]
    static void ChannelModule()
    {
        SetFlag(channelModule, MaskDef.Module);
    }

    [MenuItem(channelModule, true)]
    static bool ChannelModuleCheck()
    {
        Menu.SetChecked(channelModule, LLog.IsEnabled(MaskDef.Module));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelNetWork, false, 20)]
    static void ChannelNetWork()
    {
        SetFlag(channelNetWork, MaskDef.NetWork);
    }

    [MenuItem(channelNetWork, true)]
    static bool ChannelNetWorkCheck()
    {
        Menu.SetChecked(channelNetWork, LLog.IsEnabled(MaskDef.NetWork));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelBattle, false, 20)]
    static void ChannelBattle()
    {
        SetFlag(channelBattle, MaskDef.Battle);
    }

    [MenuItem(channelBattle, true)]
    static bool ChannelBattleCheck()
    {
        Menu.SetChecked(channelBattle, LLog.IsEnabled(MaskDef.Battle));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelResource, false, 20)]
    static void ChannelResource()
    {
        SetFlag(channelResource, MaskDef.Resource);
    }

    [MenuItem(channelResource, true)]
    static bool ChannelResourceCheck()
    {
        Menu.SetChecked(channelResource, LLog.IsEnabled(MaskDef.Resource));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelAttribute, false, 20)]
    static void ChannelAttribute()
    {
        SetFlag(channelAttribute, MaskDef.Attribute);
    }

    [MenuItem(channelAttribute, true)]
    static bool ChannelAttributeCheck()
    {
        Menu.SetChecked(channelAttribute, LLog.IsEnabled(MaskDef.Attribute));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelProcess, false, 20)]
    static void ChannelProcess()
    {
        SetFlag(channelProcess, MaskDef.Process);
    }

    [MenuItem(channelProcess, true)]
    static bool ChannelProcessCheck()
    {
        Menu.SetChecked(channelProcess, LLog.IsEnabled(MaskDef.Process));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelAlert, false, 20)]
    static void ChannelAlert()
    {
        SetFlag(channelAlert, MaskDef.Alert);
    }

    [MenuItem(channelAlert, true)]
    static bool ChannelAlertCheck()
    {
        Menu.SetChecked(channelAlert, LLog.IsEnabled(MaskDef.Alert));
        return true;
    }

    //-----------------------------------------------------------------//
    [MenuItem(channelFile, false, 20)]
    static void ChannelFile()
    {
        SetFlag(channelFile, MaskDef.File);
    }

    [MenuItem(channelFile, true)]
    static bool ChannelFileCheck()
    {
        Menu.SetChecked(channelFile, LLog.IsEnabled(MaskDef.File));
        return true;
    }

    #endregion

}

#endif

使用:

private float index = 0;
    void Update()
    {
        index += Time.deltaTime;
        if (index > 2)
        {
            index = 0;
            LLog.Log("123123");
            LLog.Log("123123123", LLog.LogColor.Yellow);

            LLog.Alert("警告");
            LLog.AllPlatform("所有平台");
            LLog.Attribute("属性");
            LLog.Battle("战斗数据");
            LLog.Error("错误");
            LLog.Module("模块");
            LLog.Process("游戏流程");
            LLog.NetWork("网络");
            LLog.Resource("资源");

            LLog.Jc.Log("jc");
            LLog.Lxf.Log("lxf");
        }
    }

后续会写把LOG输出到屏幕上,及在屏幕上控制过滤!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值