Mam następujący sposób:typu „T” nie może być używany jako typ parametru „t” w ogólnym typie lub metodzie
protected T AttachComponent<T>(){
T gsComponent = gameObject.GetComponent<T>();
if(gsComponent == null){
gsComponent = gameObject.AddComponent<T>();
}
return gsComponent;
}
W wierszu otrzymuję następujący błąd 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'.
Nie jestem pewien, co mogę zrobić, aby naprawić ten błąd, dlaczego nie mogę tego zrobić?
to na pewno to zrobić z rozszerzeniem ** ** w C#. Oto samouczek dla każdego początkującego czytającego http://stackoverflow.com/a/35629303/294884 Mam nadzieję, że to pomoże! – Fattie