Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
GameObject.GetComponent 获取组件
# [GameObject](GameObject.html).GetComponent 获取组件 function *GetComponent* (*type* : Type) : [Component](../Component/Component.html) *Description* 描述 Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function. 如果这个游戏物体包含一个类型为 type 的组件,则返回它。如果没有则为空。通过这个函数,你可以访问内建的组件或者脚本的组件。 GetComponent is the primary way of accessing other components. From javascript the type of a script is always the name of the script as seen in the project view. GetComponent 是访问别的组件的原始方法,对 javascript 来说,脚本的类型就是项目视图里面看到的脚本名称。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Start() { Transform curTransform; curTransform = gameObject.GetComponent<Transform>(); curTransform = gameObject.transform; } void Update() { ScriptName other = gameObject.GetComponent<ScriptName>(); other.DoSomething(); other.someVariable = 5; } } ``` ``` function Start () { var curTransform : Transform; curTransform = gameObject.GetComponent(Transform); // 这里等同于: curTransform = gameObject.transform; } function Update () { // 为了访问附属于这个游戏物体的别的脚本里共有的变量很函数 // (脚本名称是javascript文件的名称) var other : ScriptName = gameObject.GetComponent(ScriptName); // 调用这个脚本上的一个DoSomething函数 other.DoSomething (); // 设置别的脚本实例上的另外一个变量 other.someVariable = 5; } ``` • function *GetComponent* (*type* : string) : Component *Description* 描述 Returns the component with name type if the game object has one attached, null if it doesn't. 如果游戏物体有一个附加组件,则返回名为 type 的组件,如果没有则为空。 It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type. Example: 由于性能原因,最好使用 Type 的 GetComponent,而不是用字符串。不过有时你可能无法得到 Type,例如当你从 Javascript 访问 C#脚本时,这个时候你可以简单通过名字而不是 type 来访问组件。例如: * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { void Update() { ScriptName other; other = gameObject.GetComponent("ScriptName") as ScriptName; other.DoSomething(); other.someVariable = 5; } } ``` ``` function Update () { // To access public variables and functions // in another script attached to the same game object. // (ScriptName is the name of the javascript file) //在另一个脚本附加相同的游戏物体,访问公共变量和函数 //(ScriptName是javascript的文件名) var other : ScriptName; other = gameObject.GetComponent("ScriptName"); // Call the function DoSomething on the script //调用函数做一些事情 other.DoSomething (); // set another variable in the other script instance //设置在other脚本实例的另一个变量 other.someVariable = 5; } ```
da
2022年5月20日 22:56
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码