thieumao

LoadData - Read, Write, Download File in Unity3D

Jul 29th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.IO;
  5.  
  6. public class LoadData : MonoBehaviour {
  7.  
  8.     // Use this for initialization
  9.     void Start () {
  10.         WriteFile ("mao.txt");
  11.         // get
  12. //      string url = "http://example.com/script.php?var1=value2&var2=value2";
  13. //      WWW www = new WWW(url);
  14. //      StartCoroutine(WaitForRequest(www));
  15.  
  16.  
  17.         // post
  18. //      string url = "http://example.com/script.php";
  19. //      WWWForm form = new WWWForm();
  20. //      form.AddField("var1", "value1");
  21. //      form.AddField("var2", "value2");
  22. //      WWW www = new WWW(url, form);
  23. //      StartCoroutine(WaitForRequest(www));
  24.  
  25.         string url = "http://data.crackgame.net/test/DataPetvenger/levels.xml";
  26.         //"http://data.crackgame.net/test/DataPetvenger/DataGame";
  27.         WWW www = new WWW(url);
  28.         //StartCoroutine(WaitForRequest(www));
  29.  
  30.         StartCoroutine (TestDownload());
  31.     }
  32.  
  33.     private IEnumerator TestDownload()
  34.     {
  35.         string progress = "";
  36.         WWW www = new WWW("http://data.crackgame.net/test/DataPetvenger/levels.xml");
  37.         //yield return www;
  38.        
  39.         while (!www.isDone) {
  40.             progress = "downloaded " + (www.progress*100).ToString() + "%...";
  41.             //Debug.Log("progress " + progress);
  42.             yield return null;
  43.         }
  44.        
  45.         string fullPath = Application.persistentDataPath + "/levels.xml";
  46.         File.WriteAllBytes (fullPath, www.bytes);
  47.        
  48.         //progress = "downloaded, unzipping...";
  49.  
  50.     }
  51.  
  52.     IEnumerator WaitForRequest(WWW www)
  53.     {
  54.         yield return www;
  55.         // check for errors
  56.         if (www.error == null)
  57.         {
  58.             Debug.Log("WWW Ok!: " + www.data);
  59.         } else {
  60.             Debug.Log("WWW Error: "+ www.error);
  61.         }    
  62.     }      
  63.  
  64.     void WriteFile(string fileName){
  65.         string urlFile = Application.persistentDataPath + "/" +fileName;
  66.         if (File.Exists(urlFile))
  67.         {
  68.             Debug.Log(urlFile+" already exists.");
  69.             return;
  70.         }
  71.         Debug.Log ("Dang ghi file");
  72.         StreamWriter sr = File.CreateText(urlFile);
  73.         sr.WriteLine ("Thieu Mao");
  74.         sr.WriteLine ("Mot Mao");
  75.         sr.Close();
  76.     }
  77.  
  78.  
  79.  
  80.     void ReadFile(string file){
  81.        
  82.         if(File.Exists(file)){
  83.             var sr = File.OpenText(file);
  84.             var line = sr.ReadLine();
  85.             while(line != null){
  86.                 Debug.Log(line); // prints each line of the file
  87.                 line = sr.ReadLine();
  88.             }  
  89.         } else {
  90.             Debug.Log("Could not Open the file: " + file + " for reading.");
  91.             return;
  92.         }
  93.     }
  94.  
  95. }
Add Comment
Please, Sign In to add comment