原因

1.测试跑IOS的时候,游戏卡死,但是却无法查看日志,因为必须将手机插在mac机器上,用xcode才能实时看日志。

2.Android虽然卡死后,可以再插上电脑,通过AndroidStudio查看已经输出的日志,但是也会遇到看不到日志,或者手机插上无法被ADB侦测的问题。

目标

1.让日志可以输出到3个地方:(1)控制台(2)界面 (3)文件

2.能输出游戏指定模块的日志

3.能输出特定级别的日志,如error,warning等

代码

注:UI组件YYUIWrapContent未贴上,如果需要使用,你们可以不使用输入到ui的功能。

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;public class DebugLog
{public enum OutputModule{eNone,ePve = 1,eWonderland = 2,eTrial = 3,eMainScene = 4,eUI = 5,eOther = 6,ePveBase = 7,eMiniGame = 8,eGM = 9,eProxy = 10,eLoader = 11,eBattle = 12,eResLoad = 13,eNetWork = 14,eStory = 15,ePlatform = 16,}public enum OutputTarget{eNone,eConsole = 1,eUI = 2,eFile = 3,}public enum OutputLevel{eNone,eLog = 1,eLogWaning = 2,eLogError = 3,}//全局控制是否开启日志public static bool isOpenLog;//控制输出的模块private static int outputModule;//控制输出的目标private static int outputTarget;//控制输出的等级private static int outputLevel;//文件路径private static string filePath;//ui显示最大缓存的日志数量private const int MAX_LOG_NUM = 1000;private static List<string> logList = new List<string>();//ui组件,实现循环滚动private static YYUIWarpContent wrapContent;public static void Init(YYUIWarpContent uiContent){Application.logMessageReceived += LogCallback;//获取文件路径filePath = GetPath("LJSLogFile.txt");//删除已存在的文件if (File.Exists(filePath)){File.Delete(filePath);}//获取ui组件wrapContent = uiContent;//是否要输出日志isOpenLog = true;//指定要输出哪些模块SwitchModule(OutputModule.ePve, true);SwitchModule(OutputModule.eWonderland, true);SwitchModule(OutputModule.eTrial, true);SwitchModule(OutputModule.eMainScene, true);SwitchModule(OutputModule.eUI, true);SwitchModule(OutputModule.eOther, true);SwitchModule(OutputModule.ePveBase, true);SwitchModule(OutputModule.eMiniGame, true);SwitchModule(OutputModule.eGM, true);SwitchModule(OutputModule.eProxy, true);SwitchModule(OutputModule.eLoader, true);SwitchModule(OutputModule.eBattle, true);SwitchModule(OutputModule.eResLoad, true);SwitchModule(OutputModule.eNetWork, true);SwitchModule(OutputModule.eStory, true);SwitchModule(OutputModule.ePlatform, true);//指定要输出的目标SwitchTarget(OutputTarget.eConsole, true);SwitchTarget(OutputTarget.eUI, true);//SwitchTarget(OutputTarget.eFile, true);//指定输出的级别SwitchLevel(OutputLevel.eLog, true);SwitchLevel(OutputLevel.eLogWaning, true);SwitchLevel(OutputLevel.eLogError, true);}public static void LogCallback(string condition, string stackTrace, LogType type){string result = "";if (type == LogType.Error || type == LogType.Log || type == LogType.Warning){result = condition;}else{result = string.Format("{0}\n{1}", condition, stackTrace);}OutputUI(result);OutputFile(result);}public static void Log(OutputModule type, string message){if (!IsOuputLog(type, OutputLevel.eLog)){return;}message = ModifyLog(message, 0);OutputConsole(message, 0);}public static void LogWarning(OutputModule type, string message){if (!IsOuputLog(type, OutputLevel.eLogWaning)){return;}message = ModifyLog(message, 1);OutputConsole(message, 1);}public static void LogError(OutputModule type, string message){if (!IsOuputLog(type, OutputLevel.eLogError)){return;}message = ModifyLog(message, 2);OutputConsole(message, 2);}private static void OutputConsole(string message, int type){if(!IsOutputTarget(OutputTarget.eConsole)){return;}if (type == 0){Debug.Log(message);}else if (type == 1){Debug.LogWarning(message);}else if (type == 2){Debug.LogError(message);}}private static void OutputFile(string message){if (!IsOutputTarget(OutputTarget.eFile)){return;}FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);sw.WriteLine(message);sw.Close();fs.Close();}private static void OutputUI(string message){if (!IsOutputTarget(OutputTarget.eUI)){return;}if(logList.Count >= MAX_LOG_NUM){logList.Clear();}logList.Add(message);wrapContent.Init(logList.Count, (obj, index) =>{Text text = obj.GetComponent<Text>();text.text = logList[index];}, null, false);}private static bool IsOuputLog(OutputModule type, OutputLevel level){bool result = false;bool rule1 = (outputModule & (1 << (int)type)) > 0 ? true : false;bool rule2 = isOpenLog;bool rule3 = (outputLevel & (1 << (int)level)) > 0 ? true : false;if (rule1 && rule2 && rule3){result = true;}return result;}private static bool IsOutputTarget(OutputTarget target){bool result = false;result = (outputTarget & (1 << (int)target)) > 0 ? true : false;return result;}private static string ModifyLog(string message, int type){string result = "";switch (type){case 0:{result += "common : ";}break;case 1:{result += "warning : ";}break;case 2:{result += "error : ";}break;}result += message;return result;}private static string GetPath(string name){string path = "";switch (Application.platform){case RuntimePlatform.Android:case RuntimePlatform.IPhonePlayer:{path = Application.persistentDataPath + "/" + name;}break;case RuntimePlatform.OSXEditor:case RuntimePlatform.WindowsEditor:case RuntimePlatform.WindowsPlayer:{path = Application.dataPath + "/../" + name;}break;}return path;}public static void SwitchModule(OutputModule module, bool isOpen){if (isOpen){outputModule = outputModule | (1 << (int)module);}else{outputModule = outputModule & (~(1 << (int)module));}}public static void SwitchTarget(OutputTarget target, bool isOpen){if(isOpen){outputTarget = outputTarget | (1 << (int)target);}else{outputTarget = outputTarget & (~(1 << (int)target));}}public static void SwitchLevel(OutputLevel level, bool isOpen){if (isOpen){outputLevel = outputLevel | (1 << (int)level);}else{outputLevel = outputLevel & (~(1 << (int)level));}}
}

