O
OneJS3y ago
Singtaa

Oops sorry missed this one Yes right TS

Oops sorry, missed this one. Yes, right. TS types are all stripped when transpiled into js. So calling C# generic functions from TS won't work directly.
2 Replies
Singtaa
SingtaaOP3y ago
Another approach is to create a helper function/extension and use reflection.
public static class EntityManagerExtensions {
public static object ToComponentDataArray(this EntityQuery query, Type type, Allocator allocator) {
var m = typeof(EntityQuery).GetMethod("ToComponentDataArray", new[] { typeof(Allocator) });
m = m.MakeGenericMethod(type);
return m.Invoke(query, new object[] { allocator });
}
}
public static class EntityManagerExtensions {
public static object ToComponentDataArray(this EntityQuery query, Type type, Allocator allocator) {
var m = typeof(EntityQuery).GetMethod("ToComponentDataArray", new[] { typeof(Allocator) });
m = m.MakeGenericMethod(type);
return m.Invoke(query, new object[] { allocator });
}
}
Basically, for your example, you can make a Register function that takes in a Type type parameter. Beside being more flexible, this also makes the TS calling code a little easier to write:
import { GameObject, MeshRenderer, PrimitiveType } from "UnityEngine"

var mr = GameObject.CreatePrimitive(PrimitiveType.Sphere).GetComponent(MeshRenderer)
import { GameObject, MeshRenderer, PrimitiveType } from "UnityEngine"

var mr = GameObject.CreatePrimitive(PrimitiveType.Sphere).GetComponent(MeshRenderer)
(You can directly feed MeshRenderer into the GetComponent() call)
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server