Namespaces are like
containers for classes. Their purpose is to help
your organize your scripts and to avoid conflicts
between scripts. You might, for example,
be making tools in Unity to help you develop
your application. You could keep the tools and actual
application in separate namespaces so you didn't have too many
unnecessary classes being suggested by autocomplete. Chances are, all of the scripts
you have been writing so far have been using namespaces. At the top of C# scripts in Unity,
b
y default, you'll see two lines: "using UnityEngine;", and
"using System.Collections;" UnityEngine and System.Collections
are both namespaces. The "using" keyword means
that anything within the namespace that follows
can be used in the script. Try commenting out the "using"
directives at the top of the script, and you can see
that autocomplete has far fewer classes
that you can use. That's because classes
like GameObject, Transform, RigidBody, and many more, are all
in the UnityEngine namespace.
In order to put our
class in a namespace, we need to surround the class
with the namespace syntax. Start off with the
keyword "namespace", and then the namespace's name. This can be an existing
namespace, or a new one. In this case, we are gonna call our
namespace "SampleNamespace". Then we have an open
brace before the class, and a closed brace after the class. Since our class now
belongs to SampleNamespace, we'll indent it to show belonging. There are three ways we can use the
classes from a
specific namespace. We've already seen the
first which is to include a "using" directive at
the top of your script. The second way to access the
class is to use the dot operator. For example, instead of having
"using SampleNamespace;" at the top of the script,
every time we wanted to reference a class from
the SampleNamespace, we could type SampleNamespace,
dot, and then the class. This option avoids ambiguity,
but could be cumbersome, particularly with large namespace
names, like SampleNamespa
ce. The last option is to put the
class that you are writing into the namespace that
you need access to. This is generally not recommended, unless you mean to have that class
in the same namespace anyway. So long as classes are
in different namespaces, they may have the same name. However, since scripts
have the same name as the class contained in them,
scripts must be in different folders in order to have the same class name. When using namespaces, be careful to avoid ambiguous definitions. For
example, two very
common namespaces to use are "System",
and "UnityEngine". These both contain a definition
for a class called "Random". If you are using
them both, you need to disambiguate the class
by using the dot operator. Either use "System.Random"
or "UnityEngine.Random". You might've noticed that the
second default using directive is the "System.Collections"
namespace. This illustrates that
namespaces are nestable. In order to nest namespaces,
simply enclose one namespace
declaration wit
hin another.
Comments
summary by bing ai: Namespaces are like containers for classes that help organize scripts and avoid conflicts. The using keyword allows you to use anything within the specified namespace in your script. You can put a class in a namespace by surrounding it with the namespace syntax, starting with the keyword namespace followed by the namespace’s name. There are several ways to access classes from a specific namespace, including using a using directive at the top of your script, using the dot operator, or putting the class you are writing into the namespace you need access to. It’s important to be careful when using namespaces to avoid ambiguous definitions.
I find it interesting that you derive most of your scripts from MonoBehavior. But if you remove the UnityEngine namespace, you can't derive from Monobehavior anymore. So Mono is a class in the UnityEngine nameSpace. Interesting when you want multiple objects to for example, be an entity, but each is different. So you derive from classes all under the nameSpace entity. Such as entity: creature > human > NPC. entity: machine > human > player. Both are dynamic objects players and things in the game interact with but; One is a living being affected by diseases and fatique, has human stats and movement and is controlled by an AI. Other is an autonomous creation impervious by viruses but detrimental to water and doesn't recover, has constant but changeable stats based on parts, machine movement and is controlled by a player.
Amazing
Thanks, Man you saved me with this UCkszU2WH9gy1mb0dV-11UJg/8P50XuS9Oo7h8wSqtIagBA
1:40 you can hear the keyboard 😁
I have a question, why is my namespace not showing?
ngl but i have no clue what hes doing.
.
subcount 696k nice * 1.5
homeschool mom and 16 yo. ✅🍀
All these videos have "in Unity" in the title, yet this is the first one that involves Unity in any way and only for a brief moment, making them really generic scripting tutorials of which there are already many. It would make more sense to introduce a concept and then show how it could be used in actual (professional) Unity development, instead of just a simplistic example that is unlikely to be scalable for anything more than a dinky 2D side-scroller. There are already many other (better) tutorials on C# and calling this level of scripting "intermediate" is generous.
Silly C#, Java does this so much better with package -- I guess you gotta use what you have.