At the beginning of some unity scripts you can often see #pragma strict, #pragma downcast, #pragma implicit instructions, and some unity scripts do not have these instructions, what is the purpose of these instructions?
#pragma strict, the meaning of this directive is forced input, that is, when declaring a variable, we need to declare exactly what type of variable it is, rather than let the compiler infer the type of the variable by itself, so it is not possible to use a random name and have the compiler instantiate it for you. For example, after using this directive, we arbitrarily declare a variable:
Private Var bobby;//is not possible,
and need this:
Private Var bobby:gameobject;//this can be
By using #pragma strict, you can force us to develop good programming habits, but this is not something that must be done in unity.
and #pragma downcast and #pragma implicit command, it can be used with #pragma strict directive, so as to achieve "strict in the loose", it is a bit of meaning. First look at the #pragma implicit directive, which means that in the use of #pragma strict instructions, with this directive and can implicitly declare variables,
For example:
#pragma strict foo = 5;//Can not #pragma strict var foo = 5;//can #pragma strict #pragma implicit fo o = 5; This sentence is possible by using the #pragma implicit.
The #pragma downcast statement allows the variable to be used from super (parent type) to the #pragma strict instruction.
Sub (sub) type conversions, for example:
#pragma strict var go:gameobject; var clone:gameobject = instantiate (go); This statement is not possible because the object type returned after instantiate//Is object, not gameobject #pragma strict #pragma downcast var go: Gameobject; var clone:gameobject = instantiate (go); This statement is possible, using #pragma downcast //The following statement is also possible: #pragma strict var go:gameobject; var clone:gameobject = Instantiate (go) as gameobject; This statement is also possible because the//type is converted as.
Unity's #pragma strict, #pragma downcast and other command sharing