Unity中C# 文件本地读取,本地保存等实例

魔幻蓝天

收藏于 : 2019-01-20 09:49   被转藏 : 1   

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

 阅读文章全部内容  
点击查看
文章点评
相关文章
魔幻蓝天 关注

文章收藏:5034

TA的最新收藏