0
\$\begingroup\$

I'm making a little game on Android with Unity. And I use XML file to store different languages using System.Xml. The language selection is done when I first start the game. When I run the game in Unity everything works fine - I select the language and the game goes on, but if I run it from Android nothing happens when I select the language. I connected my phone to the computer and looked at the game logs in cmd windows via adb.exe logcat, and found this:

06-19 20:11:59.724 24342 24382 I Unity   : jar:file:///data/app/com.eSnailStudio.CountTheNumber.Game-f0RdxK_Zfj2jXL2nqoA01A==/base.apk!/assets/Localization/Languages.xml
06-19 20:11:59.724 24342 24382 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
06-19 20:11:59.724 24342 24382 I Unity   :
06-19 20:11:59.749   604   604 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
06-19 20:11:59.751   604   604 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
06-19 20:12:00.002  1857  1857 D KeyguardUpdateMonitor: handleTimeUpdate
06-19 20:12:00.021 24342 24382 E Unity   : NotSupportedException: The URI prefix is not recognized.
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Net.WebRequest.Create (System.Uri requestUri, System.Boolean useUriBase) [0x000ae] in <6b80e2850f1c48e6a227f0818ca6b227>:0
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Net.WebRequest.Create (System.Uri requestUri) [0x00014] in <6b80e2850f1c48e6a227f0818ca6b227>:0
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Xml.XmlDownloadManager.GetNonFileStream (System.Uri uri, System.Net.ICredentials credentials, System.Net.IWebProxy proxy, System.Net.Cache.RequestCachePolicy cachePolicy) [0x00000] in <b307146a71634627bcac16b7fdded4d2>:0
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Xml.XmlDownloadManager.GetStream (System.Uri uri, System.Net.ICredentials credentials, System.Net.IWebProxy proxy, System.Net.Cache.RequestCachePolicy cachePolicy) [0x00022] in <b307146a71634627bcac16b7fdded4d2>:0
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Xml.XmlUrlResolver.GetEntity (System.Uri absoluteUri, System.String role, System.Type ofObjectToReturn) [0x00032] in <b307146a71634627bcac16b7fdded4d2>:0
06-19 20:12:00.021 24342 24382 E Unity   :   at System.Xml.XmlTextReaderImpl.OpenUrlDelegate (System.Object xmlResolver) [0x0000c] in <b

The first line is the path to the android languages xml file, which I output through Debug.Log(). Through Debug.Log() I also realized that the code stops on line xml.Load(Path); in method below in the LanguageSystem script:

public static void SetLanguage(string language)
{
    var xml = new XmlDocument();
    xml.Load(Path); // Here the code stops on Android

    Strings = new Hashtable();
    var element = xml.DocumentElement[language];
    if (element != null)
    {
        var elemEnum = element.GetEnumerator();
        while (elemEnum.MoveNext())
        {
            var xmlItem = (XmlElement)elemEnum.Current;
            Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
        }
    }
    else
    {
        Debug.LogError("The specified language does not exist: " + language);
    }
}

I also opened the .APK file of the game through the archiver. Attached a photo below + photo of game game logs.enter image description hereenter image description here

What is my mistake and how do I fix it?

\$\endgroup\$
4
  • \$\begingroup\$ "Path" in xml.Load(Path) is "jar:file:///data/app/com.eSnailStudio.CountTheNumber.Game-f0RdxK_Zfj2jXL2nqoA01A==/base.apk!/assets/Localization/Languages.xml" - the firts line of game logs. I get the path this way: Path = Application.streamingAssetsPath + "/Localization/Languages.xml"; \$\endgroup\$ Commented Jun 19, 2021 at 20:20
  • \$\begingroup\$ Is the path the same for Unity and Android runs? \$\endgroup\$
    – Vaillancourt
    Commented Jun 20, 2021 at 12:33
  • \$\begingroup\$ Have you considered using a Unity TextAsset and Addressables to load this instead? That way Unity can handle any cross-platform differences internally so your code doesn't need to fuss with it. \$\endgroup\$
    – DMGregory
    Commented Jun 20, 2021 at 17:14
  • \$\begingroup\$ Vaillancourt, No, but Application.streamingAssetsPath is a cross-platform solution docs.unity3d.com/Manual/StreamingAssets.html \$\endgroup\$ Commented Jun 21, 2021 at 17:35

1 Answer 1

1
\$\begingroup\$

Thanks to DMGregory. TextAsset is a great solution.

If anyone is interested, here is what the SetLanguage method looks like now:

public static void SetLanguage(string language)
{
    var xml = new XmlDocument();
    xml.LoadXml(InFile.text);

    Strings = new Hashtable();
    var element = xml.DocumentElement[language];
    if (element != null)
    {
        var elemEnum = element.GetEnumerator();
        while (elemEnum.MoveNext())
        {
            var xmlItem = (XmlElement)elemEnum.Current;
            Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
        }
    }
    else
    {
        Debug.LogError("The specified language does not exist: " + language);
    }
}

The way I get InFile:

InFile = Resources.Load<TextAsset>("Localization/Languages");

Unity documentation: https://docs.unity3d.com/ScriptReference/Resources.Load.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .