保存文件到本地and加载本地文件

来源:互联网 发布:女生low知乎 编辑:程序博客网 时间:2024/05/05 00:43

今天要做一个移动平台的版本控制,先做一个前期的工作,就是从服务器端加载资源,然后读取到本地,再从本地读取资源。这里就以pc平台为例,移动平台也是一样,就是稍微做一点路径上的修改,

下面是不同平台路径的预编译:

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。  
  2. public static readonly string PathURL =  
  3. #if UNITY_ANDROID   //安卓  
  4.     "jar:file://" + Application.dataPath + "!/assets/";  
  5. #elif UNITY_IPHONE  //iPhone  
  6.     Application.dataPath + "/Raw/";  
  7. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  //windows平台和web平台  
  8.     "file://" + Application.dataPath + "/StreamingAssets/";  
  9. #else  
  10.         string.Empty;  
  11. #endif  

关于资源的打包不理解的,我在之前的博文中有介绍:http://blog.csdn.net/dingxiaowei2013/article/details/17439887可以去看一下这篇文章。

操作步骤:

创建脚本,命名Text.cs,并且将其拖放到MainCamera中

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. using System.Collections.Generic;  
  5. using System;  
  6.    
  7. public class Text : MonoBehaviour {  
  8.     //文本中每行的内容  
  9.     ArrayList infoall;  
  10.     //皮肤资源,这里用于显示中文  
  11.     public GUISkin skin;  
  12.     void Start ()  
  13.     {  
  14.         print("当前文件路径:"+Application.persistentDataPath);  
  15.         //删除文件  
  16.         DeleteFile(Application.persistentDataPath,"FileName.txt");  
  17.    
  18.         //创建文件,共写入3次数据  
  19.         CreateFile(Application.persistentDataPath,"FileName.txt","dingxiaowei");  
  20.         CreateFile(Application.persistentDataPath,"FileName.txt","丁小未");  
  21.         //CreateFile(Application.persistentDataPath ,"Filename.assetbundle","丁小未");  
  22.         //下载模型  
  23.         StartCoroutine(loadasset("http://192.168.1.180/3DShowResource/Products/AssetBundles/HX_DY02.assetbundle"));  
  24.         //得到文本中每一行的内容  
  25.         infoall = LoadFile(Application.persistentDataPath,"FileName.txt");  
  26.   
  27.           
  28.     }  
  29.     //写入模型到本地  
  30.     IEnumerator loadasset(string url)  
  31.     {  
  32.         WWW w = new WWW(url);  
  33.         yield return w;  
  34.         if (w.isDone)  
  35.         {  
  36.             byte[] model = w.bytes;  
  37.             int length = model.Length;  
  38.             //写入模型到本地  
  39.             CreateModelFile(Application.persistentDataPath, "Model.assetbundle", model,length);  
  40.         }  
  41.     }  
  42.   
  43.     void CreateModelFile(string path, string name, byte[] info, int length)  
  44.     {  
  45.         //文件流信息  
  46.         //StreamWriter sw;  
  47.         Stream sw;  
  48.         FileInfo t = new FileInfo(path + "//" + name);  
  49.         if (!t.Exists)  
  50.         {  
  51.             //如果此文件不存在则创建  
  52.             sw = t.Create();  
  53.         }  
  54.         else  
  55.         {  
  56.             //如果此文件存在则打开  
  57.             //sw = t.Append();  
  58.             return;  
  59.         }  
  60.         //以行的形式写入信息  
  61.         //sw.WriteLine(info);  
  62.         sw.Write(info, 0, length);  
  63.         //关闭流  
  64.         sw.Close();  
  65.         //销毁流  
  66.         sw.Dispose();  
  67.     }   
  68.    
  69.    /** 
  70.    * path:文件创建目录 
  71.    * name:文件的名称 
  72.    *  info:写入的内容 
  73.    */  
  74.    void CreateFile(string path,string name,string info)  
  75.    {  
  76.       //文件流信息  
  77.       StreamWriter sw;  
  78.       FileInfo t = new FileInfo(path+"//"+ name);  
  79.       if(!t.Exists)  
  80.       {  
  81.         //如果此文件不存在则创建  
  82.         sw = t.CreateText();  
  83.       }  
  84.       else  
  85.       {  
  86.         //如果此文件存在则打开  
  87.         sw = t.AppendText();  
  88.       }  
  89.       //以行的形式写入信息  
  90.       sw.WriteLine(info);  
  91.       //关闭流  
  92.       sw.Close();  
  93.       //销毁流  
  94.       sw.Dispose();  
  95.    }  
  96.   
  97.      
  98.    
  99.   /** 
  100.    * 读取文本文件 
  101.    * path:读取文件的路径 
  102.    * name:读取文件的名称 
  103.    */  
  104.    ArrayList LoadFile(string path,string name)  
  105.    {  
  106.         //使用流的形式读取  
  107.         StreamReader sr =null;  
  108.         try{  
  109.             sr = File.OpenText(path+"//"+ name);  
  110.         }catch(Exception e)  
  111.         {  
  112.             //路径与名称未找到文件则直接返回空  
  113.             return null;  
  114.         }  
  115.         string line;  
  116.         ArrayList arrlist = new ArrayList();  
  117.         while ((line = sr.ReadLine()) != null)  
  118.         {  
  119.             //一行一行的读取  
  120.             //将每一行的内容存入数组链表容器中  
  121.             arrlist.Add(line);  
  122.         }  
  123.         //关闭流  
  124.         sr.Close();  
  125.         //销毁流  
  126.         sr.Dispose();  
  127.         //将数组链表容器返回  
  128.         return arrlist;  
  129.    }    
  130.   
  131.     //读取模型文件  
  132.    IEnumerator LoadModelFromLocal(string path, string name)  
  133.    {  
  134.        string s = null;  
  135. #if UNITY_ANDROID  
  136.        s = "jar:file://"+path+"/"+name;  
  137. #elif UNITY_IPHONE  
  138.        s = path+"/"+name;  
  139. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  
  140.        s = "file://"+path+"/"+name;  
  141. #endif  
  142.        WWW w = new WWW(s);  
  143.        yield return w;  
  144.        if (w.isDone)  
  145.        {  
  146.            Instantiate(w.assetBundle.mainAsset);  
  147.        }  
  148.    }  
  149.   
  150.    
  151.   /** 
  152.    * path:删除文件的路径 
  153.    * name:删除文件的名称 
  154.    */  
  155.    
  156.    void DeleteFile(string path,string name)  
  157.    {  
  158.         File.Delete(path+"//"+ name);  
  159.    }  
  160.    
  161.    void OnGUI()  
  162.    {  
  163.         //用新的皮肤资源,显示中文  
  164.         GUI.skin = skin;  
  165.         //读取文件中的所有内容  
  166.         foreach(string str in infoall)  
  167.         {  
  168.             //绘制在屏幕当中  
  169.             GUILayout.Label(str);  
  170.         }  
  171.         if (GUILayout.Button("加载模型"))  
  172.         {  
  173.             StartCoroutine(LoadModelFromLocal(Application.persistentDataPath, "Model.assetbundle"));  
  174.         }  
  175.    }  
  176.    
  177. }  



上面设计到文件流操作,还有就是Application.persistentDataPath,这里并没有用Application.DataPath,后者貌似在移动平台是找不到的,前者就是所谓的沙盒文件,具有读写权限。

0 0
原创粉丝点击