- Home /
How to avoid JavaScript namespace pollution?
It seems to me that Unity JavaScript only offers a global namespace for custom JavaScript classes. Can this be circumvented?
I know that C# packages can be imported using the import
directive. Is it possible to create packages in JavaScript as well?
Answer by Bunny83 · Aug 13, 2012 at 04:52 PM
I'm not sure if there is a namespace statement in UntiyScript. If there is you would have to write out all your classes as well as in C#, otherwise you can't put them in a namespace.
Beside that Unity imports ALL classes derived from MonoBehaviour, ScriptableObject, Editor, EditorWindow and all other classes that are bound to the file name into the global namespace, always! Even when you import a dll which always contains a namespace, Unity extracts the MonoBehaviours during import. Namespaces are not supported by Unity. You can use them, especially for non Unity classes or to group your classes, but Unity classes will always be imported into the global namespace.
Answer by FarqTheOrc · Sep 28, 2012 at 06:22 PM
Unity doesn't seem to support namespaces.
To illustrate the problem, if you have a script named "/Scripts/Intro/Play.js" and attempt to have another script named "/Scripts/Game/Play.js" then you will get a conflict.
I would suggest to use some sort of prefix / 'hungarian' notation instead.
E.g. "IntroPlay.js" vs "GamePlay.js", or "I_Play.js" vs "G_Play.js"
Your answer
Follow this Question
Related Questions
How to solve "The type x exists in both y aand z"?,How to solve "The type x exists in both y and z" 1 Answer
Unity overwrites files with the same name but in different namespaces on package import? 6 Answers
Importing Packages 0 Answers
Importing data from excel using javascript; Assembly reference error. 1 Answer
Preventing the first person controller from rotating 1 Answer