Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by tvobi1 · Dec 13, 2012 at 09:10 PM · c#iospluginsobjective-cunicode

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bompi88 · Dec 14, 2012 at 01:23 PM 0
Share

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/

avatar image andrew-fray · Mar 19, 2013 at 04:06 PM 0
Share

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.

avatar image Graham-Dunnett ♦♦ · Mar 19, 2013 at 09:03 PM 0
Share

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).

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to create an iOS plugin that takes recordings from the mic and plays it back through the speaker (live)? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges