我是 Unity 和 C# 的新手,但很多事情似乎都取决于理解这种语法:GetComponent<Renderer>()。有人可以检查我的理解吗?



GetComponent<Renderer>().material.color = Color.red;

含义:

<Renderer> means:
"GetComponent could accept various types, but in this case were sending it a <Renderer> type."

接近吗?

如果我对GetComponent进行逆向工程,它会像这样开始吗:

<T> GetComponent(<T> x) { 
someComponentOfTypeT = someOtherFunction(x);
return someComponentOfTypeT; 
}

GetComponent将返回对和对象的引用(如果游戏对象已附加此组件(,或返回null(如果不存在(

因此,如果你想要一个附加到游戏对象的刚体、音频源、碰撞器、渲染器,你可以调用GetComponent。或者如果你有一个自定义脚本;ExampleScript"源自其他成分,如MonoBehaviur。

编辑1:

你的想法是正确的

这是一个基本的c#语法

这就是它看起来像逆向工程的样子

public T GetComponent<T>()
{
var objectWithTypeT = UnitysInternalMethodToGetTheDesiredType();
return objectWithTypeT;
}

我建议你检查这个链接通用方法

使用JetBrains反编译器反编译的实际方法

[SecuritySafeCritical]
public unsafe T GetComponent<T>()
{
CastHelper<T> castHelper = new CastHelper<T>();
this.GetComponentFastPath(typeof (T), new IntPtr((void*) &castHelper.onePointerFurtherThanT));
return castHelper.t;
}

最新更新