Unity's #pragma strict, #pragma downcast and other command sharing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.