查看: 1294|回复: 0
打印 上一主题 下一主题

[脚本章节]Overview: Accessing Other Components 访问其他组件

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-11 10:19:36 |只看该作者 |倒序浏览
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);GetComponent<Transform>().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 class&script 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 function DoSomething. The OtherScript script must be attached to the same game object as the following script.
应用我们学到的,我们现在能使用 GetComponent 找到附在游戏对象上的脚本或已有的组件.请注意下面的例子,假设你要调用一个名叫OtherScript的脚本,它包含有一个函数DoSomething. OtherScript脚本要附于下面脚本所在的同一游戏对象上.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {void Update() {System.Object otherScript = GetComponent<OtherScript>();otherScript.DoSomething();}}


// This finds the script called OtherScript in the same game object// 这个获得同一个物体上的OtherScript脚本// and calls DoSomething on it.// 访问他的 DoSomething .function Update () {    otherScript = GetComponent (OtherScript);    otherScript.DoSomething();}


来源: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 常用操作

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-18 20:29 , Processed in 0.190024 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部