the @ commands

so this isnt mentioned really well in the reference
and im sort of a intermediate to beginner with code

but in various complicated scripts i keep seeing this @ or # symbol followed by a relatively illogical phrase (from my perspective)

can someone shed light on these various commands

#pragma strict
#pragma implicit
#pragma downcast
(hmmm it seems the # sign causes bolding)

@System.NonSerialized

and some others i encountered while trying to understand the networking category

if it turns out that the answer to this requires tons of detailed explanation
i would like to thank that poor soul profusely in advance

Alot of scripts uses this to add a component on the fly to a gameobject http://unity3d.com/support/documentation/ScriptReference/RequireComponent.html

okay so the @ symbol is kind of like telling a particular level / functioning body to do something. @system is telling the system to do something @script is telling the script to require something. Alright so i get that, but what about the ones containing the # symbol thats more confusing to me? btw ty for the response

2 Answers

2

Ok, first of all you have to keep in mind that you work with .NET / Mono in Unity. It’s a high-level standardisized programming environment. However UnityScript (Javascript) is not a standardisized language. It has a Javascript-like syntax but it’s something totally different.

The @-symbol:

The @-symbol in Unityscript is used to add attributes to elements in your script. Since Javascript doesn’t have something like attributes they “invented” the @-symbol in Unityscript. In C# attributes are added by writing the attribute within square-brackets in front of the element you want to add it to. In Unityscript it works the same way: You have to write the @ followed by the attribute in front of the element but since a scriptfile in Unityscript automatically represents a class, you can’t write the attribute in front of the class. That’s why you can use “@script attribute” to add an attribute to the class itself.

Attributes usually hold extended information of a certain field or class. The RequireComponent attribute is an attribute that comes with Unity. When you add it to a script Unity can read the attribute when you add your script to a gameobject and react to it by adding the given component to the gameobject.

System.NonSerialized is a default attribute that comes with .Net / Mono. It is usually used to tell a serializer to not serialize the field that is marked with that attribute. Unity also uses this information to not serialize it in their custom serializer and to hide it in the inspector.

The #pragma keywords:

Lines that start with # are not really part of the actual code. They are compiler commands / options. You can tell the compiler how it should process the following code. For example #pragma strict tells the compiler to not allow untyped(dynamic) variables. So you tell the compiler to throw errors on you when you try to use untyped variables. For more information see Performance Optimization

There are a lot other preprocessor-directives in general (not all work in Unity. Afaik #define won’t work)

At least in Java, these @Commands are called “Annotations”, they are use to change the behavior of methods, classes and even variables.
For example (In java)

@SessionScoped
public class MyClass{...}

In a Web context, this class will persist in the user session and will not be destroyed until the user session expiress.

These annotations are current used in others languages to modify somethings.

In Unity, they are called “attibutes”:
http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.Attributes.html

See this one:

// Javascript example
@script AddComponentMenu ("Transform/Follow Transform")
class FollowTransform extends MonoBehaviour {
}
// C# example:
[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}

The AddComponentMenu attribute allows
you to place a script anywhere in the
“Component” menu, instead of just the
“Component->Scripts” menu.

They modify the class behavior, like in Java.

Actually don't forget that annotations or attributes doesn't change anything of the code or behaviour itself. It's just additional information for those systems that are going to use your stuff. It's never a guaranty that every system behave the same way. It's information that can be interpreted differently. Every serializer "should" not serialize a field marked as NonSerialized but nobody forces him.