- Home /
Reading data from external text file for WebGL
I'm running a script that creates a deck of cards by reading from an external tsv file. I am using the Applications.streamingAssetsPath() method to get my file info. It works fine locally, but once I go to WebGL it does not find the file.
I've used the example from: https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html to try and fix this problem, I've modified it in various ways up to and including removing the if statement and just having it run the main part of the it (which doesn't work in the editor, but should work in a browser).
I am fairly new to Unity and if I can't fix this I'm wondering if there is a better option to set the data from instead of just reading this TSV file.
My script is:
using System.Collections;
using System.IO;
using UnityEngine;
public class CreateAlert : MonoBehaviour {
private string filePath;
private string result;
public GameObject alertCardPrefab;
private void Awake() {
Debug.Log("BeforeStartCoroutine result: " + result);
filePath = Path.Combine(Application.streamingAssetsPath, "AlertDeck.tsv");
StartCoroutine(FixPath());
Debug.Log("AfterStartCoroutine result: " + result);
}
// Use this for initialization
private void Start() {
Debug.Log("Start Result: " + result);
CreatAlertDeck();
}
// Update is called once per frame
private void Update() {}
private void CreatAlertDeck() {
Debug.Log("BeforeStringRead result: " + result);
StringReader sr = new StringReader(result);
string line;
while ((line = sr.ReadLine()) != null) {
// Debug.Log(line);
string[] alertCard = line.Split('\t');
int quantity = int.Parse(alertCard[7]);
for (int i = 0; i < quantity; i++) {
GameObject alertCardInstance = Instantiate(alertCardPrefab);
alertCardInstance.transform.SetParent(transform);
AlertCard alertCardObject = alertCardInstance.GetComponent<AlertCard>();
alertCardObject.Id = int.Parse(alertCard[0]);
alertCardObject.CardName = alertCard[1];
alertCardObject.AbilityText = alertCard[3];
alertCardObject.IsOverride = alertCard[4] == "Y" ? true : false;
alertCardObject.IsShip = alertCard[5] == "Y" ? true : false;
alertCardObject.IsAnamoly = alertCard[6] == "Y" ? true : false;
switch (alertCard[2]) {
case "Y":
alertCardObject.AlertColor = AlertCard.Alert.Yellow;
break;
case "O":
alertCardObject.AlertColor = AlertCard.Alert.Orange;
break;
case "R":
alertCardObject.AlertColor = AlertCard.Alert.Red;
break;
}
}
}
}
private IEnumerator FixPath() {
if (filePath.Contains("://")) {
WWW www = new WWW(filePath);
yield return www;
result = www.text;
Debug.Log("WWW " + result);
} else {
result = File.ReadAllText(filePath);
Debug.Log("NOT WWW: " + result);
}
}
}
When you say "it does not find the file", what is the exact error recorded in the log?
Ah, didn't know this had finally be approved.
Actually last night I had some help and fixed the problem. The reason I had thought it wasn't reading the file was because it was taking longer than the time between Awake() -> Start() to read it.
I got rid of the Start() and added Update() like this:
// Update is called once per frame
private void Update() {
if (result != null && isFirstRun) {
CreatAlertDeck();
}
}
Checking for a null value on "result" prevents it from trying to read the string the until the file is loaded into it. At the end of my CreateAlertDeck() method it sets isFirstRun to false to prevent it from running a second time.
As for what the exact error in the log was that it couldn't use a null string for the StringReader. Which it was still null because it hadn't finished downloading yet.