- Home /
how to pass unicode text from C# to Objective-C
Hi, i need pass a localized text from c# to native objective-c code, i have the simple c# code:
public class main : MonoBehaviour { [DllImport("__Internal")] private static extern void _setText(string text);
void Start () {
_setText("Привет Мир");
}
}
and objective-c:
extern "C" void _setText(const char* text) { UIView* view = UnityGetGLView(); UILabel* label = [[[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
label.text = [NSString stringWithUTF8String:text];
label.font = [UIFont fontWithName:@"Arial" size:50];
[view addSubview:label];
}
with an english text it is work fine, but a localized text not convert to the expexted result.
also tried declare the c# signature method how
[DllImport("__Internal", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
ineffectually.
Have any ideas? Please help!
if think you have to substitute your characters in "Привет Мир" with code like "\\u03a0". So your unicode code should probably be "\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442 \\u041C\\u0438\\u0440". Take a look at the link for a converter: http://rishida.net/tools/conversion/
I'm trying to figure out the same thing. I'm guessing your _setText() func needs to take a wchar or similar wide character, so that the unicode string converts properly, but need to play more.
I think Andrew is pretty spot on. In ObjectiveC the type is shown as UTF8. I suspect in $$anonymous$$ono a string is UTF16 (aka wchar).
Answer by tdmowrer · Nov 16, 2018 at 01:12 AM
You need to specify the encoding in Objective C too. This should work on iOS:
C#:
class Foo
{
[DllImport("__Internal")]
static extern void SetText([MarshalAs(UnmanagedType.LPWStr)]string text, int textLength);
void Bar()
{
var text = "Привет Мир";
SetText(text, text.Length);
}
}
C/Objective-C:
extern "C" void SetText(const char16_t* text, int textLength)
{
NSString* textAsNsString = [[NSString alloc] initWithBytes: (const void*)text
length: sizeof(char16_t) * textLength
encoding: NSUTF16LittleEndianStringEncoding];
// Do something with textAsNsString
}
Your answer
Follow this Question
Related Questions
Cannot receive URL Scheme with App not running 1 Answer
Returning a byte array from ObjC to C# script on ios 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers