在 Unity 中,如何创建指针集合?
In Unity, how do you create a collection of pointers?
是否可以在 C# 中创建指针列表或数组?
我想要一个 T*
的列表,而不是使用 IntPtr
,因为我永远不得不在各处键入 Marshal Ptr To Structure
方法,而不是直接通过 ->
命令。
我尝试用 T* 创建一个列表,但它说它不能将它用作类型。那么我唯一的选择是在需要时从 IntPtr
不断转换吗?
可以声明一个指针数组。
unsafe Vector3*[] vectorPointerArray;
不能像List<Vector3*>
一样使用指针作为泛型参数,这还是under consideration.
因为可行性1,你可以创建一个集合来保存指针,虽然它不能实现像IList<T>
.
这样的通用接口
unsafe class UnsafePointerList<T> where T : unmanaged
{
private T*[] _items;
public T* this[int index] => _items[index];
}
UnsafePointerList<Vector3> vectorPointerList;
var x = vectorPointerList[0]->x;
是否可以在 C# 中创建指针列表或数组?
我想要一个 T*
的列表,而不是使用 IntPtr
,因为我永远不得不在各处键入 Marshal Ptr To Structure
方法,而不是直接通过 ->
命令。
我尝试用 T* 创建一个列表,但它说它不能将它用作类型。那么我唯一的选择是在需要时从 IntPtr
不断转换吗?
可以声明一个指针数组。
unsafe Vector3*[] vectorPointerArray;
不能像
List<Vector3*>
一样使用指针作为泛型参数,这还是under consideration.因为可行性1,你可以创建一个集合来保存指针,虽然它不能实现像
这样的通用接口IList<T>
.unsafe class UnsafePointerList<T> where T : unmanaged { private T*[] _items; public T* this[int index] => _items[index]; } UnsafePointerList<Vector3> vectorPointerList; var x = vectorPointerList[0]->x;