传统的UnityPlayer.UnitySendMessage性能较差,在Android或IOS向Unity高频对话中效率较差,所以通过Android或ios直接调用C#对象效果更好。
C#代码
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;
public class UnityTest : MonoBehaviour {
public delegate void DebugTest (string text); //声明Callback类型
[DllImport ("__Internal")]
static extern void callBackInit (DebugTest debugEvent);
//Window系统去掉关键词前缀[MonoPInvokeCallback (typeof (DebugTest))],ios必须此关键词。
[MonoPInvokeCallback (typeof (DebugTest))] public static void DebugTestEvent (string text) {
Debug.Log (text);
}
}
向C#暴露的ios Test.h文件
typedef void (*DebugTest)(string text);
extern "C"{
extern void callBackInit(DebugTest debugEvent);
}
Test.m 文件
#include "Test.h"
void callBackInit(DebugTest debugEvent){
debugEvent("调用成功!");
}
Cocos+u3d开发交流群:521643513