xcode 如何将字符串从原生 iOS 插件返回到统一?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37047781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:47:31  来源:igfitidea点击:

How to return string from native iOS plugin to unity?

xcodeunity3d

提问by Rohith Srinivas

I am creating an iOS Plugin which needs to return a string (or const char*) to Unity. How do I implement it ?

我正在创建一个 iOS 插件,它需要向 Unity 返回一个字符串(或 const char*)。我该如何实施?

回答by Cabrra

    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:""];
}

回答by Shpand

I'd like to clarify previous answer. C# declaration:

我想澄清以前的答案。C#声明:

[DllImport("__Internal")]
private static extern string getString();

Returning a string from objc is exactly like @Cabrra said:

从 objc 返回一个字符串就像@Cabrra 说的:

char* convertNSStringToCString(const NSString* nsString)
{
    if (nsString == NULL)
        return NULL;

    const char* nsStringUtf8 = [nsString UTF8String];
    //create a null terminated C string on the heap so that our string's memory isn't wiped out right after method's return
    char* cString = (char*)malloc(strlen(nsStringUtf8) + 1);
    strcpy(cString, nsStringUtf8);

    return cString;
}

extern "C" char* getString()
{
    const NSString* str = @"string";
    return convertNSStringToCString([carrier carrierName]);
}

Though some people mentioned that this way will lead to a memory leak that is not right. This way works fine in Unity and no leaks occurs (I tested it many times). Unity clearly statesthat

虽然有人提到这种方式会导致内存泄漏,但这是不对的。这种方式在 Unity 中工作正常并且不会发生泄漏(我测试了很多次)。统一明确指出的是

String values returned from a native method should be UTF–8 encoded and allocated on the heap. Mono marshalling calls free for strings like this.

从本机方法返回的字符串值应采用 UTF-8 编码并在堆上分配。像这样的字符串可以免费使用 Mono 编组调用。