封装 UnityEngine.Debug.Log 为Dll ,游戏发布关闭 Log 减少性能消耗
2015-05-08 16:19
661 查看
本文参考雨松Mono的文章:
Mono介绍的是Mac 上Console 编译DLL的方法,本文是在 Win7 系统使用MonoDeveloper 编译。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
在游戏发布时,有很多东西需要进行优化 ,消耗性能的东西 能减少就减少。
UnityEngine.Debug.Log 是很消耗性能的操作,即使我们选择的是Release 版本发布,这个函数仍然会执行并且输出Log ,不仅影响性能,还有可能泄漏隐私。所以我们在发布上线时会关掉Log。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
比如下面这段代码:
运行之后在界面上会有 一个按钮 ,点击之后会输出 Log 。
我们打开 Profiler 来监测 CPU的消耗。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
可以看到 在点击按钮时,CPU 出现了一个小 波峰。 FPS从1000 掉到了 250 。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
点击该波峰,定位到 对应的函数 ,可以看到 输出Log 占用了 84.8% 的CPU(当前)。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
赶紧把它干掉吧。
我们来封装一个 LOG,编译成DLL,可以自己控制是否输出Log。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
首先用MonoDevelop 新建一个 Library 项目。
右键References , 引用 UnityEngine.dll 。
右键 SNKDebug项目,点击Option 打开设置界面,设置.Net 为 2.0 。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
添加以下代码:
把解决方案配置修改为 Release , 然后点击菜单栏 Build - Build ALL
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
找到生成的 SNKDebug.dll ,拖到Unity 中,然后像下面使用
如果需要关闭Log ,只需要设置
工程打包下载:
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
http://www.xuanyusong.com/archives/2782
Mono介绍的是Mac 上Console 编译DLL的方法,本文是在 Win7 系统使用MonoDeveloper 编译。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
在游戏发布时,有很多东西需要进行优化 ,消耗性能的东西 能减少就减少。
UnityEngine.Debug.Log 是很消耗性能的操作,即使我们选择的是Release 版本发布,这个函数仍然会执行并且输出Log ,不仅影响性能,还有可能泄漏隐私。所以我们在发布上线时会关掉Log。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
比如下面这段代码:
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } void OnGUI() { if (GUILayout.Button("Log")) { Debug.Log("test Log"); Debug.LogWarning("test LogWarning"); Debug.LogError("test LogError"); } } // Update is called once per frame void Update () { } }
运行之后在界面上会有 一个按钮 ,点击之后会输出 Log 。
我们打开 Profiler 来监测 CPU的消耗。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
可以看到 在点击按钮时,CPU 出现了一个小 波峰。 FPS从1000 掉到了 250 。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
点击该波峰,定位到 对应的函数 ,可以看到 输出Log 占用了 84.8% 的CPU(当前)。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
赶紧把它干掉吧。
我们来封装一个 LOG,编译成DLL,可以自己控制是否输出Log。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
首先用MonoDevelop 新建一个 Library 项目。
右键References , 引用 UnityEngine.dll 。
右键 SNKDebug项目,点击Option 打开设置界面,设置.Net 为 2.0 。
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
添加以下代码:
/************************** * 文件名:SNKDebuger.cs; * 文件描述:Unity Log的封装; * 创建日期:2015/05/08; * Author:陈鹏; ***************************/ using UnityEngine; using System.Collections; public class SNKDebuger { static public bool EnableLog = true; static public void Log(object message) { Log(message,null); } static public void Log(object message, Object context) { if(EnableLog) { Debug.Log(message,context); } } static public void LogError(object message) { LogError(message,null); } static public void LogError(object message, Object context) { if(EnableLog) { Debug.LogError(message,context); } } static public void LogWarning(object message) { LogWarning(message,null); } static public void LogWarning(object message, Object context) { if(EnableLog) { Debug.LogWarning(message,context); } } }
把解决方案配置修改为 Release , 然后点击菜单栏 Build - Build ALL
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
找到生成的 SNKDebug.dll ,拖到Unity 中,然后像下面使用
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { //SNKDebuger.EnableLog = false; } void OnGUI() { if (GUILayout.Button("Log")) { SNKDebuger.Log("test Log"); SNKDebuger.LogWarning("test LogWarning"); SNKDebuger.LogError("test LogError"); } } // Update is called once per frame void Update () { } }
如果需要关闭Log ,只需要设置
SNKDebuger.EnableLog = false;
工程打包下载:
http://pan.baidu.com/s/10LUrW
文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/
相关文章推荐
- 封装Debug.Log 游戏发布关闭 Log 减少性能消耗
- 封装 UnityEngine.Debug.Log 为Dll
- UIFramework之UnityEngine.Debug封装
- UIFramework之UnityEngine.Debug封装
- Unity游戏开发性能优化(Mesh优化)
- 【Unity基础知识②】创建C#脚本、Debug.log输出语句
- Unity 游戏框架搭建 (八) 减少加班利器-QLog
- 如何有效提升Unity Gear VR游戏性能
- 【unity小技巧之一】使Debug.Log支持颜色等富文本信息
- Hi,腾讯WeTest联合Unity官方打造的性能分析工具UPA,今日全新发布!
- 总结使用Unity 3D优化游戏运行性能的经验
- 利用 Profiler(性能分析器)查看游戏在移动真机上的性能消耗数据
- [Unity3D]总结使用Unity 3D优化游戏运行性能的经验
- 在Release版本中如何关闭Debug版本中的log
- 总结使用Unity 3D优化游戏运行性能的经验
- Unity 游戏框架搭建 (九) 减少加班利器-QConsole
- 如何提高Unity Gear VR游戏性能
- Unity封装dll教程整理
- UnityEngine.UI.dll is in timestamps but is not known in assetdatabase
- unity游戏开发 发布android 游戏