[unity3d]保存文件到本地and加載本地文件

今天要作一個移動平臺的版本控制,先作一個前期的工做,就是從服務器端加載資源,而後讀取到本地,再從本地讀取資源。這裏就以pc平臺爲例,移動平臺也是同樣,就是稍微作一點路徑上的修改,web

下面是不一樣平臺路徑的預編譯:windows

//不一樣平臺下StreamingAssets的路徑是不一樣的,這裏須要注意一下。 public static readonly string PathURL = #if UNITY_ANDROID   //安卓 	"jar:file://" + Application.dataPath + "!/assets/"; #elif UNITY_IPHONE  //iPhone 	Application.dataPath + "/Raw/"; #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  //windows平臺和web平臺 	"file://" + Application.dataPath + "/StreamingAssets/"; #else         string.Empty; #endif

關於資源的打包不理解的,我在以前的博文中有介紹: http://blog.csdn.net/dingxiaowei2013/article/details/17439887能夠去看一下這篇文章。

操做步驟:

建立腳本,命名Text.cs,而且將其拖放到MainCamera中數組

using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic; using System;   public class Text : MonoBehaviour {     //文本中每行的內容     ArrayList infoall;     //皮膚資源,這裏用於顯示中文     public GUISkin skin;     void Start ()     {         print("當前文件路徑:"+Application.persistentDataPath);         //刪除文件         DeleteFile(Application.persistentDataPath,"FileName.txt");           //建立文件,共寫入3次數據         CreateFile(Application.persistentDataPath,"FileName.txt","dingxiaowei");         CreateFile(Application.persistentDataPath,"FileName.txt","丁小未");         //CreateFile(Application.persistentDataPath ,"Filename.assetbundle","丁小未");         //下載模型         StartCoroutine(loadasset("http://192.168.1.180/3DShowResource/Products/AssetBundles/HX_DY02.assetbundle"));         //獲得文本中每一行的內容         infoall = LoadFile(Application.persistentDataPath,"FileName.txt");               }     //寫入模型到本地     IEnumerator loadasset(string url)     {         WWW w = new WWW(url);         yield return w;         if (w.isDone)         {             byte[] model = w.bytes;             int length = model.Length;             //寫入模型到本地             CreateModelFile(Application.persistentDataPath, "Model.assetbundle", model,length);         }     }      void CreateModelFile(string path, string name, byte[] info, int length)     {         //文件流信息         //StreamWriter sw;         Stream sw;         FileInfo t = new FileInfo(path + "//" + name);         if (!t.Exists)         {             //若是此文件不存在則建立             sw = t.Create();         }         else         {             //若是此文件存在則打開             //sw = t.Append();             return;         }         //以行的形式寫入信息         //sw.WriteLine(info);         sw.Write(info, 0, length);         //關閉流         sw.Close();         //銷燬流         sw.Dispose();     }       /**    * path:文件建立目錄    * name:文件的名稱    *  info:寫入的內容    */    void CreateFile(string path,string name,string info)    {       //文件流信息       StreamWriter sw;       FileInfo t = new FileInfo(path+"//"+ name);       if(!t.Exists)       {         //若是此文件不存在則建立         sw = t.CreateText();       }       else       {         //若是此文件存在則打開         sw = t.AppendText();       }       //以行的形式寫入信息       sw.WriteLine(info);       //關閉流       sw.Close();       //銷燬流       sw.Dispose();    }          /**    * 讀取文本文件    * path:讀取文件的路徑    * name:讀取文件的名稱    */    ArrayList LoadFile(string path,string name)    {         //使用流的形式讀取         StreamReader sr =null;         try{             sr = File.OpenText(path+"//"+ name);         }catch(Exception e)         {             //路徑與名稱未找到文件則直接返回空             return null;         }         string line;         ArrayList arrlist = new ArrayList();         while ((line = sr.ReadLine()) != null)         {             //一行一行的讀取             //將每一行的內容存入數組鏈表容器中             arrlist.Add(line);         }         //關閉流         sr.Close();         //銷燬流         sr.Dispose();         //將數組鏈表容器返回         return arrlist;    }        //讀取模型文件    IEnumerator LoadModelFromLocal(string path, string name)    {        string s = null; #if UNITY_ANDROID        s = "jar:file://"+path+"/"+name; #elif UNITY_IPHONE        s = path+"/"+name; #elif UNITY_STANDALONE_WIN || UNITY_EDITOR        s = "file://"+path+"/"+name; #endif        WWW w = new WWW(s);        yield return w;        if (w.isDone)        {            Instantiate(w.assetBundle.mainAsset);        }    }      /**    * path:刪除文件的路徑    * name:刪除文件的名稱    */      void DeleteFile(string path,string name)    {         File.Delete(path+"//"+ name);    }      void OnGUI()    {         //用新的皮膚資源,顯示中文         GUI.skin = skin;         //讀取文件中的全部內容         foreach(string str in infoall)         {             //繪製在屏幕當中             GUILayout.Label(str);         }         if (GUILayout.Button("加載模型"))         {             StartCoroutine(LoadModelFromLocal(Application.persistentDataPath, "Model.assetbundle"));         }    }   }



上面設計到文件流操做,還有就是Application.persistentDataPath,這裏並無用Application.DataPath,後者貌似在移動平臺是找不到的,前者就是所謂的沙盒文件,具備讀寫權限。

運行後的效果:


這裏演示了讀取操做文本和打包文件,若是切換到移動平臺可能中文字體沒法顯示,就須要作個字體便可,後面繼續版本控制後續工做。


補充:假設你的程序中已經預先將2進制文件作好,以下圖所示,你須要把二進制文件放在StreamingAssets這個文件夾中,必定要放在這裏。服務器

 

舉個例子,放在StreamingAssets中二進制文件打包後,Unity會將這些二進制文件放置在對應平臺下的路徑下。因此根據不一樣平臺,訪問的路徑是不同的。切記,你的二進制文件必定要放在StreamingAssets !!!!!!ide

01 #if UNITY_EDITOR
02         string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml";
03  
04 #elif UNITY_IPHONE
05       string filepath = Application.dataPath +"/Raw"+"/my.xml";
06  
07 #elif UNITY_ANDROID
08       string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
09  
10 #endif

 



==================== 迂者 丁小未 CSDN博客專欄=================學習

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243字體

Unity QQ羣:858550         cocos2dx QQ羣:280818155url

====================== 相互學習,共同進步 ===================spa

相關文章
相關標籤/搜索