I would like to use namespaces in an EditorWindow class, but for some reason Unity doesn’t like it when I do it.
Without namespaces everything is working fine.
The following line breaks:
[MenuItem("My Menu/Item 1", priority = 2)]
public static void ShowWindow()
{
EditorWindow window = EditorWindow.GetWindow<MyAwesomeWindow>(title: "My Awesome Window");
}
I laso tried it like this:
[MenuItem("My Menu/Item 1", priority = 2)]
public static void ShowWindow()
{
EditorWindow window = EditorWindow.GetWindow<MyNamespace.MyAwesomeWindow>(title: "My Awesome Window");
}
In both case I get the following error:
Instance of MyAwesomeWindow couldn’t be created because there is no script with that name.
Is there anything I did wrong, or is this just a Unity issue?
Unfortunately the scripting classes, like EditorWindow, MonoBehaviour etc. cannot be included inside a namespace.
For other classes it works fine, e.g. you could put your custom class inside a namespace and it will work.
This is also the reason of various naming clashes that people have when downloading multiple Asset Store content into the same project, because nothing guarantees that two publishers cannot use the same class name.
Namespaces are now supported.in unity.4. I use them heavily and they work. There is one known issue. If you have a default parameter in a function within the class.then they fail.
One warning with namespaces: You can no longer use default values with namespaces or you will get above error (no class with matching filename …) as well!
So if you use namespaces, ensure to use overloads with added / varying parameters to replicate default values
So do you also use them with editor windows? Because I know they work fine with my other scripts, it just can’t find the editor window when I put that in my namespace.
Just knocked this together and it worked for me: Tested in Unity4.1.
namespace DISUnity.Editor
{
public class TEST : EditorWindow
{
[MenuItem ("Window/My Window")]
public static void Init ()
{
// Both work
TEST window = ( TEST )EditorWindow.GetWindow( typeof( TEST ) );
//TEST window = ( TEST )EditorWindow.GetWindow<TEST>();
}
void OnGUI()
{
EditorGUILayout.LabelField( "HELLO WORLD" );
}
}
}
Does your cs file have the same name as your class?