Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 Bypp · Jul 09, 2014 at 07:44 PM · c#locale

Get user's ISO 3166-1 alpha-2 country code

I'd like to know a reliable way to get the user's ISO 3166-1 alpha-2 (2-letter code, ex : US, CA) with c# in unity3d ?

I've tried RegionInfo.TwoLetterISORegionName but it returns an empty string.

Are they not implemented ?

How do I do this ?

The main error I'm getting here is that the TwoLetterISORegionName is empty when I try and get it. The CultureInfo and RegionInfo are always returning United States as the culture unless I use this workaround :

 [System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
 privatestaticexternintGetSystemDefaultLCID();
 
 RegionInfo regionInfo = newRegionInfo(GetSystemDefaultLCID());
 CultureInfo cultureInfo = newCultureInfo(GetSystemDefaultLCID());
 

However, the TwoLetterISORegionName is empty and the ThreeLetterWindowsRegionName returns something like "Canada", which is no good for me.

UPDATE :

Here's the Debug information I get using the code above :

     Debug.Log("----- REGION INFO -----"); // Empty
     Debug.Log("CurrencyEnglishName : " + regionInfo.CurrencyEnglishName); // Empty
     Debug.Log("CurrencyNativeName : " + regionInfo.CurrencyNativeName); // Not implemented exception
     Debug.Log("CurrencySymbol : " + regionInfo.CurrencySymbol); // CAD
     Debug.Log("DisplayName : " + regionInfo.DisplayName); // Empty
     Debug.Log("EnglishName : " + regionInfo.EnglishName); // Empty
     Debug.Log("GeoId : " + regionInfo.GeoId); // 959515600
     Debug.Log("IsMetric : " + regionInfo.IsMetric); // True
     Debug.Log("ISOCurrencySymbol : " + regionInfo.ISOCurrencySymbol); // Canadian Dollar
     Debug.Log("Name : " + regionInfo.Name); // Empty
     Debug.Log("NativeName : " + regionInfo.NativeName); // Empty
     Debug.Log("ThreeLetterISORegionName : " + regionInfo.ThreeLetterISORegionName); // Empty
     Debug.Log("ThreeLetterWindowsRegionName : " + regionInfo.ThreeLetterWindowsRegionName); // Canada
     Debug.Log("TwoLetterISORegionName : " + regionInfo.TwoLetterISORegionName); // Empty

Obviously those are wrong values (well it's right, I'm in Canada and all, but the 3 letter code isn't "Canada", and the geoid can't be that big a number last I checked), or I'm missing something !

Quick update : The GeoID returned is always different, which makes no sense at all, as if it was randomly generated. Also, my Unity version is 4.3.4f, so I believe I'm pretty much up to date.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bypp · Jul 11, 2014 at 08:41 PM

Finally ended up finding a solution... and I'm not liking it, but hell it works. Opened up Visual Express 2008 (important that it is 2008, because you can't change target .NET platform on later versions, I believe) and created a .dll using the exact same class (RegionInfo) that I would have used in Unity C#.

So in C++/CLR : Create a .dll with a function that takes a char* as a parameter, assign the 2 characters from the System::String you get from the TwoLetterISORegionName and thats it. Copy the .dll in the plugins folder in Unity.

In C#/Unity : Link the .dll using DllImport and your method name (the method will not take a char* here but a IntPtr). Allocate the bytes to the IntPtr here, then call the C++ function, then Read the bytes, (cast them as chars for every index), and finally don't forget to release the memory. Allocate, Read and Release were used with the Marshal class.

This wouldn't work very well with a ton of characters but since I have only 2 it's not too bad. I realize this is a sketchy solution at best and demands a lot of work but since the RegionInfo class doesn't work well in Unity this is what I came up with. Hope I might help someone out !

Comment
Add comment · Show 1 · 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
avatar image mtytel · Sep 16, 2014 at 01:15 AM 0
Share

I was afraid writing a plugin was the only option.. Here I go :/

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Renderer on object disabled after level reload 1 Answer


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