Overview: Member Variables & Global Variables 成员变量 & 全局变量

Any variable defined outside of any function defines a member variable. The variables are accessible through the inspector inside Unity. Any value stored in a member variable is also automatically saved with the project.

定义在函数体之外的变量称之为成员变量(注:这不是通常成员变量的定义).这个变量可以在Unity的检视面板访问.保存在成员变量的值将随工程自动保存.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public float memberVariable = 0.0F;
}
var memberVariable = 0.0; 

The above variable will show up as a numeric property called "Member Variable" in the inspector.

上面的变量将在检视面板中以一个数字形式显示,属性名为"Member Variable".

If you set the type of a variable to a component type (i.e. Transform , Rigidbody , Collider , any script name, etc.) then you can set them by dragging game objects onto the value in the inspector.

如果你设置一个变量类型为组件类型(类似于 Transform , Rigidbody , Collider ,或脚本名等),之后你便可以通过拖拽游戏对象到检视面板去设置他们的值.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Transform enemy;
    void Update() {
        if (Vector3.Distance(enemy.position, transform.position) < 10)
            print("I sense the enemy is near!");

    }
}
var enemy : Transform;

function Update() {
    if ( Vector3.Distance( enemy.position, transform.position ) < 10 )
        print("I sense the enemy is near!");
}

You can also create private member variables. Private member variables are useful for storing state that should not be visible outside the script. Private member variables are not saved to disk and are not editable in the inspector. They are visible in the inspector when it is set to debug mode. This allows you to use private variables like a real time updating debugger.

你也可以创建私有成员变量.私有成员变量在脚本外部是不可见的.它不会在检视面板显示.他们只有在检视面板的调试模式下才可见.那是个实时更新私有变量的调试器.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    private Collider lastCollider;
        void OnCollisionEnter(Collision collisionInfo) {
            lastCollider = collisionInfo.collider;
    }
}
private var lastCollider : Collider;

function OnCollisionEnter(collisionInfo : Collision ) {
    lastCollider = collisionInfo.collider;
}

Global variables   全局变量

You can also create global variables using the static keyword.

你也可以用static关键字创建全局变量.

This creates a global variable called someGlobal.

这是创建了一个名为someGlobal的全局变量.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public static int someGlobal = 5;
    public void Awake() {
        print(typeof(someGlobal));
        typeof(someGlobal) = 1;
    }
}
// The static variable in a script named 'TheScriptName.js'

// 脚本里的静态变量名为 'TheScriptName.js'
static var someGlobal = 5;

// You can access it from inside the script like normal variables:

// 你可以像普通变量一样去访问它:
print(someGlobal);
someGlobal = 1;

To access it from another script you need to use the name of the script followed by a dot and the global variable name.

从另一个脚本访问它,你需要用”脚本名加一个小圆点再加上全局变量名”.

print(TheScriptName.someGlobal);
TheScriptName.someGlobal = 10;

最后修改:2011年5月17日 Tuesday 11:59

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。