- Home /
Alternatives to Debug.Log() for Non-MonoBehaviour Classes
I'm working on a data outputter package to use in several studies. I'd like to be able to break away from using UnityEngine;
for it. The only problem I've run into is how to get debug messages back into Unity. They would be quite useful in the player logs for error reports and debugging to be in the context of game events logged.
Currently, I've just been throwing WarningExceptions for minor issues and those come through Unity logs like any other Exception. Is there anything similar for plain messages?
Along with that any best-practices for Non-MonoBehaviour scripts designed for use with Unity would be appreciated. :)
Answer by Bunny83 · Sep 21, 2021 at 01:22 AM
I'm not quite sure I understand your question. Debug.Log has absolutely nothing to do with MonoBehaviours ^^. Unity's Debug class is a static class and the Log method is a static method. It's even thread safe. So it can be used from anywhere as long as you are inside a Unity project of course.
Keep in mind that you never need a using statement in order to use a certain type from that namespace. You can always use the fully qualified class name
UnityEngine.Debug.Log("Hello there");
If you're looking for a way to abstract the concept of logging so you're not bound to Unity, in that case yo uhave to use your own logger abstraction. Note that Unity actually ships with a logger abstraction system already. See the Logger class. However since you said you don't want to rely on Unity it wouldn't make much sense to use Unity's system ^^. As I said you can always implement your own interfaces / abstraction. Of course when you want the logs to show up in Unity you would need a Unity logger implementation of your interface anyways.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is there a logging method, similar to C#'s Console.WriteLine? (what I want is the {N}) 3 Answers
Question on Strange Debug.Log() Behavior 0 Answers
Debug.Log a gameobject in C ? 2 Answers