Unity 自定义Log系统相关推荐

  1. [Unity] 自定义日志系统 解决Unity Log的痛点

    当前Unity日志存在的问题: 1.日志打印没有时间 2.日志文件中Log.Warning.和Error区分度不大 3.长时间没有清理容易产生动辄几十MB,几十万行的日志文件 本日志系统区别于Unit ...

  2. Unity自定义日志系统

    Unity的系统日志算是很好用,但当程序很大时,就会比较乱,所以在项目中我们往往要对日志系统进行重新封装使用. 一.日志系统封装 如下代码,我们可以通过m_Log来控制是否打印日志,或对输出的日志加入 ...

  3. 利用 CocoaLumberjack 搭建自己的 Log 系统

    2019独角兽企业重金招聘Python工程师标准>>> 先说下需求,我理想中的 Log 系统需要: 可以设定 Log 等级 可以积攒到一定量的 log 后,一次性发送给服务器,绝对不 ...

  4. 使用 ADB LogCat 查看在Android真机上 Unity debug.log 输出日志

    控制台窗口输入指令格式为:[adb] logcat [<option>] ... [<filter-spec>] ... 其中的 [<option>] 的指令都有: ...

  5. gradle学习(19)-log系统

    2019独角兽企业重金招聘Python工程师标准>>> 1.log信息的分类 除了常用的 debug,info,warning,error ,gradle自己特有的quiet和lif ...

  6. unity简单技能系统

    unity简单技能系统 类类型概览 CharacterSkillManager      角色技能管理器 挂载在角色 持有SkillData与释放器 通过释放器进行技能释放 SkillDeployer ...

  7. Unity简单商城系统,用SQLite数据库保存/加载数据

    Unity简单商城系统案例 流程 最后效果展示 1. 创建项目并导入SQLite需要的dll文件 2. 创建数据库表(玩家表和商店表) 3. Singleton 单例脚本 4. 封装SQLite数据库 ...

  8. Unity引擎音效系统简介

    Unity引擎音效系统简介 音频文件设置选项: Force To Mono:多声道转单声道 Normalize:当强制转为单声道时,混合过程中被标准化. Load In Background:在后台加 ...

  9. Unity自定义UI组件(六)日历、日期拾取器

    前言 考虑到工业项目中可能会利用到类似日历的工具,就比如选取某个时间节点,所以我结合UGUI源码开发了日历工具和日期拾取器工具,简单易用,接口齐全,可中文显示,外观可自定义.只需要导入脚本,即可在Hi ...

最新文章

  1. RabbitMQ 延迟队列,太实用了!
  2. WINCE6.0+S3C2443的启动过程---eboot6
  3. BZOJ 1269: [AHOI2006]文本编辑器editor Splay
  4. 正则表达式的威力--轻松消除HTML代码
  5. 网络与IO知识扫盲(七):仿照Netty工作架构图,手写多路复用模型
  6. js中如何删除json对象的某一个选项
  7. 单例模式到Java内存模型
  8. eclipse中tomcat服务器locations不能修改,解决eclipse中Tomcat服务器的server location选项不能修改的问题...
  9. 计算机网络————P2 标准化工作及相关组织
  10. nologging mysql_oraclenologgingoperation
  11. 一、瞰景Smart3D软件介绍
  12. 16个大数据常见案例分享,看完别说还不懂大数据!
  13. java-等差等比求和
  14. 【平衡小车制作】(四)陀螺仪MPU6050(超详解)
  15. HTML 的属性 lang=“en“ 语言设置为中文
  16. 2019滴滴java面试总结 (包含面试题解析)
  17. 爬取起点小说网免费小说
  18. python比较运算符中大于等于且小于等于的表达方式
  19. java 提取违反顺序_oracle 中 java.sql.SQLException: ORA-01002: 提取违反顺序
  20. 大数据必备技能_大数据需要具备的5种必备技能

热门文章

  1. Linux 环境下安装 GitLab 与配置
  2. IT结合测试时,准备数据的注意事项(之二:表之间的关系)。
  3. Qt编译错误“GL/gl.h:No such file or directory”的解决方法
  4. python中日志logging模块和异常捕获traceback模块的使用
  5. Android 使用JSON格式与服务器交互 中文乱码问题解决
  6. Access to the path ‘‘ is denied.解决方案
  7. Vue-Access-Control:前端用户权限控制解决方案
  8. maven工程找不到jar包(依赖)的解决方法
  9. mysql中CONCAT值为空的问题解决办法
  10. Vm下安装centos7.0时电脑进入黑屏的解决方法(选择Install Centos 7或者是Test this media install Centos 7以后,虚拟机屏幕立马就进入黑屏状态)