返回介绍

Component.GetComponent 获取组件

发布于 2019-12-18 15:37:35 字数 2472 浏览 1045 评论 0 收藏 0

JavaScript => GetComponent(type: Type): Component;
C# => Component GetComponent(Type type);

Parameters 参数

typeThe type of Component to retrieve.
要获取的组件类型

Description 描述

Returns the component of Type /type/ if the game object has one attached, null if it doesn't.

如果游戏对象有附加type类型的组件,则返回,如果没有则为空。

JavaScript:

// Disable the spring on the HingeJoint component.
 
var hinge : HingeJoint; 
hinge = GetComponent(HingeJoint); 
hinge.useSpring = false;

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public HingeJoint hinge;
    void Example() {
        hinge = GetComponent<HingeJoint>();
        hinge.useSpring = false;
    }
}

JavaScript => GetComponent(): T; C# => T GetComponent();

Description 描述

Generic version. See the Generic Functions page for more details.

泛型版本。参见泛型函数页面了解更多细节。

JavaScript => GetComponent(type: string): Component; C# => Component GetComponent(string type);

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来访问组件。例如:

JavaScript:

// Disable the spring on the HingeJoint component.
 
var hinge : HingeJoint; 
hinge = GetComponent("HingeJoint"); 
hinge.useSpring = false; 

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public HingeJoint hinge;
    void Example() {
        hinge = GetComponent("HingeJoint") as HingeJoint;
        hinge.useSpring = false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文