如何检查一个游戏对象是否在Unity中有一个组件方法? [英] How to check if a game object has a component method in Unity?

查看:3776
本文介绍了如何检查一个游戏对象是否在Unity中有一个组件方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是:

<$>
p> public static bool HasComponent< T>(this GameObject obj)
{
return obj.GetComponent< T>()!= null;
}

我正在使用它:

  void Update()
{

if(Input.GetKey(w))
{
if(gameObject.HasComponent< Rigidbody>())
{
print(Has a rigid body。);
return;
}

print(没有刚体。);


gameObject没有刚体,但它是仍然打印它。

解决方案



  public static bool HasComponent< T>(this GameObject obj)where T:Component 
{
return obj.GetComponent< T>()!= null ;
}

请注意,您忘记了

其中T:Component



第一行的一部分!

有了这个语法错误,扩展是没有意义的:它总是找到某个组件(因为T有点空白),因此永远不会为空。这只是一个语法错误。






注意。



解释什么是扩展。



对于读者来说,不熟悉c#中的类别,也就是说c#中的Extensions。 。这里有一个简单的



扩展在Unity中绝对至关重要 - 您至少在每行中使用它们一次的代码。 基本上在Unity中,你几乎可以做任何扩展。请注意,OP甚至不会显示包装类。你可以把它放在这样的文件中:

  public static class ExtensionsHandy //这个类名是不相关的,没有使用


public static bool HasComponent< T>(this GameObject obj)where T:Component
{
return obj.GetComponent< T>()!= null ;


public static bool IsNear(this float ff,float target)
{
float difference = ff-target;
difference = Mathf.Abs(difference);
if(差值<20f)返回true;
else返回false;


public static float Jiggle(this float ff)
{
return ff * UnityEngine.Random.Range(0.9f,1.1f);


public static Color Colored(float float alpha,int r,int g,int b)
{
return new Color(
(float )r / 255f,
(float)g / 255f,
(float)b / 255f,
alpha);
}

}

在这个例子中,典型的扩展。通常你会有几十甚至几百个。 (您可能更喜欢将它们分组到不同的 HandyExtensions 文件中 - 封装类的文件名完全不相关)。每个工程师和团队都有自己的公用扩展名所有的时间。



请注意,这里例如范例我对一个超出我的范畴的东西提出了一个棘手的问题。 (谢天谢地EricL在那里!)顺便说一句,在大多数/某些/较老的语言中,你称之为类别。在c#中它是一个扩展,但人们总是说类别或扩展。正如我所说,你在Unity中经常使用它们。干杯






如果你喜欢那样的话,这里有一个漂亮的:

  //游戏中令人难以置信的有用数组处理类别! 

public static T AnyOne< T>(this T [] ra)where T:class
{
int k = ra.Length;
int r = UnityEngine.Random.Range(0,k);
return ra [r];
}


I am writing a method to check if a gameObject has a component.

Here it is:

public static bool HasComponent <T>(this GameObject obj)
{
    return obj.GetComponent<T>() != null;
}

And I'm using it like this:

void Update()
{

    if (Input.GetKey("w"))
    {
        if (gameObject.HasComponent<Rigidbody>())
        {
            print("Has a rigid body.");
            return;
        }

        print("Does not have rigid body.");
    }
}

The gameObject does NOT have a rigid body but it is still printing that it does have.

解决方案

It is just...

public static bool HasComponent <T>(this GameObject obj) where T:Component
    {
    return obj.GetComponent<T>() != null;
    }

Note that you forgot the

where T:Component

part of the first line!

With that syntax error, the extension is meaningless: it's always finding "some component" (since T is sort of "blank") and hence is never null. It's just a syntax error.


NOTE.

Explanation of "what the heck is an Extension".

For anyone reading this who is not familiar with categories in c# .. that is to say "Extensions" in c# ... here's a easy tutorial ...

Extensions are absolutely critical in Unity - you use them at least once in every line of code. Basically in Unity you do almost everything in an Extension. Note that the OP did not even bother showing the wrapper class. You'd have it in a file like this:

public static class ExtensionsHandy // this class name is irrelevant and not used
    {

    public static bool HasComponent <T>(this GameObject obj) where T:Component
        {
        return obj.GetComponent<T>() != null;
        }

    public static bool IsNear(this float ff, float target)
        {
        float difference = ff-target;
        difference = Mathf.Abs(difference);
        if ( difference < 20f ) return true;
        else return false;
        }

    public static float Jiggle(this float ff)
        {
        return ff * UnityEngine.Random.Range(0.9f,1.1f);
        }

    public static Color Colored( this float alpha, int r, int g, int b )
        {
        return new Color(
            (float)r / 255f,
            (float)g / 255f,
            (float)b / 255f,
            alpha );
        }

    }

In the example I included three more typical extensions. Normally you'd have dozens or even hundreds of them. (You may prefer to group them in different HandyExtensions files - the file name of the wrapper class is totally irrelevant.) Every engineer and team has their own "common extensions" they use all the time.

Note that here for example EXAMPLE I was asking a tricky question about something in an category that was beyond me. (Thank goodness EricL was there!) By the way in most/some/older languages you call this a "category". In c# it is an "extension", but people say either "category" or "extension" all the time.

As I say, you use these constantly in Unity. Cheers


If you like that sort of thing, here's a beautiful one:

// the unbelievably useful array handling category for games!

public static T AnyOne<T>(this T[] ra) where T:class
    {
    int k = ra.Length;
    int r = UnityEngine.Random.Range(0,k);
    return ra[r];
    }

这篇关于如何检查一个游戏对象是否在Unity中有一个组件方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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