封装 UnityEngine.Debug.Log 为Dll
来源:互联网 发布:艾德思奇调价软件 编辑:程序博客网 时间:2024/06/06 09:38
在游戏发布时,有很多东西需要进行优化 ,消耗性能的东西 能减少就减少。
UnityEngine.Debug.Log 是很消耗性能的操作,即使我们选择的是Release 版本发布,这个函数仍然会执行并且输出Log ,不仅影响性能,还有可能泄漏隐私。所以我们在发布上线时会关掉Log。
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的消耗。
可以看到 在点击按钮时,CPU 出现了一个小 波峰。 FPS从1000 掉到了 250 。
点击该波峰,定位到 对应的函数 ,可以看到 输出Log 占用了 84.8% 的CPU(当前)。
赶紧把它干掉吧。
我们来封装一个 LOG,编译成DLL,可以自己控制是否输出Log。
首先用MonoDevelop 新建一个 Library 项目。
右键References , 引用 UnityEngine.dll 。
删除默认的 System 。
添加以下代码:
/************************** * 文件名: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
找到生成的 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;
阅读全文
0 0
- 封装 UnityEngine.Debug.Log 为Dll
- 封装 UnityEngine.Debug.Log 为Dll ,游戏发布关闭 Log 减少性能消耗
- UIFramework之UnityEngine.Debug封装
- 封装Debug.Log 游戏发布关闭 Log 减少性能消耗
- Unity3D 封装自己的Debug类库dll
- debug log
- debug log
- VS.net封装asp为Dll
- MFC 封装C++类为dll
- 对话框使用ocx控件封装为dll
- VB封装Excel_VBA代码为Dll
- log封装
- Unity3d 加载UnityEngine.UI.dll失败的解决方案(2017.2)
- DLL封装
- DLL封装
- DLL封装
- dll封装
- 封装DLL
- Android开发之通过蓝牙耳机实现讯飞语音识别的功能
- 文章标题
- 【深度学习】Ubuntu环境下Tensorflow的安装以及与Pycharm的相互配置
- JSP之Cookie与Session
- 警告: A docBase E:/Tomcat/webapps/Test inside the host appBase has been specified, and will be ignored
- 封装 UnityEngine.Debug.Log 为Dll
- dubbo-3 使用maven构建dubbo可执行jar包
- 1047. 编程团体赛(20)
- 给seo外链员的一些忠告
- NRF52832开发:GPIO驱动
- 银行企业建设容器持久化存储的必要性以及难点剖析
- Mysql分区
- java实现amr转mp3
- 《Edge Boxes: Locating Object Proposals from Edges》读后感