- Home /
Load and save text from web server
Hi all,
I'm doing an app for mobile, and i need to load text from web to use it after i unity.
So i would like to load the text and save it in a text asset (or .txt file).
There is my script :
using UnityEngine;
using System.Collections;
public class http : MonoBehaviour {
public string url = "http://www.mywebsite.com/largetextfile.txt";
public float progress = 0;
private string text;
IEnumerator demarre() {
WWW www = new WWW(url);
yield return www;
text = www.text;
progress = www.progress;
// how to save it in a file ?
}
}
When i debug (Debug.Log(www.text);)i have an error :
count <= std::numeric_limits::max() UnityEditor.DockArea:OnGUI() maxVertices < 65536 && maxIndices < 65536*3
HEy brother I am creating app for Android too I dont know coding much, searched everywhere but found only that TO make text change dynamicaly from outside we have to create server and link the text to that sever but that topic goes completly off to me I think you are trying to that same ,if you can help then please reply how to make text change I want to change the text of my mobile app somehow everyweek without rebuilding
Answer by ajketan · Jul 22, 2014 at 06:54 PM
Make sure that the "www.progress" part is completed 100% first.... and then you can call the following function. Stream reader and writer uses "System.IO" so you will have to make the header entry.
void WriteIntoMyLocalFile(string textFromServerFile)
{
string filePath;
filePath = Application.persistentDataPath + "/LocalLargeTextFile.txt";
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
using (StreamWriter writer = new StreamWriter(fs))
{
writer.Write(textFromServerFile);
writer.Close();
writer.Dispose();
}
fs.Close();
fs.Dispose();
}
}
Use this function... This part of code is tested on iOS, Android and WP8. It was a bit tricky to read the content from web for me....but I see that you have already figured that out.
Cheers
Ketan
I use Application.persistent data path because it is convenient and consistent throughout all the platforms... you can use your resource directories as well but I have not tested working with them...
Answer by WilliamLeu · Jul 22, 2014 at 06:39 PM
get the length of the text and see if it's over 16K characters. You might have a good string, but the editor might have problems displaying it to the debug log because it's too large.
As for saving it, easiest way is WriteAllText.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Trouble creating a text file 4 Answers
How do I load a 60mb text file? 0 Answers
Executing code from a text file and writing changes to it 1 Answer