Overview: Accessing Other Components 訪問其他組件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: 常用的組件可以通過簡單的成員變量取得: Component Accessible as Transform transform Rigidbody rigidbody Renderer renderer Camera camera (only on camera objects) Light light (only on light objects) Animation animation Collider 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獲取.
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 class&script names. 注意transform 和 Transform 的差異.前面是一個變量(小寫),后面是一個類或腳本名稱(大寫).這個不同之處可以讓你區(qū)分變量,類和腳本名稱. 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 function DoSomething. The OtherScript script must be attached to the same game object as the following script. 應用我們學到的,我們現在能使用 GetComponent 找到附在游戲對象上的腳本或已有的組件.請注意下面的例子,假設你要調用一個名叫OtherScript的腳本,它包含有一個函數DoSomething. OtherScript腳本要附于下面腳本所在的同一游戲對象上.
最后修改:2011年5月16日 Monday 21:49
|
|