Package UnityEngine.Debug.Log for DLL, game release close Log reduces performance consumption

Source: Internet
Author: User

This article refers to the article of Rain Pine Mono:

http://www.xuanyusong.com/archives/2782

Mono introduces the way that the console compiles DLLs on Mac, this article is compiled using Monodeveloper in the Win7 system.

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

When the game is released, there are a lot of things that need to be optimized to reduce the amount of things that can consume performance.

UnityEngine.Debug.Log is a very performance-intensive operation, even if we choose Release release, this function will still execute and output Log, not only affect performance, but also may leak privacy. So we're going to turn off log when we launch it.

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

For example, the following code:

Using unityengine;using System.collections;public class Newbehaviourscript:monobehaviour {//Use this for Initializatio Nvoid Start () {}    void Ongui ()    {        if (Guilayout.button ("Log"))        {            Debug.Log ("Test Log");            Debug.logwarning ("Test logwarning");            Debug.logerror ("Test LogError");        }    } Update is called once per framevoid update () {}}

After running, there will be a button on the interface that will output Log when clicked.

We open the Profiler to monitor CPU consumption.

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/


You can see that the CPU has a small crest when the button is clicked. FPS dropped from 1000 to 250.

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/


Click on the crest, navigate to the corresponding function, you can see the output log occupies 84.8% of the CPU (current).

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

Let's get rid of it.


Let's encapsulate a log, compile it into a DLL, and control whether or not to output the log itself.

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

First, create a new Library project with MonoDevelop.

Right-click References, reference UnityEngine.dll.


Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

Add the following code:

/************************** * file name: SNKDebuger.cs; * File Description: Unity log encapsulation; * Date Created: 2015/05/08; * Author: Chen Peng; /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);}}}


Change the solution configuration to Release, then click on the menu bar build-build all

Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

Locate the generated SNKDebug.dll, drag it into unity, and then use the

Using unityengine;using System.collections;public class Newbehaviourscript:monobehaviour {//Use this for Initializatio Nvoid 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 framevoid update () {}}

If you need to close log, you only need to set

Snkdebuger.enablelog = false;

Engineering Package Download:

Http://pan.baidu.com/s/1sjsPFC5


Article transferred from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn/

Package UnityEngine.Debug.Log for DLL, game release close Log reduces performance consumption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.