Search Unity

Purpose of creating your own namespace?

Discussion in 'Scripting' started by HarleyK, Apr 8, 2017.

  1. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    This may seem like a really noobish question but what is the purpose of creating your own namespace and or framework? How would one go about doing this?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    It's really for the sake of having organization mostly, and being able to do a using statement when you use that particular functionality. There's a little more to it, but basically for keeping a collection of a certain type of asset or tool or something.

    There's tons of examples online, on this forum and elsewhere.
     
    HarleyK likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Agree with regard to organization. Keeping similar things grouped. You'll see it all throughout the .NET library.
    Create your own namespace?
    Code (CSharp):
    1. namespace MyNewSpace {
    2. public static class ExampleClass{
    3. public static string FloatStr2(this string str, float f){
    4. return f.ToString("F2");
    5. }
    6. }
    7. }
    heh.
     
    HarleyK likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,155
    Everyone else has already mentioned organization, but the second purpose is to avoid naming conflicts. If you have two classes with identical names then the only way to differentiate between them is to give them different namespaces and refer to them through that.

    For example there is a Color struct under UnityEngine and another Color struct under System.Drawing. If you wanted to make use of both of them then you would need to refer to them as UnityEngine.Color and System.Drawing.Color. Or with other shortcuts assigned via the using keyword.

    https://msdn.microsoft.com/en-us/library/sf0df423.aspx
     
    HarleyK and flashframe like this.
  5. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Organization. I put test scripts into a TestCrap namespace so I don't get it confused with anything else. For your personal projects you can just put everything into the global namespace and it'll be fine, but for libraries that you distribute to other people you really need those to be in other namespaces. For example, if I send you my CharacterController script but you already have a CharacterController script, there's a conflict. Both cannot be used at the same time if they're in the same namespace (in this case, the global namespace). I'd have to put my CharacterController in a namespace called Uzi or something to differentiate them.

    But in general you don't need to worry about them when programming Unity.
     
    HarleyK likes this.
  6. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    Thank you all for the replies and the helpful info, I've just been seeing it a lot in other peoples work and was curious!