init
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30d423b9d44d240e7a5d6bf14c711e45
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "Omarator.Mosissdk",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": []
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a8ada65d54024e918e57ccad4157d28
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
30
Packages/com.omarator.mosissdk/Runtime/RuntimeExample.cs
Normal file
30
Packages/com.omarator.mosissdk/Runtime/RuntimeExample.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Use this runtime example C# file to develop runtime code.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace Omarator.Mosissdk
|
||||
{
|
||||
/// <summary>
|
||||
/// Provide a general description of the public class.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Packages require XmlDoc documentation for ALL Package APIs.
|
||||
/// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/xml-documentation-comments
|
||||
/// </remarks>
|
||||
public class MyPublicRuntimeExampleClass
|
||||
{
|
||||
/// <summary>
|
||||
/// Provide a description of what this private method does.
|
||||
/// </summary>
|
||||
/// <param name="parameter1"> Description of parameter 1 </param>
|
||||
/// <param name="parameter2"> Description of parameter 2 </param>
|
||||
/// <param name="parameter3"> Description of parameter 3 </param>
|
||||
/// <returns> Description of what the function returns </returns>
|
||||
public int CountThingsAndDoStuff(int parameter1, int parameter2, bool parameter3)
|
||||
{
|
||||
return parameter3 ? (parameter1 + parameter2) : (parameter1 - parameter2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 362d6f07ea083437fa4a53500d86b64a
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Concurrent;
|
||||
using System;
|
||||
|
||||
public class UnityMainThreadDispatcher : MonoBehaviour
|
||||
{
|
||||
private static readonly ConcurrentQueue<Action> ExecutionQueue = new ConcurrentQueue<Action>();
|
||||
|
||||
public static void Enqueue(Action action)
|
||||
{
|
||||
ExecutionQueue.Enqueue(action);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
while (ExecutionQueue.TryDequeue(out var action))
|
||||
{
|
||||
action.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7bfde52f3a724ee3b494287fcd0aa8e
|
||||
Reference in New Issue
Block a user