unity to oc 其实是 unity to c to oc啦
1 C# 头文件
using System.Runtime.InteropServices;
2 C# 方法
#if UNITY_IOS
[DllImport("__Internal")]
private static extern string goCommunitet();
#else
private static string goCommunitet(){}
#endif
3 C 方法 –注意:如果返回的是string或者object注意你返回的引用的生命周期哦否则会崩溃的
extern "C"
{
char* MakeStringCopy(const char* string)
{
if (string == NULL)
return NULL;
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
const char* goCommunitet(){
string sz ="1234r54657";
return MakeStringCopy(sz.c_str());
}
}
oc to unity
C 方法
UnitySendMessage(gameobject的名字, 脚本方法名, 传递参数);
例如:UnitySendMessage("Directional Light", "function", sz.c_str());