Skip to content

English version

VirtueSky edited this page Jun 11, 2025 · 1 revision

🌍 How to detect regions in Unity

❓Problem Statement

  • Recently, I received a task: Change the logo/background for the loading screen if the user is in the Japanese market 🇯🇵.

  • When I got the task, I was confident it would be simple—just use RegionInfo.CurrentRegion.TwoLetterISORegionName to get the current region of the user's device. However, when running the game and logging the result, it didn’t work as expected. Even though I was in Vietnam, the results were inconsistent. I tested on various environments like Editor (Windows/macOS), iOS, and Android, but the results often returned US or IV, even though my device settings were set to Vietnam.

  • After researching, I found several others facing the same issue:

  • I still haven’t found an explanation for why the results are incorrect.

💡Solutions

  • During my research, I found several ways to determine the user's region:

🧩 Method 1: Detect via System Language

Example:

    private void Start()
    {
        if (Application.systemLanguage == SystemLanguage.Japanese)
        {
            // Handle logic
        }
    }

✅ Advantages:

  • Easy to use as it is built into Unity.
  • No access permissions required.

❌ Disadvantages:

  • Low accuracy because users can change their system language.

🧩 Method 2: Detect via Device Region

Example:

    private void Start()
    {
        if (RegionInfo.CurrentRegion.TwoLetterISORegionName.Equals("JP"))
        {
            // Handle logic
        }
    }

Example: Using system region unity

    private void Start()
    {
        if (RegionHelper.GetCurrentRegion().TwoLetterISORegionName.Equals("JP"))
        {
            // Handle logic
        }
    }

✅ Advantages:

  • Easy to use as it is built into Unity.
  • No access permissions required.

❌ Disadvantages:

  • Medium accuracy because users can change their device region (though this is rare).
  • Results may be incorrect in some cases (as mentioned above). However, I wrote a library to handle this issue and provide accurate results on Android/iOS.
  • You can use the system region unity library to get the current region of the device.

🧩 Method 3: Detect via Network IP Address

    void Start()
    {
        StartCoroutine(GetIPRegion());
    }

    IEnumerator GetIPRegion()
    {
        UnityWebRequest www = UnityWebRequest.Get("https://ipinfo.io/json");
        yield return www.SendWebRequest();

        if (www.result == UnityWebRequest.Result.Success)
        {
            Debug.Log(www.downloadHandler.text);
        }
        else
        {
            Debug.LogError("Error: " + www.error);
        }
    }

Result when running the game:

Screenshot 2025-06-11 at 22 48 17

✅ Advantages:

  • No permissions required (easily accessible via third-party APIs).
  • Can determine country, region, and city (usually accurate to the city level).

❌ Disadvantages:

  • Requires an internet connection (must be online to access the API).
  • Relies on third-party services (if the API is down or changes, results may be affected).
  • Requires JSON parsing to extract necessary information.
  • Results may be inaccurate if the user uses a VPN.

🧩 Method 4: Detect via GPS (Global Positioning System)

✅ Advantages:

  • High accuracy (can determine latitude, longitude, and altitude).
  • Useful for location-based games/apps (e.g., AR games, tracking).

❌ Disadvantages:

  • Requires location access permissions (on Android/iOS, user consent is needed).
  • Consumes battery and resources (especially for continuous tracking).
  • Not available in environments with poor signal (indoors, basements).

(If anyone has used this method, feel free to contribute an example. Thanks in advance! 😊)

📊 Summary of Advantages and Disadvantages of Region Detection Methods in Unity

💡Method ✅ Advantages ❌ Disadvantages
System Language - Easy to use with Application.systemLanguage
- No permissions required
- Low accuracy as users can change their language settings
Device Region - Easy to use with RegionInfo
- No permissions required
- Can be improved with libraries
- Medium accuracy
- May return incorrect values on some devices or operating systems
Network IP (via API) - Can accurately determine country, region, and city
- No permissions required
- Requires internet connection
- Relies on third-party APIs
- Inaccurate with VPN
- Requires JSON parsing
GPS - High accuracy
- Useful for location-based applications
- Requires location permissions
- Consumes battery
- Ineffective in poor signal areas

🔗 Reference library for more accurate device region detection: system-region-unity

📌 Conclusion

  • Each method has its own advantages and disadvantages. Depending on the specific requirements of your application, you can choose the most suitable method.

  • In my case, I chose Method 2 because:

    • It is easy to use, does not rely on third-party services, and does not require permissions.
    • Medium accuracy is sufficient for my needs since my game allows offline play.
    • Users rarely change their device region (or leave it on automatic settings).