NativeArrayManager.Convert

This commit is contained in:
ousttrue
2022-02-09 19:22:02 +09:00
parent 720f011ae2
commit abd1c5a840

View File

@@ -2,6 +2,7 @@ using System;
using System.Linq;
using System.Collections.Generic;
using Unity.Collections;
using System.Runtime.InteropServices;
namespace UniGLTF
{
@@ -56,5 +57,24 @@ namespace UniGLTF
array.CopyFrom(data);
return array;
}
/// <summary>
/// サイズの違う型にコピーする。
///
/// 例
/// NativeArray<ushort> => NativeArray<uint>
/// </summary>
public NativeArray<U> Convert<T, U>(NativeArray<T> src, Func<T, U> convert)
where T : struct
where U : struct
{
var bytes = CreateNativeArray<byte>(src.Length * Marshal.SizeOf<T>());
var dst = bytes.Reinterpret<U>(1);
for (int i = 0; i < src.Length; ++i)
{
dst[i] = convert(src[i]);
}
return dst;
}
}
}