Namespaces in Unity 3D :
Namespaces are container of the classes. Namespaces help us to organize the scripts. When we create a new C# script in Monodevelop, two namespaces are included at the top, these are ' using UnityEngine; ' and ' using System.Collection; ' . We can access all the classes of these namespaces in our script.
To put a class in namespace we need to surround the class with the namespace.
For Example : Use the keyword namespace and give any name to this namespace.
using UnityEngine;
using System.Collections;
namespace DemoNamespace
{
public class DemoClass1 : MonoBehaviour
{
void Start ()
{
Debug.Log("Namespace is created..");
}
}
}
There are three ways to use the classes of the namespace -
First : By using the 'using' directive at the top
using UnityEngine;
using System.Collections;
using DemoNamespace;
public class DemoClass2 : MonoBehaviour
{
void Start ()
{
Debug.Log("First way to use the classes of namespace.");
}
}
Second : Access the classes using dot operator
using UnityEngine;
using System.Collections;
public class DemoClass3 : MonoBehaviour
{
void Start ()
{
DemoNamespace.DemoClass1 MyClass = new DemoNamespace.DemoClass1();
Debug.Log("Second way to use the classes of namespace.");
}
}
Third : To put the class in the namespace
using UnityEngine;
using System.Collections;
namespace DemoNamespace
{
public class DemoClass4 : MonoBehaviour
{
void Start ()
{
DemoClass1 MyClass= new DemoClass1();
Debug.Log("Third way to use the classes of namespace.");
}
}
}
Thankyou !
Happy Coding !
0 Comment(s)