Unity3D 中文脚本参考

cgkelei

贡献于2012-06-12

字数:0 关键词: 游戏开发

长宽高(北京)科技有限公司 Unity3D 中文脚本参考 (更新至 v2.5) 译:Nette 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 目录 一、脚本概览.....................................................................................................................1 概览: 常用操作 ...............................................................................................................1 概览: 跟踪时间 ...............................................................................................................1 概览: 访问其他组件 .......................................................................................................2 概览: 访问其他游戏物体 ...............................................................................................2 概览: 向量 .......................................................................................................................4 概览: 成员变量 & 全局变量变量................................................................................5 概览: 实例化 ...................................................................................................................5 概览: Coroutines & Yield ...............................................................................................6 概览: 用 C#编写脚本 .....................................................................................................7 概览: 最重要的类 ...........................................................................................................8 概览: 性能优化 ...............................................................................................................8 概览: 脚本编译(高级) .............................................................................................10 二、运行时类 ........................................................................................................................12 AnimationCurve............................................................................................................12 AnimationEvent ............................................................................................................13 AnimationState..............................................................................................................14 Application.....................................................................................................................16 Array ..............................................................................................................................22 BitStream .......................................................................................................................25 BoneWeight....................................................................................................................26 Bounds............................................................................................................................27 Collision .........................................................................................................................28 Color...............................................................................................................................29 ContactPoint..................................................................................................................32 ControllerColliderHit ...................................................................................................33 Debug .............................................................................................................................34 Event...............................................................................................................................35 GL...................................................................................................................................38 GUIContent ...................................................................................................................41 GUILayoutOption.........................................................................................................43 GUILayoutUtility..........................................................................................................43 GUILayout.....................................................................................................................45 GUISettings ...................................................................................................................59 GUIStyleState ................................................................................................................59 GUIStyle.........................................................................................................................60 GUIUtility ......................................................................................................................64 GUI.................................................................................................................................65 GeometryUtility.............................................................................................................80 Gizmos............................................................................................................................80 Graphics.........................................................................................................................82 Hashtable .......................................................................................................................84 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 HostData ........................................................................................................................84 Input...............................................................................................................................85 JointDrive ......................................................................................................................89 JointLimits.....................................................................................................................90 JointMotor.....................................................................................................................91 JointSpring ....................................................................................................................91 Keyframe .......................................................................................................................91 LayerMask.....................................................................................................................92 LightmapData................................................................................................................93 LightmapSettings ..........................................................................................................93 MasterServer .................................................................................................................94 MaterialPropertyBlock.................................................................................................96 Mathf..............................................................................................................................98 Matrix4x4.....................................................................................................................104 NetworkMessageInfo ..................................................................................................107 NetworkPlayer.............................................................................................................108 NetworkViewID...........................................................................................................109 Network........................................................................................................................109 Object...........................................................................................................................122 AnimationClip .............................................................................................................125 AssetBundle .................................................................................................................128 AudioClip.....................................................................................................................129 Component...................................................................................................................130 Behaviour.....................................................................................................................134 Animation ....................................................................................................................135 AudioListener..............................................................................................................141 AudioSource ................................................................................................................143 Camera.........................................................................................................................147 ConstantForce .............................................................................................................158 GUIElement.................................................................................................................160 GUIText........................................................................................................................162 GUITexture..................................................................................................................163 GUILayer.....................................................................................................................165 LensFlare .....................................................................................................................166 Light .............................................................................................................................168 MonoBehaviour...........................................................................................................172 Terrain..........................................................................................................................187 networkView................................................................................................................192 Projector ......................................................................................................................195 Skybox..........................................................................................................................198 Collider.........................................................................................................................200 BoxCollider..................................................................................................................204 CapsuleCollider...........................................................................................................206 CharacterController ...................................................................................................208 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 MeshCollider ...............................................................................................................213 RaycastCollider ...........................................................................................................215 SphereCollider.............................................................................................................217 TerrainCollider............................................................................................................219 WheelCollider..............................................................................................................221 Joint..............................................................................................................................224 CharacterJoint ............................................................................................................226 ConfigurableJoint .......................................................................................................228 FixedJoint ....................................................................................................................232 HingeJoint....................................................................................................................233 SpringJoint ..................................................................................................................236 MeshFilter....................................................................................................................238 ParticleAnimator.........................................................................................................239 ParticleEmitter............................................................................................................241 Renderer ......................................................................................................................244 LineRenderer...............................................................................................................248 MeshRenderer .............................................................................................................250 ParticleRenderer .........................................................................................................252 SkinnedMeshRenderer ...............................................................................................254 TrailRenderer ..............................................................................................................257 Rigidbody.....................................................................................................................259 TextMesh......................................................................................................................269 Transform ....................................................................................................................270 Flare .............................................................................................................................279 Font...............................................................................................................................279 GameObject.................................................................................................................280 Material........................................................................................................................288 Mesh .............................................................................................................................296 PhysicMaterial.............................................................................................................303 ScriptableObject..........................................................................................................305 GUISkin .......................................................................................................................306 Shader ..........................................................................................................................309 TerrainData .................................................................................................................311 TextAsset......................................................................................................................313 Texture .........................................................................................................................314 Cubemap......................................................................................................................315 MovieTexture...............................................................................................................317 RenderTexture.............................................................................................................319 Texture2D.....................................................................................................................322 Particle .........................................................................................................................328 Path...............................................................................................................................329 Physics..........................................................................................................................330 Ping...............................................................................................................................334 Plane.............................................................................................................................334 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 PlayerPrefsException..................................................................................................335 PlayerPrefs...................................................................................................................335 QualitySettings ............................................................................................................337 Quaternion...................................................................................................................338 Random........................................................................................................................342 Ray................................................................................................................................343 RaycastHit ...................................................................................................................343 RectOffset ....................................................................................................................347 Rect...............................................................................................................................348 RenderSettings ............................................................................................................349 Resolution ....................................................................................................................350 Resources .....................................................................................................................351 Screen...........................................................................................................................353 SoftJointLimit .............................................................................................................355 String ............................................................................................................................355 SystemInfo ...................................................................................................................356 Time..............................................................................................................................357 Vector2 .........................................................................................................................360 Vector3 .........................................................................................................................362 Vector4 .........................................................................................................................369 WWWForm.................................................................................................................372 WWW...........................................................................................................................376 WheelFrictionCurve ...................................................................................................384 WheelHit ......................................................................................................................385 YieldInstruction...........................................................................................................386 Coroutine .....................................................................................................................386 WaitForEndOfFrame .................................................................................................387 WaitForFixedUpdate ..................................................................................................388 WaitForSeconds ..........................................................................................................388 属性 ......................................................................................................................................390 AddComponentMenu .................................................................................................390 ContextMenu ...............................................................................................................390 ExecuteInEditMode ....................................................................................................391 HideInInspector ..........................................................................................................391 NonSerialized...............................................................................................................391 RPC ..............................................................................................................................392 RenderBeforeQueues ..................................................................................................392 RequireComponent.....................................................................................................392 Serializable...................................................................................................................393 枚举 ......................................................................................................................................393 AnimationBlendMode.................................................................................................393 AudioVelocityUpdateMode ........................................................................................394 CameraClearFlags ......................................................................................................394 CollisionFlags ..............................................................................................................395 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 ConfigurableJointMotion...........................................................................................397 ConnectionTesterStatus ..............................................................................................397 CubemapFace..............................................................................................................398 EventType ....................................................................................................................398 FilterMode ...................................................................................................................400 FocusType ....................................................................................................................401 ForceMode ...................................................................................................................401 HideFlags .....................................................................................................................402 ImagePosition ..............................................................................................................402 JointDriveMode...........................................................................................................403 JointProjectionMode...................................................................................................403 KeyCode.......................................................................................................................404 LightRenderMode.......................................................................................................409 LightShadows ..............................................................................................................409 LightType.....................................................................................................................410 MasterServerEvent .....................................................................................................411 NetworkConnectionError ..........................................................................................411 NetworkDisconnection................................................................................................412 NetworkPeerType........................................................................................................412 NetworkStateSynchronization ...................................................................................412 ParticleRenderMode...................................................................................................412 PhysicMaterialCombine .............................................................................................413 PlayMode .....................................................................................................................413 PrimitiveType ..............................................................................................................414 QualityLevel ................................................................................................................415 QueueMode..................................................................................................................415 RPCMode.....................................................................................................................416 RenderTextureFormat................................................................................................416 RigidbodyInterpolation ..............................................................................................417 RotationDriveMode ....................................................................................................417 RuntimePlatform ........................................................................................................417 ScaleMode....................................................................................................................418 SendMessageOptions ..................................................................................................418 SkinQuality..................................................................................................................418 Space.............................................................................................................................419 TerrainLighting...........................................................................................................419 TextAlignment .............................................................................................................420 TextAnchor ..................................................................................................................420 TextClipping ................................................................................................................420 TextureFormat.............................................................................................................421 TextureWrapMode......................................................................................................421 WrapMode...................................................................................................................422 三、编辑器类 ......................................................................................................................423 AnimationClipCurveData ..........................................................................................423 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 AnimationUtility..........................................................................................................423 AssetDatabase..............................................................................................................424 AssetImporter..............................................................................................................428 AssetPostprocessor......................................................................................................429 AudioImporter ............................................................................................................432 ModelImporter............................................................................................................433 TextureImporter..........................................................................................................435 AssetPostprocessor......................................................................................................437 BuildPipeline ...............................................................................................................441 DragAndDrop..............................................................................................................444 DrawGizmo..................................................................................................................446 Editor ...........................................................................................................................447 EditorApplication........................................................................................................449 EditorGUILayout........................................................................................................451 EditorGUIUtility .........................................................................................................464 EditorGUI....................................................................................................................466 EditorPrefs...................................................................................................................473 EditorStyles..................................................................................................................475 EditorUtility.................................................................................................................475 EditorWindow .............................................................................................................479 FileUtil..........................................................................................................................484 HandleUtility ...............................................................................................................484 Handles.........................................................................................................................488 Help ..............................................................................................................................493 MenuCommand...........................................................................................................494 MenuItem.....................................................................................................................494 ModelImporterClipAnimation...................................................................................496 MonoScript ..................................................................................................................497 MovieImporter ............................................................................................................498 ObjectNames ...............................................................................................................499 ScriptableWizard ........................................................................................................499 Selection .......................................................................................................................504 TrueTypeFontImporter ..............................................................................................505 Undo .............................................................................................................................507 枚举 ......................................................................................................................................509 AudioImporterChannels ............................................................................................509 AudioImporterFormat................................................................................................509 BuildAssetBundleOptions ..........................................................................................510 BuildOptions................................................................................................................510 BuildTarget..................................................................................................................511 DragAndDropVisualMode .........................................................................................512 FontRenderMode ........................................................................................................513 FontTextureCase .........................................................................................................513 GizmoType...................................................................................................................513 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 ImportAssetOptions....................................................................................................514 ModelImporterGenerateAnimations.........................................................................515 ModelImporterGenerateMaterials............................................................................515 MouseCursor ...............................................................................................................516 PrefabType...................................................................................................................516 ReplacePrefabOptions ................................................................................................517 SelectionMode .............................................................................................................517 TextureImporterFormat.............................................................................................518 TextureImporterGenerateCubemap..........................................................................519 TextureImporterMipFilter .........................................................................................519 TextureImporterNPOTScale......................................................................................520 TextureImporterNormalFilter ...................................................................................520 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 一、脚本概览 这是一个关于 Unity 内部脚本如何工作的简单概览。 Unity 内部的脚本,是通过附加自定义脚本对象到游戏物体组成的。在脚本对象内部不 同的函数被特定的事件调用。最常用的列在下面: Update: 这个函数在渲染一帧之前被调用。这里是大部分游戏行为代码被执行的地方,但除了物 理代码。 FixedUpdate 这个函数在每个物理时间步被调用一次。这是处理基于物理的游戏行为的地方。 在任何函数之外的代码: 在任何函数之外的代码在物体被加载的时候运行。 这个可以用来初始化脚本状态。 注意: 文档的这个部分假设你是用 Javascript,参考用 C#编写脚本获取如何使用 C#和 Boo 编写脚本的信息。 你也能定义事件句柄。它们的名称都以 On 开始,(例如 OnCollisionEnter).为了查看完整 的预定义事件的列表,参考 MonoBehaviour 文档。 概览: 常用操作 大多数游戏物体的操作是通过游戏物体的 Transform 和/或 Rigidbody 来做的。在行为脚 本内部它们可以分别通过 transform 和 rigidbody 访问.因此如果你想绕着 Y 轴每帧旋转 5 度, 你可以如下写: function Update() { transform.Rotate(0, 5, 0); } 如果你想向前移动一个物体,你应该如下写: function Update() { transform.Translate(0, 0, 2); } 概览: 跟踪时间 Time 类包含一个非常重要的类变量,称为 deltaTime.这个变量包含从上一次调用 Update 或 FixedUpdate (根据你是在 Update 函数还是在 FixedUpdate 函数中)到现在的时间量。 所以对于上面的例子,修改它使这个物体以一个恒定的速度旋转而不依赖于帧率: function Update() { transform.Rotate(0, 5 * Time.deltaTime, 0); } 移动物体: function Update() { transform.Translate(0, 0, 2 * Time.deltaTime); } 如果你加或减一个每帧改变的值,你应该将它与 Time.deltaTime 相乘。当你乘以 Time.deltaTime 时,你实际表达:我想以 10 米/秒移动这个物体而不是 10 米/帧。这不仅仅 是因为你的游戏将独立于帧率运行,同时也是因为运动的单位容易理解。(10 米/秒) 另一个例子,如果你想随着时间增加光照的范围。下面的表达式,以 2 单位/秒改变半 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 径. function Update() { light.range += 2.0 * Time.deltaTime; } 当通过力处理刚体的时候,你通常不必用 Time.deltaTime 乘,因为引擎已经为你考虑到 了这一点。 概览: 访问其他组件 组件被附加到游戏物体。附加 Renderer 到游戏物体使它在场景中渲染,附加一个 Camera 使它变为相机物体。所有的脚本都是组件,因为它们能被附加到游戏物体。 最常用的组件可以作为简单成员变量访问: Component 可如下访问 Transform transform Rigidbody rigidbody Renderer renderer Camera camera (only on camera objects) Light light (only on light objects) Animation animation Collider collider ... 等等。 对于完整的预定义成员变量的列表,查看 Component, Behaviour 和 MonoBehaviour 类文 档。如果游戏物体没有你想取回类型的组件,上面的变量将被设置为 null。 任何附加到一个游戏物体的组件或脚本都可以通过 GetComponent 访问。 transform.Translate(0, 1, 0); // 等同于 GetComponent(Transform).Translate(0, 1, 0); 注意 transform 和 Transform 之间大小写的区别. 前者是变量(小写),后者是类或脚本名称 (大写). 大小写不同使你能够从类和脚本名中区分变量。 应用我们所学,你可以使用 GetComponent 找到任何附加在同一游戏物体上的脚本和组 件。请注意要使下面的例子能够工作,你需要有一个名为 OtherScript 的脚本,其中包含一 个 DoSomething 函数。OtherScript 脚本必须与下面的脚本附加到相同的游戏物体上。 //这个在同一个游戏物体上找到名为 OtherScript 的脚本 //并调用它上面的 DoSomething. function Update () { otherScript = GetComponent(OtherScript); otherScript.DoSomething(); } 概览: 访问其他游戏物体 大多数高级的代码不仅需要操作一个物体。Unity 脚本接口有各种方法来找到并访问其 他游戏物体和组件。在下面,我们假定有一个名为 OtherScript.js 的脚本附加到场景的游戏 物体上。 var foo = 5; function DoSomething ( param : String) { print(param + " with foo: " + foo); } 1. 通过检视面板赋值引用. 你可以通过检视面板赋值变量到任何物体。 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 // 变换拖动到 target 槽的物体 var target : Transform; function Update () { target.Translate(0, 1, 0); } 你也可以在检视面板中公开到其他物体的引用。下面你可以拖动一个包含 OtherScript 的游戏物体到检视面板中的 target 槽。 // 设置在检视面板中赋值的 target 变量上的 foo,调用 DoSomething. var target : OtherScript; function Update () { //设置 target 物体的 foo 变量 target.foo = 2; // 调用 target 上的 DoSomething target.DoSomething("Hello"); } 2. 通过物体层次定位. 对于一个已存在的物体,可以通过游戏物体的 Transform 组件来找到子和父物体: // 找到脚本所附加的 // 游戏物体的子"Hand" transform.Find("Hand").Translate(0, 1, 0); 一旦在层次视图中找到这个变换,你可以使用 GetComponent 来获取其他脚本. //找到名为"Hand"的子. //在附加到它上面的 OtherScript 中, 设置 foo 为 2. transform.Find("Hand").GetComponent(OtherScript).foo = 2; //找到名为"Hand"的子. //调用附加到它上面的 OtherScript 脚本中的 DoSomething. transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello"); //找到名为"Hand"的子. //然后应用一个力到附加在 hand 上的刚体. transform.Find("Hand").rigidbody.AddForce(0, 10, 0); 你可以循环所有的子。 // 变换的所有子向上移动 10 个单位! for (var child : Transform in transform) { child.Translate(0, 1, 0); } 参考 Transform 类文档获取更多信息。 3.根据名称或标签定位. 你可以使用 GameObject.FindWithTag 和 GameObject.FindGameObjectsWithTag 搜索具有 特定标签的游戏物体。使用 GameObject.Find 根据名称查找物体. function Start () { // 按照名称 var go = GameObject.Find("SomeGuy"); go.transform.Translate(0, 1, 0); // 按照标签 var player = GameObject.FindWithTag("Player"); player.transform.Translate(0, 1, 0); } 你可以在结果上使用 GetComponent,在找到的游戏物体上得到任何脚本或组件。 function Start () { // 按名称 var go = GameObject.Find("SomeGuy"); go.GetComponent(OtherScript).DoSomething(); //按标签 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 var player = GameObject.FindWithTag("Player"); player.GetComponent(OtherScript).DoSomething(); } 一些特殊的物体有快捷方式,如主相机使用 Camera.main。 4. 作为参数传递. 一些事件消息在事件中包含详细信息。例如,触发器事件传递碰撞物体的 Collider 组件 到处理函数。 OnTriggerStay 给我们一个到碰撞器的引用。从这个碰撞器我们可以获取附加到其上的 刚体。 function OnTriggerStay (other : Collider ) { //如果另一个碰撞器也有一个刚体 / 应用一个力到它上面! if (other.rigidbody) { other.rigidbody.AddForce(0, 2, 0); } } 或者我们可以通过碰撞器获取附加在同一物体上的任何组件。 function OnTriggerStay (other : Collider ) { // 如果另一个碰撞器附加了 OtherScript // 调用它上面的 DoSomething. // 大多数时候碰撞器不会附加脚本, // 所以我们需要首先检查以避免 null 引用异常。 if (other.GetComponent(OtherScript)) { other.GetComponent(OtherScript).DoSomething(); } } 注意通过上述例子中的 other 变量,你可以访问碰撞物体中的任何组件。 5.一种类型的所有脚本 使用 Object.FindObjectsOfType 找到所有具有相同类或脚本名称的物体,或者使用 Object.FindObjectOfType 找到这个类型的第一个物体。 function Start () { // 找到场景中附加了 OtherScript 的任意一个游戏物体. var other : OtherScript = FindObjectOfType(OtherScript); other.DoSomething(); } 概览: 向量 Unity 使用 Vector3 类统一表示全体 3D 向量。3D 向量的不同组件可以通过 x, y 和 z成 员变量访问。 var aPosition : Vector3; aPosition.x = 1; aPosition.y = 1; aPosition.z = 1; 你也能够使用 Vector3 构造函数来同时初始化所有组件. var aPosition = Vector3(1, 1, 1); Vector3 也定义了一些常用的常量值. var direction = Vector3.up; // 与 Vector3(0, 1, 0);相同 单个向量上的操作可以使用下面方式访问: someVector.Normalize(); 使用多个向量的操作可以使用 Vector3 类函数: theDistance = Vector3.Distance(oneVector, otherVector); (注意你必须在函数名之前写 Vector3.来告诉 Javascript 在哪里找到这个函数. 这适用于 所有类函数。) 你也可以使用普通数学操作来操纵向量。 长宽高(北京)科技有限公司 长宽高(北京)科技有限公司 combined = vector1 + vector2; 查看 Vector3 类文档获取完整操作和可用属性的列表。 概览: 成员变量 & 全局变量变量 定义在任何函数之外的变量是一个成员变量。在 Unity 中这个变量可以通过检视面板来 访问。任何保存在成员变量中的值也可以自动随工程保存。 var memberVariable = 0.0; 上面的变量将在检视面板中显示为名为"Member Variable"的数值属性。 如果你设置变量的类型为一个组件类型(例如 Transform, Rigidbody, Collider, 任何脚本 名称, 等等.)然后你可以在检视面板中通过拖动一个游戏物体来设置它们。 var enemy : Transform; function Update() { if ( Vector3.Distance( enemy.position, transform.position ) < 10 ) { print("I sense the enemy is near!"); } } 你也可以创建私有成员变量。私有成员变量可以用来存储那些在该脚本之外不可见的状 态。私有成员变量不会被保存到磁盘并且在检视面板中不能编辑。当它被设置为调试模式时, 它们在检视面板中可见。这允许你就像一个实时更新的调试器一样使用私有变量。 private var lastCollider : Collider; function OnCollisionEnter( collisionInfo : Collision ) { lastCollider = collisionInfo.other; } 全局变量 你也可以使用 static 关键字创建全局变量。 这创造了一个全局变量,名为 someGlobal。 // 'TheScriptName.js'中的一个静态变量 static var someGlobal = 5; // 你可以在脚本内部像普通变量一样访问它 print(someGlobal); someGlobal = 1; 为了从另一个脚本访问它,你需要使用这个脚本的名称加上一个点和全局变量名。 print(TheScriptName.someGlobal); TheScriptName.someGlobal = 10; 概览: 实例化 实例化复制一个物体。包含所有附加的脚本和整个层次。它以你期望的方式保持引用: 到外部物体引用的克隆层次将完好,在克隆层次上到物体的引用将映射到克隆的物体。 实例化是难以置信的快和非常有用的。因此最大化地使用它是必要的。 例如,这里是一个小的脚本,当附加到一个带有碰撞器的刚体上时将销毁它自己并实例 化一个爆炸物体。 var explosion : Transform; // 当碰撞发生时销毁我们自己 // 并生成一个爆炸预设 function OnCollisionEnter () { Destroy (gameObject); var theClonedExplosion : Transform; 长宽高(北京)科技有限公司

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档

相关文档