- Home /
How to make namespaces appear in the AddComponent List automatically?
Let's say I have a component called Platform within the namespace Hardware. Then let's say I have another component Platform within the namespace GameEntity. Both inherit from monobehaviour.
So when I go to add a component to a gameobject from the add component menu I'll see two items named 'Platform'. Is there a way to tell which is which without adding them and checking the file.
Ideally the component list would read Hardware.Platform and GameEntity.Platform. And for the sake of this question, let's pretend that I can't just rename my scripts.
The best I can find right now is -> AddComponentMenu("Hardware.Platform")] <- at the top of each class but that doesn't actually remove the original 'Platform' from the component menu and I'd rather unity just use the namespaces automatically rather than having to do it manually each time.
Answer by ShadyProductions · Feb 10, 2020 at 05:56 PM
You can enforce a specific using by doing
using Platform = Hardware.Platform;
then every Platform class in that script will reference to that specific one.
Or you could do AddComponent<Hardware.Platform>()
I'm specifically looking for how to make it appear like this in the AddComponent menu on game objects. Not how to use a namespace in a script.
I don't think you can do that other than rena$$anonymous$$g the script. Or using AddComponent$$anonymous$$enu
Answer by Bunny83 · Feb 10, 2020 at 06:49 PM
You just have the wrong approach to begin with. I highly recommend you read the guidelines for namespaces.
Specifically:
DO NOT give the same name to types in namespaces within a single application model.
The main purpose of namespaces is to group things together which belong together. Namespaces are not meant to distinguish between different implementations of the same thing. Namespaces do help when you use third-party frameworks to avoid naming conflicts. Do not create nameconflicts on purpose within the same application. So use proper classnames which actually describe what that component does and what's the difference between your two classes.
Thanks, very interesting read. But I'm not looking for how to use namespaces. I'm looking for an easier way to make namespaces appear in the AddComponent menu.
You're right of course, that one should try to make class names distinct. But one doesn't always have full control over them.
Also, even when class names are all distinct, it might be useful to someone to be able to see from the AddComponent pop-up, what namespace a class is in.
Actually, for me what would be even more useful (and would effectively encompass the namespace thing) would be if there was a field showing you the path to the script when you hovered over it.
So if there were a way to customise that pop-up, I'd be very interested to hear about it. I imagine that there isn't, though. And I may just resort to using the attribute the OP mentioned, which I wasn't aware of before (so thanks, @1illGamer !)