Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Create a code packet that can be addressed through 'using' keyword

Discussion in 'Scripting' started by Kinkku2, Jan 14, 2016.

  1. Kinkku2

    Kinkku2

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hi! I'm wondering how to import a new code packet into a C# class. What I mean is things like Unity's own 'UnityEngine' and 'UnityEditor' code packets that are imported to classes by the phrase 'using UnityEngine'. I'd like to create my own packet and I'm not sure at all how to go about creating one. Thanks.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,494
    They're called namespaces
    Code (csharp):
    1.  
    2. namespace MyStuff
    3. {
    4.     public class MyClass { }
    5. }
    6.  
    Code (csharp):
    1.  
    2. using MyStuff;
    3.  
    4. MyClass mc = new MyClass();
    5.  
    Note that MonoBehaviour derivatives will organize themselves according to namespace in the Add Component menu in the Inspector.
     
    Kiwasi and eisenpony like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,838
    !!!!
    I had no idea....
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    781
    Wait what? Please do explain?
     
  5. Kinkku2

    Kinkku2

    Joined:
    Sep 29, 2014
    Posts:
    106
    Thank you for the answer! How will the order affect the components?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,494
    Yeah. We unfortunately don't leverage it much in Fractured State because a huge chunk of code was done before Unity supported namespaced MonoBehaviours and we just haven't been irked into going back and cleaning it up.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,838
    Select a GameObject, see inspector, click "Add Component", click "Script". If your scripts are arranged in namespaces, then that menu will have a folder for each namespace.
     
  8. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    781
    That's neato!
     
  9. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    @Kinkku2 You can also have namespaces that have multiple spaces within them. Just separate them with a dot(.). I don't think there is a limit to the number of spaces you can have in a namespace.

    Code (CSharp):
    1. // Movies/Reader.cs
    2. namespace Awesome.Movies{
    3.     public class Reader{ /* Does cool stuff */ }
    4. }
    5. // Movies/Writer.cs
    6. namespace Awesome.Movies{
    7.     public class Writer{ /* Does cool stuff */ }
    8. }
    9.  
    10. // Music/Reader.cs
    11. namespace Awesome.Music{
    12.     public class Reader{ /* Does cool stuff */ }
    13. }
    14. // Music/Writer.cs
    15. namespace Awesome.Music{
    16.     public class Writer{ /* Does cool stuff */ }
    17. }
    Create instance of the movie reader:
    Code (CSharp):
    1. using Awesome.Movies;
    2.  
    3. public class MovieStuff : MonoBehaviour {
    4.     void Start(){
    5.         Reader reader = new Reader();
    6.     }
    7. }
     
    jtsmith1287 and Kiwasi like this.
unityunity