xcode – 如何将字符串从本机iOS插件返回到统一?

xcode – 如何将字符串从本机iOS插件返回到统一?,第1张

概述我正在创建一个iOS插件,需要将一个字符串(或const char *)返回给Unity.我该如何实现它? extern "C"{ int _pow2(int x) { // Just a simple example of returning an int value return x * x; } // Returns a c 我正在创建一个iOS插件,需要将一个字符串(或const char *)返回给Unity.我该如何实现它?解决方法
extern "C"{    int _pow2(int x)    {        // Just a simple example of returning an int value        return x * x;    }    // Returns a char* (a string to Unity)    char* _helloWorldString()    {        // We can use Nsstring and go to the c string that Unity wants        Nsstring *helloString = @"Hello World";        // UTF8String method gets us a c string. Then we have to malloc a copy to give to Unity. I reuse a method below that makes it easy.        return cStringcopy([helloString UTF8String]);    }    // Here is an example of getting a string from Unity    char* _combinestrings(const char* cString1,const char* cString2)    {        // This shows we can create two Nsstrings* from the c strings from Unity        Nsstring *string1 = CreateNsstring(cString1);        Nsstring *string2 = CreateNsstring(cString2);        Nsstring *combinedString = [Nsstring stringWithFormat:@"%@ %@",string1,string2];        // Same as before,have to go to a c string and then malloc a copy of it to give to Unity        return cStringcopy([combinedString UTF8String]);    }}//I also like to include these two convenIEnce methods to convert between c string and Nsstring*. You need to return a copy of the c string so that Unity handles the memory and gets a valID value.char* cStringcopy(const char* string){    if (string == NulL)        return NulL;    char* res = (char*)malloc(strlen(string) + 1);    strcpy(res,string);    return res;}// This takes a char* you get from Unity and converts it to an Nsstring* to use in your objective c code. You can mix c++ and objective c all in the same file.static Nsstring* CreateNsstring(const char* string){    if (string != NulL)        return [Nsstring stringWithUTF8String:string];    else        return [Nsstring stringWithUTF8String:""];}
总结

以上是内存溢出为你收集整理的xcode – 如何将字符串从本机iOS插件返回到统一?全部内容,希望文章能够帮你解决xcode – 如何将字符串从本机iOS插件返回到统一?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1048884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存