- Home /
How to read text file (.txt) from URL?
I need is to load a text file from my web server.
Answer by Fattie · Jan 25, 2016 at 09:29 PM
/* WebTextFileChecker.cs
This checks the text in a text file on the web - believe it!
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WebTextFileChecker:MonoBehaviour
{
public void Start()
{
StartCoroutine(Check());
}
private IEnumerator Check()
{
WWW w = new WWW("http://yourserver.com/handyFile.txt");
yield return w;
if (w.error != null)
{
Debug.Log("Error .. " +w.error);
// for example, often 'Error .. 404 Not Found'
}
else
{
Debug.Log("Found ... ==>" +w.text +"<==");
// don't forget to look in the 'bottom section'
// of Unity console to see the full text of
// multiline console messages.
}
/* example code to separate all that text in to lines:
longStringFromFile = w.text
List<string> lines = new List<string>(
longStringFromFile
.Split(new string[] { "\r","\n" },
StringSplitOptions.RemoveEmptyEntries) );
// remove comment lines...
lines = lines
.Where(line => !(line.StartsWith("//")
|| line.StartsWith("#")))
.ToList();
*/
}
}
Answer by ina · Dec 25, 2010 at 05:00 PM
1) Use myWWW = WWW(url);
to fetch data from your server.
2) Parse the resulting string for the data segments you need. myWWW.text
You will probably need a couple of helper functions.
wwwStr2Array()
converts a string you might get from your WWW fetch to an array. http://answers.unity3d.com/questions/33243/array-dump-to-string-for-send-via-www/33351#33351
You can use the same idea if your WWW returns a string like: username=fred&password=flintstones
- you would just put it through the delimiter split in the above link again.
Once your values are in arrays, you can just manipulate them as you would usually.
This Does not work on iPADs. Do you have any other option?
Your answer
![](https://koobas.hobune.stream/wayback/20220613073052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Transfer closed with bytes remaining to read 1 Answer
load file with www on webplayer 1 Answer
Why does accessing www.text blocks the coroutine using WWW class? 1 Answer
How do I load a 60mb text file? 0 Answers
Internet reachability on iOS 4 Answers