Console.WriteLine vs Print

前端 未结 1 1438
太阳男子
太阳男子 2021-01-20 19:37

I know this might sound like a really dumb question, but I am rather confused and can only find answers to the question \'WriteLine vs debug.log\' instead of this one. I ori

相关标签:
1条回答
  • 2021-01-20 20:16

    For Unity code, I recommend using the static methods in the Debug class, such as Debug.Log, Debug.LogFormat, etc.

    There are other variations on these methods such as Debug.LogWarning and Debug.LogError which you can find listed in the first link above. In the Editor you can use the buttons above the log panel to filter the log to only show the messages you want, for example errors only if there is too much noise from your other log messages.

    Unity's MonoBehaviour.print method is a thin wrapper around Debug.Log that has two drawbacks:

    • It's missing some of the features of the above methods, such as the ability to pass a context argument that ties the debug message to the object in the Unity Editor, and the ability to filter by log type in the Editor.
    • It's a method of MonoBehaviour, so you can only use it in classes derived from that. The Debug methods can be used in any of your Unity code.

    It's also worth noting that all of these log methods are available in a runtime build as well as in the Editor. When they are called in a build, the log messages are available in the MyAppName_Data/output_log.txt file, where MyAppName is the name of your executable.

    Be careful to not leave too many logging calls in your code when you make a runtime build, especially in methods that are called in every frame like your Update methods. You can use Application.isEditor to test whether you're running in the editor or not.

    It's also possible to use platform dependent compilation for this. This saves a bit of space at runtime, but it is trickier. For example, you might think that UNITY_EDITOR means you're running in the Editor, but it is also defined in runtime builds!

    0 讨论(0)
提交回复
热议问题