バインドされた (UnityEngine.Component) リストをジェネリック (T) リストに変換する必要がありますが、これは可能ですか? どうやって?
私は Unity と C# を使用していますが、一般的にどのようにこれを行うのか知りたいです。
List<Component> compList = new List<Component>();
foreach(GameObject obj in objects) // objects is List<GameObject>
{
var retrievedComp = obj.GetComponent(typeof(T));
if(retrievedComp != null)
compList.Add(retrievedComp);
}
List<T> newList = new List<T>(compList as IEnumerable<T>); // ERROR HERE
foreach(T n in newList)
Debug.Log(n);
ありがとう!
それが問題だと思います。このランタイムエラーが発生しています...
ArgumentNullException: Argument cannot be null.
Parameter name: collection
System.Collections.Generic.List`1[TestPopulateClass].CheckCollection (IEnumerable`1 collection)
System.Collections.Generic.List`1[TestPopulateClass]..ctor (IEnumerable`1 collection)
DoPopulate.AddObjectsToList[TestPopulate] (System.Reflection.FieldInfo target) (at Assets/Editor/ListPopulate/DoPopulate.cs:201)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters)
DoPopulate.OnGUI () (at Assets/Editor/ListPopulate/DoPopulate.cs:150)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)