MyPlugin.cs 792 B

1234567891011121314151617181920212223242526272829303132
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. public class MyPlugin : MonoBehaviour
  4. {
  5. // 声明C++函数
  6. [DllImport("ThreadTool")]
  7. private static extern int Add(int a, int b);
  8. [DllImport("ThreadTool")]
  9. private static extern void CallCSharpFunction(Callback callback);
  10. // 定义回调委托
  11. public delegate int Callback(int value);
  12. // C#回调函数
  13. private int MyCallback(int value)
  14. {
  15. Debug.Log("C# received value from C++: " + value);
  16. return value * 2;
  17. }
  18. void Start()
  19. {
  20. // 调用C++函数
  21. int result = Add(5, 3);
  22. Debug.Log("Result from C++: " + result); // 输出: Result from C++: 8
  23. // 注册C#回调函数并调用C++函数
  24. CallCSharpFunction(MyCallback);
  25. }
  26. }