Search Unity

Using namespaces with custom EditorWindow

Discussion in 'Scripting' started by RoelBartstra, Mar 23, 2013.

  1. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20
    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:
    Code (csharp):
    1.  
    2.         [MenuItem("My Menu/Item 1", priority = 2)]
    3.         public static void ShowWindow()
    4.         {
    5.             EditorWindow window = EditorWindow.GetWindow<MyAwesomeWindow>(title: "My Awesome Window");
    6.         }
    7.  
    I laso tried it like this:
    Code (csharp):
    1.  
    2.         [MenuItem("My Menu/Item 1", priority = 2)]
    3.         public static void ShowWindow()
    4.         {
    5.             EditorWindow window = EditorWindow.GetWindow<MyNamespace.MyAwesomeWindow>(title: "My Awesome Window");
    6.         }
    7.  
    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?
     
  2. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Which version of Unity are you using? Unity 4 does support using namespaces, anything before does not.

    Karl
     
  3. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    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.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You error is
    yourClassName window = (yourClassName )EditorWindow.GetWindow(typeof(yourClassName));
    not
    EditorWindow window = ...
     
  5. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    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
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  7. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20
    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.
     
  8. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20
    Both are not working with namespaces, and without namespaces they both work.
     
  9. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    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.

    Code (csharp):
    1.  
    2. namespace DISUnity.Editor
    3. {
    4.     [CustomEditor( typeof( EntityTypeMap ) )]
    5.     public class EntityMapEditor : UnityEditor.Editor
    6. {
    7. ....
    8. }
    9. }
    10.  
    This is an example of my old code that worked, i have since switched to using property drawers:

    Code (csharp):
    1.  
    2. Editor e = Editor.CreateEditor( MyMonoClass, typeof( MyMonoCLassType ) );
    3.  
    k
     
    Last edited: Mar 24, 2013
  10. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    Just knocked this together and it worked for me: Tested in Unity4.1.

    Code (csharp):
    1.  
    2. namespace DISUnity.Editor
    3. {
    4.     public class TEST : EditorWindow
    5.     {
    6.         [MenuItem ("Window/My Window")]
    7.         public static void Init ()
    8.         {
    9.             // Both work
    10.             TEST window =  ( TEST )EditorWindow.GetWindow( typeof( TEST ) );
    11.             //TEST window =  ( TEST )EditorWindow.GetWindow<TEST>();      
    12.         }
    13.  
    14.  
    15.         void OnGUI()
    16.         {
    17.             EditorGUILayout.LabelField( "HELLO WORLD" );
    18.         }
    19.     }
    20. }
    21.  
    Does your cs file have the same name as your class?
     
    Last edited: Mar 24, 2013
  11. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20

    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....
     
  12. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I don't expect this is the problem, but as it is in the code you posted, I still wanted to ask.

    Code (csharp):
    1. EditorWindow window = EditorWindow.GetWindow<MyAwesomeWindow>(title: "My Awesome Window");
    I hope you don't have "title:" in your actual code.
     
  13. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20
    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!