简体   繁体   中英

How to deal with invisible c# namespaces in Unity editor?

The unity documentation recommend to use namespaces to organize your code, avoid extended class name, and make more maintainable code.

ControlerCameraAction becomes Controleurs.Cameras.Action and CameraAction becomes Cameras.Action . However in Unity editor you can't see your namespaces, only the last class name, and this can be confusing since some class have now same name.

Editor shows now Action and Action
So how to use namespaces in Unity? Am I doing something wrong?

So how to use namespaces in Unity ?

Like you did, basically. Namespaces are used in code, not in the editor. The main problem of Unity is the way that scripts are treated. Scripts get compiled internally into debuggable IL-Code.

But in the editor itself, they are always named by their filenames, which are forced to be the same as the class name.

This is the reason you don't see your whole type-name (namespace + classname), but always the classname alone.

Am i doing something wrong ?

No, you aren't. As far as I remember, there is no way to display the classname instead of the filename in the editor, since the displayed data is determined by the meta-files that Unity generates.

So tl;dr:

You are doing it right and there is no way to display the namespaces in the Editor.

I'd suggest you should sort your scripts into folders and use the AddComponentMenu -Attribute to organize your code physically according to your namespaces. This is the same pattern that is used by Microsoft.

Easy way is to think in namespace how file folder. You assign your desired name avoiding similar name to systems and unity namespace. Each namespace inside another namespace is similar to navigate inside a folder inside another folder.

Ussually this start with your company name or your plugin name:

namespace mycompany_name
{
    namespace myplugin_name
    {
    }
}

or

namespace myplugin_name
{
    //part name i.e. Networking, Utils, Database or similar ramification
    namespace myplugin_part_name
    {
    }
}

Later you reference to it in another script with "using":

using mycompany_name.myplugin_name

The unity documentation recommend to use namespaces to organize your code, avoid extended class name, and make more maintainable code.

Yes !

ControlerCameraAction becomes Controleurs.Cameras.Action and CameraAction becomes Cameras.Action .

Yes, and no.

The idea behind namespaces is organization not just simple use this feature called namespaces . And that organization should follow a logic. If every class has a different namespace then you will have to import as many namespaces as classes that you will use in your code.

You can think in terms of 'modules' or maybe see if a layered architecture can be useful for you.

If you have ControllerCameraAction and CameraAction , you can a.- use the namespace Cameras for both (then you will have Cameras.CameraAction and Cameras.ControllerCameraAction ) , b.- if you have a layered architecture (like MVP, MVVM, or some more DomainDesign Driven, etc.) you can have namespaces using the layer name and the module. (then you will have something like Presentation.Cameras.ControllerCameraAction , Domain.Cameras.CameraAction and this can help you to follow an Onion architecture ).


The syntax for namespaces are like this:

 namespace Domain.Cameras { public class CameraAction { } } 

And you use them with using directive

 using Domain.Cameras; namespace Presentation.Cameras { public class ControllerCameraAction { private CameraAction cameraAction; ... } } 

More about namespaces here !

By default all classes that don't have an explicit namespace belong to global namespace, so even when you are not writing any namespace you are using one.


Unity will not make any difference between namespaces, this is more ac# characteristic. And it helps with organization, separation of concerns principle, and avoiding name conflicts too, but in the last instance, your class names should still be representative and clear enough to understand what that class does. If you see Camera.cs and Camera.cs it's really hard to see what class does what. If you open those files and see the namespace/code/folder where they are that will help, but the idea is save those extra seconds/cognitive load and just be more explicit with your names.


As a complement here you can see another interesting discussion about namespaces use .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM