#if UNITY_IOS using System.Runtime.InteropServices; #endif #if UNITY_ANDROID using UnityEngine; #endif namespace Google.Maps.Scripts { /// /// Script to get the country portion of the device's locale using native code on Android and iOS. /// public class DeviceCountryProvider : CountryProvider { /// Device's country code, populated during . private string CountryCode; /// /// When woken up by Unity, get the device's country code, if available. Do this once at the /// start, instead of in , because native calls can be expensive on some /// platforms. /// public void Awake() { #if UNITY_EDITOR // If running in the editor, don't try to run native code, even if the build target is one of // the mobile platforms below. CountryCode = null; #elif UNITY_ANDROID try { using (var localeClass = new AndroidJavaClass("java.util.Locale")) { using (var defaultLocale = localeClass.CallStatic("getDefault")) { CountryCode = defaultLocale.Call("getCountry"); } } } catch (System.Exception e) { Debug.LogWarningFormat("[Maps SDK] DeviceCountryError: " + "Couldn't get device country: {0}\nSee https://developers.google.com/maps/" + "documentation/gaming/support/error_codes#device-country-error for more information.", e); } #elif UNITY_IOS CountryCode = MuskGetLocaleRegion(); #else CountryCode = null; #endif } /// public override string GetCountry() { return CountryCode; } #if UNITY_IOS /// /// Native iOS function to get the device region. /// [DllImport("__Internal")] private static extern string MuskGetLocaleRegion(); #endif } }