团结,有组件方法 [英] Unity, Has Component Method

查看:179
本文介绍了团结,有组件方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写一个方法来检查,如果一个游戏物体有分量

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

下面是:

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("Does not have rigid body.");
    }
}



游戏对象没有刚性的身体,但它是仍然打印,它确实有。

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

推荐答案

这只是...

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

请注意,你忘了。

第一线的一部分

使用的语法错误,!扩展是没有意义的:它总是发现某些成分(因为T是一种空白),因此不能为null。这只是一个语法错误。

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.

请注意。

解释的到底是什么分机。

Explanation of "what the heck is an Extension".

有关的人读这是谁不熟悉C#类..这是说扩展在C#.. 。这里有一个简单的教程 ...

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

扩展在Unity是绝对关键的 - 你至少一次的每一行代码使用它们。 基本上在Unity你做一个扩展几乎所有的东西。注意OP甚至没有展示包装类。你必须是在这样的文件:

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 );
        }

    }

在我包括三个例子典型的扩展。通常情况下你有几十甚至上百人。 (您可能希望他们在不同的 HandyExtensions 文件组 - 包装类的文件名是完全不相干的。)每一位工程师和团队都有自己的共同扩展,他们使用所有的时间。

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.

请注意,这里例如的我问有关在类别,这是超越我的东西一个棘手的问题。 (谢天谢地EricL在那里!)顺便在大多数/一些/旧的语言,你一个类别调用它。在C#这是一个延伸,但人们说,无论是类别或扩展的所有时间。

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.

正如我说,你在Unity这些经常使用。干杯

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

如果你喜欢那种事情,这里有一个美丽的:

// 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];
    }

这篇关于团结,有组件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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