- Home /
android xml language issue
hi guys i am tryin to make multilanguage. it work well on unity but not working on android device.
#if UNITY_EDITOR
LMan = new Lang(Path.Combine(Application.dataPath, "Resources/lang.xml"), currentLang, false);
#elif UNITY_ANDROID
LMan = new Lang(Path.Combine(Application.persistentDataPath, "lang.xml"), currentLang, false);
//i tried other ways too, without ifelse etc.;
LMan = new Lang(Path.Combine(Application.dataPath, "Resources/lang.xml"), currentLang, false);
and here is xml load part of code.
public function setLanguage ( path : String, language : String) {
var xml : XmlDocument = new XmlDocument();
xml.Load(path);
Strings = new Hashtable();
var element : XmlElement = xml.DocumentElement.Item[language];
if (element) {
var elemEnum : IEnumerator = 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);
}
}
Answer by Bunny83 · Apr 25, 2015 at 10:38 AM
Assets that are in your project can't be reached via file IO as all assets are packed into asset files inside the apk. Application.persistentDataPath is a location where your app can store data. This location is initially empty. You won't find there anthing you didn't put there first.
You can use Streaming Assets by placing your files inside a folder called "StreamingAssets". That way those files are just packed into the apk (which is just a zip file) instead of getting packed as asset.
Though if the language files should be packed into the project as assets, you have two options:
Place them in a "Resources" folder and use Resources.Load to get back a TextAsset.
Directly use the asset by declaring a public variable of type "TextAsset" (could be an array) and just assign your xml file(s) in the inspector to this(those) variable(s)
The public variable approach is usually the prefered one. Using the Resources folder has some disadvantages as all assets in Resources folders are fetched right at start. StreamingAssets would work as well, but it's easier to gather / manipulate by endusers as they are directly inside the apk.
cheers man it worked. here is working code
var asset : TextAsset;
public function setLanguage ( path : String, language : String) {
asset = Resources.Load("lang") as TextAsset;
var xml : XmlDocument = new XmlDocument();
xml.LoadXml(asset.text);
Your answer
Follow this Question
Related Questions
XmlWriter.Create exception, "Could not find the file..." on Android 0 Answers
Load file on Android 1 Answer
Epic slow android build time, lots of Read/Write enabled sprites! 1 Answer
Get external .xml in mobile 1 Answer
xmlDocument.loadXml/load raises NotImplementedException on android, works in editor 1 Answer