【发布时间】:2021-12-26 21:39:16
【问题描述】:
我需要编写一个基于参数抓取组件的函数。
我尝试使用字符串,然后是 GetType,但它不起作用。
我尝试使用 MonoBehaviour,但没有成功。我当前的版本如下。我错过了什么?
void move(MonoBehaviour co){
Touch screentouch = Input.GetTouch(0);
Ray ray = camera.ScreenPointToRay(screentouch.position);
if (screentouch.phase == TouchPhase.Began)
{
if (Physics.Raycast(ray, out RaycastHit hitInfo))
{
if (hitInfo.collider.gameObject.GetComponent<co>() != null)
{
set = true;
}
}
}
【问题讨论】:
-
您不能将参数用作泛型类型参数..这甚至根本不应该编译...
-
泛型参数需要在编译时解析(它本身可以是泛型类型;因此
GetComponent<co>()甚至不会编译)。 stackoverflow.com/a/33906910/2864740 是否解决了与动态类型/字符串名称相关的问题?