1234567891011121314151617181920212223242526272829303132 |
- using UnityEngine;
- using System.Runtime.InteropServices;
- public class MyPlugin : MonoBehaviour
- {
- // 声明C++函数
- [DllImport("ThreadTool")]
- private static extern int Add(int a, int b);
- [DllImport("ThreadTool")]
- private static extern void CallCSharpFunction(Callback callback);
- // 定义回调委托
- public delegate int Callback(int value);
- // C#回调函数
- private int MyCallback(int value)
- {
- Debug.Log("C# received value from C++: " + value);
- return value * 2;
- }
- void Start()
- {
- // 调用C++函数
- int result = Add(5, 3);
- Debug.Log("Result from C++: " + result); // 输出: Result from C++: 8
- // 注册C#回调函数并调用C++函数
- CallCSharpFunction(MyCallback);
- }
- }
|