unity3D教程之访问其他组件.docx
文本预览下载声明
Components are attached to game objects. Attaching a Renderer component to a game object is what makes it render on the screen, attaching a Camera turns it into a camera object. All scripts are components thus they can be attached to game objects. 【狗刨学习网】组件附属于游戏物体.把一个 Renderer (渲染器)组件附到游戏对象,可以使游戏对象显示到场景,附一个 Camera (摄像机)可以把物体变成一个摄像机物体.所有脚本都是组件,因此都能附到游戏对象上.The most common components are accessible as simple member variables: 常用的组件可以通过简单的成员变量取得:ComponentAccessible asTransform transformRigidbody rigidbodyRenderer rendererCamera camera (only on camera objects)Light light (only on light objects)Animation animationCollider collider... etc.For the full list of predefined member variables see the documentation for the Component , Behaviour and MonoBehaviour classes. If the game object does not have a component of the type you wish to fetch, the above variables will be set to null. 要看所有的预定义成员变量,请查看文档 Component , Behaviour 和 MonoBehaviour 类.如果游戏对象中没有你想要取得的组件类型,上面的变量值将为null.Any component or script attached to a game object can be accessed through GetComponent.附在游戏对象上的组件或脚本可以通过GetComponent获取.C# JavaScript using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Awake() {transform.Translate(0, 1, 0);GetComponentTransform().Translate(0, 1, 0);}}transform.Translate(0, 1, 0);// is equivalent to //相当于GetComponent(Transform).Translate(0, 1, 0);Note the case difference between transform and Transform . The former is a variable (lower case), the latter is a class or script name (upper case). This case difference lets you to differentiate variables from classscript names. 注意transform 和 Transform 的差异.前面是一个变量(小写),后面是一个类或脚本名称(大写).这个不同之处可以让你区分变量,类和脚本名称.Applying what we have learned, we can now find any script or builtin component which is attached to the same game object using GetComponent . Please note that to make the following example work you need to have a script called OtherScript containing a
显示全部