Search Unity

xml.Load(Path) doesn't work on Android

Discussion in 'Scripting' started by OMEGA-KRAKEN-GAMES, Jun 19, 2021.

  1. OMEGA-KRAKEN-GAMES

    OMEGA-KRAKEN-GAMES

    Joined:
    Mar 21, 2020
    Posts:
    2
    Hello, I'm making a little game on android. And I use xml files to store different languages. 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:

    Code (CSharp):
    1.     public static void SetLanguage(string language)
    2.     {
    3.         var xml = new XmlDocument();
    4.         xml.Load(Path); // Here the code stops on Android
    5.  
    6.         Strings = new Hashtable();
    7.         var element = xml.DocumentElement[language];
    8.         if (element != null)
    9.         {
    10.             var elemEnum = element.GetEnumerator();
    11.             while (elemEnum.MoveNext())
    12.             {
    13.                 var xmlItem = (XmlElement)elemEnum.Current;
    14.                 Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
    15.             }
    16.         }
    17.         else
    18.         {
    19.             Debug.LogError("The specified language does not exist: " + language);
    20.         }
    21.     }
    I also opened the .apk file of the game through the archiver. Attached a photo below + photo of game game logs. Photo.png Photo.png

    What is my mistake and how do I fix it?
     
  2. OMEGA-KRAKEN-GAMES

    OMEGA-KRAKEN-GAMES

    Joined:
    Mar 21, 2020
    Posts:
    2
    Thanks to DMGregory. `TextAsset` is a great solution.
    If anyone is interested, here is what the `SetLanguage` method looks like now:
    Code (CSharp):
    1.     public static void SetLanguage(string language)
    2.     {
    3.         var xml = new XmlDocument();
    4.         xml.LoadXml(InFile.text);
    5.         Strings = new Hashtable();
    6.         var element = xml.DocumentElement[language];
    7.         if (element != null)
    8.         {
    9.             var elemEnum = element.GetEnumerator();
    10.             while (elemEnum.MoveNext())
    11.             {
    12.                 var xmlItem = (XmlElement)elemEnum.Current;
    13.                 Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
    14.             }
    15.         }
    16.         else
    17.         {
    18.             Debug.LogError("The specified language does not exist: " + language);
    19.         }
    20.     }

    The way I get `InFile`:
    Code (CSharp):
    1. InFile = Resources.Load<TextAsset>("Localization/Languages");
    Unity documentation: https://docs.unity3d.com/ScriptReference/Resources.Load.html
     
    Last edited: Jun 22, 2021