move to package
This commit is contained in:
91
Packages/com.omarator.mosissdk/Runtime/KotlinBridge.cs
Normal file
91
Packages/com.omarator.mosissdk/Runtime/KotlinBridge.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public class KotlinBridge : MonoBehaviour
|
||||
{
|
||||
static KotlinBridge Instance;
|
||||
Texture2D texture;
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct NativeCallbacks
|
||||
{
|
||||
public IntPtr OnMessage;
|
||||
public IntPtr OnServiceInitialized;
|
||||
public IntPtr OnFrameAvailable;
|
||||
public IntPtr OnBufferReady;
|
||||
public IntPtr OnTextureReady;
|
||||
}
|
||||
public delegate void OnMessageDelegate(string message);
|
||||
public delegate void OnServiceInitializedDelegate(bool success);
|
||||
public delegate void OnFrameAvailableDelegate();
|
||||
public delegate void OnBufferReadyDelegate();
|
||||
public delegate void OnTextureReadyDelegate(uint gl_texture);
|
||||
[DllImport("my_native_lib")]
|
||||
private static extern void SetNativeCallbacks(ref NativeCallbacks callbacks);
|
||||
[DllImport("my_native_lib")]
|
||||
private static extern IntPtr InitGLAD();
|
||||
[DllImport("my_native_lib")]
|
||||
private static extern IntPtr UpdateTexture();
|
||||
|
||||
[AOT.MonoPInvokeCallback(typeof(OnMessageDelegate))]
|
||||
static void OnMessage(string message)
|
||||
{
|
||||
//Debug.Log("High-speed callback: " + message);
|
||||
}
|
||||
|
||||
[AOT.MonoPInvokeCallback(typeof(OnServiceInitializedDelegate))]
|
||||
static void OnServiceInitialized(bool success)
|
||||
{
|
||||
Debug.Log("Service Initialized: " + success);
|
||||
}
|
||||
|
||||
[AOT.MonoPInvokeCallback(typeof(OnServiceInitializedDelegate))]
|
||||
static void OnFrameAvailable()
|
||||
{
|
||||
UnityMainThreadDispatcher.Enqueue(() => {
|
||||
GL.IssuePluginEvent(UpdateTexture(), 1);
|
||||
});
|
||||
}
|
||||
|
||||
[AOT.MonoPInvokeCallback(typeof(OnBufferReadyDelegate))]
|
||||
static void OnBufferReady()
|
||||
{
|
||||
Debug.Log("Buffer Ready");
|
||||
UnityMainThreadDispatcher.Enqueue(() => {
|
||||
GL.IssuePluginEvent(InitGLAD(), 1);
|
||||
});
|
||||
}
|
||||
|
||||
[AOT.MonoPInvokeCallback(typeof(OnTextureReadyDelegate))]
|
||||
static void OnTextureReady(uint gl_texture)
|
||||
{
|
||||
Debug.Log("Texture Ready: " + gl_texture);
|
||||
UnityMainThreadDispatcher.Enqueue(() => {
|
||||
var renderer = Instance.GetComponent<Renderer>();
|
||||
//renderer.material = new Material(Instance.oesShader);
|
||||
renderer.materials[2].mainTexture = Texture2D.CreateExternalTexture(1024, 1024,
|
||||
TextureFormat.RGBA32, false, false, (IntPtr)gl_texture);
|
||||
});
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Instance = this;
|
||||
NativeCallbacks callbacks = new NativeCallbacks();
|
||||
callbacks.OnMessage = Marshal.GetFunctionPointerForDelegate(new OnMessageDelegate(OnMessage));
|
||||
callbacks.OnServiceInitialized = Marshal.GetFunctionPointerForDelegate(new OnServiceInitializedDelegate(OnServiceInitialized));
|
||||
callbacks.OnFrameAvailable = Marshal.GetFunctionPointerForDelegate(new OnFrameAvailableDelegate(OnFrameAvailable));
|
||||
callbacks.OnBufferReady = Marshal.GetFunctionPointerForDelegate(new OnBufferReadyDelegate(OnBufferReady));
|
||||
callbacks.OnTextureReady = Marshal.GetFunctionPointerForDelegate(new OnTextureReadyDelegate(OnTextureReady));
|
||||
SetNativeCallbacks(ref callbacks);
|
||||
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
{
|
||||
using (var ktClass = new AndroidJavaClass("com.omixlab.mosis.unity.MyKotlinPlugin"))
|
||||
{
|
||||
ktClass.CallStatic("StartMosisService");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user