2023-03-06 20:00:14 +00:00
|
|
|
using AOT;
|
|
|
|
using CsBindgen;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
2023-03-08 18:49:05 +00:00
|
|
|
using Unity.VisualScripting;
|
2023-03-06 20:00:14 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
2023-03-08 18:49:05 +00:00
|
|
|
|
|
|
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
|
|
|
public delegate int MyDelegate(int x, int y);
|
2023-03-06 20:00:14 +00:00
|
|
|
|
|
|
|
public class NewBehaviourScript : MonoBehaviour
|
|
|
|
{
|
2023-03-08 18:49:05 +00:00
|
|
|
public Text text1;
|
|
|
|
public Text text2;
|
|
|
|
|
|
|
|
static Text test_s;
|
|
|
|
|
|
|
|
|
2023-03-06 20:00:14 +00:00
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
unsafe void Start()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-03-08 18:49:05 +00:00
|
|
|
test_s = text1;
|
2023-03-06 20:00:14 +00:00
|
|
|
|
2023-03-08 18:49:05 +00:00
|
|
|
// var sum2 = NativeMethods.my_add(50, 49); // simple test
|
2023-03-06 20:00:14 +00:00
|
|
|
|
|
|
|
|
2023-03-08 18:49:05 +00:00
|
|
|
|
|
|
|
[MonoPInvokeCallback(typeof(Func<int, int, int>))]
|
|
|
|
static int MethodWithDebug(int x, int y)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var sum = x + y;
|
|
|
|
UnityEngine.Debug.Log("Call from Rust:" + sum);
|
|
|
|
test_s.text = "Call from Rust:" + sum.ToString();
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
test_s.text = "Exception from Rust:" + ex.ToString();
|
|
|
|
Debug.LogError(ex.ToString());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 20:00:14 +00:00
|
|
|
|
2023-03-08 18:49:05 +00:00
|
|
|
NativeMethods.csharp_to_rust(MethodWithDebug);
|
2023-03-06 20:00:14 +00:00
|
|
|
|
|
|
|
|
2023-03-08 18:49:05 +00:00
|
|
|
var f = NativeMethods.rust_to_csharp();
|
2023-03-06 20:00:14 +00:00
|
|
|
|
|
|
|
|
2023-03-08 18:49:05 +00:00
|
|
|
var sum2 = f(1000, 2000);
|
|
|
|
UnityEngine.Debug.Log("Invoke Rust Method:" + sum2);
|
|
|
|
text2.text = "Invoke Rust method:" + sum2;
|
2023-03-06 20:00:14 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2023-03-08 18:49:05 +00:00
|
|
|
text2.text = "Exceptio invoke rust:" + ex.ToString();
|
|
|
|
Debug.LogError(ex.ToString());
|
2023-03-06 20:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
|
|
|
|
static int Method(int x) => x * x;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[MonoPInvokeCallback(typeof(Func<int, int>))]
|
|
|
|
static int Callback(int x)
|
|
|
|
{
|
|
|
|
Debug.Log("Callback!");
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace System.Runtime.InteropServices
|
|
|
|
{
|
|
|
|
internal struct CLong
|
|
|
|
{
|
|
|
|
public int Value; // Windows = int, Unix x32 = int, Unix x64 = long
|
|
|
|
}
|
|
|
|
|
|
|
|
internal struct CULong
|
|
|
|
{
|
|
|
|
public uint Value; // Windows = uint, Unix x32 = uint, Unix x64 = ulong
|
|
|
|
}
|
|
|
|
}
|