I’m working on an improved Debug console, but am unable to call any methods on it. Here is a simplified version of the class:
using Assets.Editor;
using UnityEditor;
using UnityEngine;
public class Logger : EditorWindow
{
[MenuItem("Window/Logger")]
public static void Init()
{
Logger window = (Logger)GetWindow(typeof(Logger), false, "Logger");
window.position = new Rect(window.position.xMin + 100f, window.position.yMin + 100f, 200f, 400f);
}
public static void Log(string msg)
{
}
}
If I have a class that calls Logger.Log(“something”), unity reports that ‘Logger’ does not exist in the current context. Visual Studio however does not find an error with any of my code. This code will only need to work when in the Unity editor.
So how can I access Logger from standard classes?
Since you guess an assembly issue this class is compiled in a seperate assembly? right? Where do you have this assembly in your project? And where do you try to use it? You know that this is an editor script and therefore can only be used in the editor. The class won’t exist at runtime since the assembly isn’t included (since it’s an editor assembly).
I was able to get this working using a surrogate class within the main assembly.
- A LoggerUI class (Logger in the original question) is in the editor assembly, and cannot be accessed by any other classes.
- A static Logger class (The surrogate) is in the main assembly. It has has logging methods, and saves all logs in a list.
- The LoggerUI class then calls GetList() on the Logger class every frame (because the editor assembly is able to access the main one), to update and draw the logged lines.
The list is only updated once every frame. Although this will result in good performance (you can call Logger.Log() many times per frame), when the game crashes, the list might not be as up to date as it should be.