(Unity)如何从GameObject中获取通用组件类?

问题描述 投票:-1回答:1

如何从产卵的GameObject中获取通用组件?

I.e我有一个基础通用类。

class Base<T> : MonoBehaviour where T : MonoBehaviour 
{
    public Foo() {}
}

和派生类。

class Example : Base<Example>
{

}

我不知道如何从产卵的GameObject中获取这个类,我把它作为一个组件。

我试过了,但我的结果是空的,所以我决定寻求一些帮助:)

// @object - Spawned before and exists.
Base<MonoBehaviour> tmp = @object.GetComponent<Base<MonoBehaviour>>();
c# unity3d generics gameobject
1个回答
0
投票

尝试使用接口实现。

class Base<T> : MonoBehaviour, IBase where T : MonoBehaviour 

并从GO中获取接口。

var tmp = @object.GetComponent<IBase>();
© www.soinside.com 2019 - 2024. All rights reserved.