Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AndrewD2 · Jun 22, 2016 at 05:10 AM · webglyieldcsv

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);
         }
     }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Jun 22, 2016 at 07:39 PM 0
Share

When you say "it does not find the file", what is the exact error recorded in the log?

avatar image AndrewD2 tanoshimi · Jun 22, 2016 at 08:15 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

CSV Parser compatible with WebGL? 0 Answers

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

In c#, how to yield until a download is complete? 1 Answer

C# version of yield when loading a scene? 1 Answer

yield WaitForSeconds makes GUI button disappear 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges