今天测试了下Unity的日志输出方法发现Debug.LogFormat在release模式下相当于直接注释了那行代码,而自己写的空方法则不会有这样的优化;测试代码如下:
public class LogTest : MonoBehaviour
{
int testCount2 = 0;
int testCount = 0;
public int Count { get { testCount++;return testCount; } }
public int Count2 { get { testCount2++;return testCount2; } }
// Use this for initialization
void Start()
{
Debug.LogFormat("Test Start:{0}", Count);//release 模式下testCount计数不会增加,说明不会执行这行代码!!!说明编译时这行代码相当于注释掉了
MyLogFormat("Test Start 2:{0}", Count2);//release模式下也会增加计数,说明编译本身不会优化空方法
}
private void OnGUI()
{
GUILayout.Space(100);
GUILayout.Label("TestCount:" + testCount);
GUILayout.Label("TestCount2:" + testCount2);
}
void MyLogFormat(string fmt,params object[] args)
{
}
}
测试结果在release模式下
TestCount:0
TestCount2:1