Using namespaces with custom EditorWindow

Hi everyone,

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?

Which version of Unity are you using? Unity 4 does support using namespaces, anything before does not.

Karl

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.

You error is
yourClassName window = (yourClassName )EditorWindow.GetWindow(typeof(yourClassName));
not
EditorWindow window = …

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.

K

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.

Both are not working with namespaces, and without namespaces they both work.

I use Editors, PropertyDrawers, ScriptableObjects and MonoBehaviours in namespaces with no problems. Perhaps this is an issue with EditorWindows?

Here is some code I use now for Editors, i do not manually invoke the window, it just gets used when i edit a monobehaviour of type EntityTypeMap.

namespace DISUnity.Editor
{
    [CustomEditor( typeof( EntityTypeMap ) )]
    public class EntityMapEditor : UnityEditor.Editor
{
....
}
}

This is an example of my old code that worked, i have since switched to using property drawers:

Editor e = Editor.CreateEditor( MyMonoClass, typeof( MyMonoCLassType ) );

k

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?

Cool, this code seems to work, now I only need to figure out why my code was not working :).
My filename = classname, so it has to be something else…

I don’t expect this is the problem, but as it is in the code you posted, I still wanted to ask.

EditorWindow window = EditorWindow.GetWindow<MyAwesomeWindow>(title: "My Awesome Window");

I hope you don’t have “title:” in your actual code.

Thanks! that was it! I’m not a programmer, so still make a lot of dumb mistakes when messing around with code :P.

thx all!