Unity3D - 获得成分 [英] Unity3D - get component

查看:219
本文介绍了Unity3D - 获得成分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Unity3D C#

What is the simplest way to getcomponent in Unity3D C#?

我的情况下getcomponent最简单的方法:

My case:

GameObject gamemaster. 
//C# script MainGameLogic.cs(attached to gamemaster). 
A boolean backfacedisplayed(in MainGameLogic.cs).
A function BackfaceDisplay()(in MainGameLogic.cs).



另一个 C#剧本 FlipMech.cs ,我需要从 MainGameLogic.cs 来检查,如果(backfacedisplayed == TRUE),我会打电话给 BackfaceDisplay() MainGameLogic.cs >。我如何能做到这一点在C#?

Another C# script FlipMech.cs, which I need to check from MainGameLogic.cs if(backfacedisplayed == TRUE), I will call BackfaceDisplay()from MainGameLogic.cs. How can I do it in C#?

在JS是相当直截了当。在 FlipMech.js

In js is rather straight forward. In FlipMech.js:

//declare gamemaster
var gamemaster:GameObject;



那么无论我需要:

Then wherever I need:

if(gamemaster.GetComponent(MainGameLogic).backfacedisplayed==true)
{
    gamemaster.GetComponent(MainGameLogic).BackfaceDisplay();
}



但好像 C#的方式更加复杂,这一点。

But it seems like C# is way more complicated that this.

推荐答案

在C#中,使用这将让该组件,

in c#, you will get the component using this,

if(gamemaster.GetComponent<MainGameLogic>().backfacedisplayed==true)
    {
        gamemaster.GetComponent<MainGameLogic>().BackfaceDisplay();
    }

这篇关于Unity3D - 获得成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