Well. I’m working on an audio visualizer/player and I have one big issue I cant solve: Building
The thing is I used code from the UnityEditor dll to open a file browser for loading audio files.
My problem when building is, that I get an error ( error CS0246: The type or namespace name `UnityEditor’ could not be found. Are you missing a using directive or an assembly reference? ).
I was lurking arround here to find an answer and found a thread where someone fixed it by putting the code into an folder named Editor. I did this and now my script can reference the script using UnityEditor because its in another assembly.
So how do I fix the building error or get a working reference to the Assambly-CSharp-Editor?
Builds arnt supposed to use the editor namespace. The editor namespace exists only to provide functionality that you can use to make it easier to make your program, it shouldn’t be a part of your program.
Generally, I’ve found that if I keep my editor code properly seperated in the editor folder, game code doesn’t interact with editor code and everything builds fine.
However, cases where I’m working quickly or being lazy can often quickly be remedied by using pre-processor directives:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class MyClass : MonoBehaviour {
#if UNITY_EDITOR
[SomeEditorAttribute]
#endif
public float someField;
}
See, the UNITY_EDITOR is a provides preprocessor constant that allows you to specify code that should only be compiled in the editor. That way, builds will ignore it.
1 Like