- Home /
How to get the streaming assets path in WebGL?
I know this has been asked thousand times, but I just can not see the error. I have a xml file placed in a streaming assets folder. I am able to read it for any platform except the WebGL. I test it in Firefox. There is not even a way to debug it but I know the problem is here because of the Firefox console. It says, the problem is in LanguageManager when calling the getString() - no strings just exists. See the code bellow:
public class LanguageManager : MonoBehaviour
{
private Hashtable Strings;
private string filePath;
void Awake()
{
if (Application.platform == RuntimePlatform.WindowsEditor) // If Unity Editor...
{
filePath = "Assets/StreamingAssets/Languages.xml";
}
else if (Application.platform == RuntimePlatform.WindowsPlayer) // If Standalone...
{
filePath = Application.dataPath + "/StreamingAssets/Languages.xml";
}
else if (Application.platform == RuntimePlatform.Android) // If Android...
{
string tempPath = System.IO.Path.Combine (Application.streamingAssetsPath, "Languages.xml");
// Android only use WWW to read file
WWW reader = new WWW (tempPath);
while (!reader.isDone) {
}
filePath = Application.persistentDataPath + "/db";
System.IO.File.WriteAllBytes (filePath, reader.bytes);
}
else if (Application.platform == RuntimePlatform.WebGLPlayer) // If WebGL ERROR HERE
{
filePath = System.IO.Path.Combine (Application.streamingAssetsPath, "Languages.xml");
}
string language = PlayerPrefs.GetString ("LanguageSettings", "English"); // Set Language.
var xml = new XmlDocument();
xml.Load(filePath); // Define path to the XML document.
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);
}
public string getString (string name)
{
if (!Strings.ContainsKey(name))
{
Debug.LogError("The specified string does not exist: " + name);
return "";
}
return (string)Strings[name];
}
}
Have your read the information in the docs? web platforms are handled the same as android
https://docs.unity3d.com/ScriptReference/Application-strea$$anonymous$$gAssetsPath.html
Thank you for your comment. Yes, I have read it. But as soon as I put this:
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WebGLPlayer)
it is not working as well.
It's not working in WebGL because you must not use things like this in webGL:
while (!reader.isDone) {
}
You have to use coroutines on webGL. See this documentation page, especially the section:
Do not block on WWW or WebRequest downloads
It's actually generally adviced to not do this in any build type.
Ok, I am closer now. I devided the script into two buttons. One button prints the path and the second button does the rest. The game even did not start because of this error. Now it starts. I press button 1 - it prints the path. The printed path is working on the computer. Then I press the second button and it crases, says: "Could not find file". It is strange. Path is correct, but the rest of the code can not find or access it. See the picture. I also changed to get the filePath, see the script bellow.
The path printed at top of the screen is working on my computer, but the path from the console at the bottom is not. What is this? I give it a path and it tries to find the file on the different path? Why?
public void Button1 ()
{
text.text = Application.dataPath + "/Strea$$anonymous$$gAssets/Languages.xml";
filePath = Application.dataPath + "/Strea$$anonymous$$gAssets/Languages.xml";
}
public void Button2()
{
string language = PlayerPrefs.GetString ("LanguageSettings", "English"); // Set Language.
var xml = new XmlDocument();
xml.Load(filePath); // Define path to the X$$anonymous$$L document.
Strings = new Hashtable();
var element = xml.DocumentElement[language];
if (element != null)
{
var elemEnum = element.GetEnumerator();
while (elemEnum.$$anonymous$$oveNext())
{
var xmlItem = (XmlElement)elemEnum.Current;
Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
}
}
else
Debug.LogError("The specified language does not exist: " + language);
}
Have you fix that? I'm in the same situation with a similar code @xxluky
Not sure, it has been a time ago. Now I am using this but I am not sure if that is working today. I still run my game on Android only.
else if (Application.platform == RuntimePlatform.WebGLPlayer) // If WebGL...
{
filePath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, "Languages.xml");
StartCoroutine(UserDetailsXmlPath());
}
IEnumerator UserDetailsXmlPath()
{
if (filePath.Contains ("://") || filePath.Contains (":///"))
{
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
result = www.downloadHandler.text;
}
else
{
result = File.ReadAllText (filePath);
}
var xml = new XmlDocument();
if (Application.platform != RuntimePlatform.WebGLPlayer)
xml.Load(filePath); // Define path to the X$$anonymous$$L document for all platforms except the WebGL.
else
xml.LoadXml (result); // Define path to the X$$anonymous$$L document only for WebGL.
}