被用作類型參數「T」我有以下方法:類型「T」不能在通用類型或方法
protected T AttachComponent<T>(){
T gsComponent = gameObject.GetComponent<T>();
if(gsComponent == null){
gsComponent = gameObject.AddComponent<T>();
}
return gsComponent;
}
在AddComponent
線我得到以下錯誤:
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'GameObject.AddComponent<T>()'.
There is no boxing conversion or type parameter conversion from 'T' to 'UnityEngine.Component'.
我不知道我能做些什麼來解決這個錯誤,爲什麼我不能這樣做?
你一定會做到這一點**,在C#中的擴展**。這裏是任何初學者閱讀http://stackoverflow.com/a/35629303/294884的教程希望它有幫助! – Fattie