讀取WebCamTexture內容是用Color32 type,但是Texture2D更新內容需要byte type。
使用StructLayout宣告可以將不同type資料共用同一個記憶體位址。
(Win/Mac測試ok)
[StructLayout(LayoutKind.Explicit)]
public struct Color32Bytes{
[FieldOffset(0)]
public byte[] byteArray;
[FieldOffset(0)]
public Color32[] colors;
}
colorData = new Color32Bytes();
colorData.colors = new Color32[webCamTexture.width * webCamTexture.height];
webCamTexture.GetPixels32(colorData.colors);
displayTexture.LoadRawTextureData(colorData.byteArray);
displayTexture.Apply();
Unity Native Plugin Fast C# Callback
使用MonoPInvokeCallback宣告,可以讓C++ Plugin呼叫C#函式。
(Android測試ok)
[C#]
// declare callback
delegate void RenderCallback(int request);
[MonoPInvokeCallback(typeof(RenderCallback))]
static void RenderFunc(int request) {
// do something
}
[DllImport("unity_plugin")]
static extern void RegisterCallbacks(RenderCallback cb);
[C++]
typedef void (*RenderCB)(int event);
static RenderCB sfpRender = nullptr;
extern "C" void RegisterCallbacks(RenderCB cb) {
sfpRender = cb;
}
沒有留言:
張貼留言