- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
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的检视面板访问.保存在成员变量的值将随工程自动保存.
C#
JavaScript
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 ,或脚本名等),之后你便可以通过拖拽游戏对象到检视面板去设置他们的值.
C#
JavaScript
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.
你也可以创建私有成员变量.私有成员变量在脚本外部是不可见的.它不会在检视面板显示.他们只有在检视面板的调试模式下才可见.那是个实时更新私有变量的调试器.
C#
JavaScript
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的全局变量.
C#
JavaScript
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;
来源:unity3d圣典 更多分享尽在web3D纳金网http://www.narkii.com/
————————————————————————————
Subsections 章节
[脚本章节]Scripting Overview 脚本概述
[脚本章节]Overview: Script compilation 脚本编译(高级)
[脚本章节]Overview: Performance Optimization 性能优化
[脚本章节]Overview: The most important classes 重要的类
[脚本章节]Overview : Writing Scripts in C# 使用C#书写脚本
[脚本章节]Overview: Coroutines & Yield 协同程序 & 中断
[脚本章节]Overview: Instantiate 实例
[脚本章节]Overview: Member Variables & Global Variables 成员变量 & 全局变量
[脚本章节]Overview: Vectors 向量
[脚本章节]Overview: Accessing Other Game Objects 访问其他游戏物体
[脚本章节]Overview: Accessing Other Components 访问其他组件
[脚本章节]Overview: Keeping Track of Time 记录时间
[脚本章节]Overview: Common Operations 常用操作
|
|