0

I am trying to read a text file which is placed on Server. Its a text file. I am able to read it but the problem is if Internet is slow it stuck the game for a while. May be I need to run this task as Asynchronous but I am not able to do this. I cant find any reasonable help online.

Please let me know how can I run a task in background or Asynchronously in Unity. Here is the function which I want to be executed in background.

private void readFile()
{
    Debug.Log("Trying to Read");
    WebClient client = new WebClient();
    Stream stream = client.OpenRead("my text file url.txt");
    reader = new StreamReader(stream);
    result = reader.ReadToEnd();
    Debug.Log(result);
}

Looking forward for quick help. Thanks

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Jawad Amjad
  • 2,532
  • 2
  • 29
  • 59

2 Answers2

1

You can use the WWW or WWWForm class to get this done... You just need to give the file download path and the downloaded content can be extracted from the .text property of the instantiated WWW object.

public class ExampleClass : MonoBehaviour {
public string url = "File download URL here";
IEnumerator Start() {
    WWW www = new WWW(url);
    yield return www;
    //retreive the file content using www.text
    //write your code here
}

If you need more info refer the Unity doc or this article uses the www to download an xml and display the data present

gameOne
  • 609
  • 1
  • 10
  • 22
  • The problem this code is that if the file is not there on the server it reads the page not found page and in this way the above method always read something which I dont require. Please suggest something which gives error or exception in the case mentioned. Also please let me know if this runs NOT on main thread but asynchronously. – Jawad Amjad Dec 22 '14 at 18:56
  • well, Im not sure what your error page looks like. However, there is a property called "error" which might help you in solving that issue. Having said that, I believe if it's a same "page not found" you can code accordingly so that you can detect that particular error...http://docs.unity3d.com/ScriptReference/WWW-error.html – gameOne Dec 23 '14 at 06:02
  • Here is what it looks like http://www.tenlogix.com/asdasd WWW read all the buttons and other stuff and it does not give an error unfortunately. – Jawad Amjad Dec 23 '14 at 06:03
  • and what does a text download link looks like? – gameOne Dec 23 '14 at 06:07
  • http://tenlogix.com/cupcakemania/Ads/com.tls.pancakemaker.txt Here is the correct link – Jawad Amjad Dec 23 '14 at 06:09
  • Unfortunately I dont have unity installed with me right now and cannot test whatever I say... But, I feel the error page should return the same content in www.text everytime and hence you can do a check if the content is similar to the error page content. – gameOne Dec 23 '14 at 06:17
  • yes you are right I think I will have to check the content for error. One more thing does this method runs in background or Asynchronously. – Jawad Amjad Dec 23 '14 at 06:19
  • yes, this does not interrupt the process execution in any way... this runs in the background.. You can check the documentation – gameOne Dec 23 '14 at 06:22
  • Thanks If you dont mind can you please help me with another problem here http://stackoverflow.com/questions/27600792/working-with-litjson-in-c-sharp-in-unity/27601629?noredirect=1#comment43630243_27601629 – Jawad Amjad Dec 23 '14 at 06:23
-1

Here's an option:

var task = new Task(readFile);
task.Start();
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118