Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using System.IO;
- public class LoadData : MonoBehaviour {
- // Use this for initialization
- void Start () {
- WriteFile ("mao.txt");
- // get
- // string url = "http://example.com/script.php?var1=value2&var2=value2";
- // WWW www = new WWW(url);
- // StartCoroutine(WaitForRequest(www));
- // post
- // string url = "http://example.com/script.php";
- // WWWForm form = new WWWForm();
- // form.AddField("var1", "value1");
- // form.AddField("var2", "value2");
- // WWW www = new WWW(url, form);
- // StartCoroutine(WaitForRequest(www));
- string url = "http://data.crackgame.net/test/DataPetvenger/levels.xml";
- //"http://data.crackgame.net/test/DataPetvenger/DataGame";
- WWW www = new WWW(url);
- //StartCoroutine(WaitForRequest(www));
- StartCoroutine (TestDownload());
- }
- private IEnumerator TestDownload()
- {
- string progress = "";
- WWW www = new WWW("http://data.crackgame.net/test/DataPetvenger/levels.xml");
- //yield return www;
- while (!www.isDone) {
- progress = "downloaded " + (www.progress*100).ToString() + "%...";
- //Debug.Log("progress " + progress);
- yield return null;
- }
- string fullPath = Application.persistentDataPath + "/levels.xml";
- File.WriteAllBytes (fullPath, www.bytes);
- //progress = "downloaded, unzipping...";
- }
- IEnumerator WaitForRequest(WWW www)
- {
- yield return www;
- // check for errors
- if (www.error == null)
- {
- Debug.Log("WWW Ok!: " + www.data);
- } else {
- Debug.Log("WWW Error: "+ www.error);
- }
- }
- void WriteFile(string fileName){
- string urlFile = Application.persistentDataPath + "/" +fileName;
- if (File.Exists(urlFile))
- {
- Debug.Log(urlFile+" already exists.");
- return;
- }
- Debug.Log ("Dang ghi file");
- StreamWriter sr = File.CreateText(urlFile);
- sr.WriteLine ("Thieu Mao");
- sr.WriteLine ("Mot Mao");
- sr.Close();
- }
- void ReadFile(string file){
- if(File.Exists(file)){
- var sr = File.OpenText(file);
- var line = sr.ReadLine();
- while(line != null){
- Debug.Log(line); // prints each line of the file
- line = sr.ReadLine();
- }
- } else {
- Debug.Log("Could not Open the file: " + file + " for reading.");
- return;
- }
- }
- }
Add Comment
Please, Sign In to add comment