unity3d本地文件读写



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

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

01. //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
02. public static readonly string PathURL =
03. #if UNITY_ANDROID   //安卓
04.     "jar:file://" + Application.dataPath + "!/assets/";
05. #elif UNITY_IPHONE  //iPhone
06.     Application.dataPath + "/Raw/";
07. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  //windows平台和web平台
08.     "file://" + Application.dataPath + "/StreamingAssets/";
09. #else
10.         string.Empty;
11. #endif

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

操作步骤:

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

001. using UnityEngine;
002. using System.Collections;
003. using System.IO;
004. using System.Collections.Generic;
005. using System;
006.   
007. public class Text : MonoBehaviour {
008.     //文本中每行的内容
009.     ArrayList infoall;
010.     //皮肤资源,这里用于显示中文
011.     public GUISkin skin;
012.     void Start ()
013.     {
014.         print("当前文件路径:"+Application.persistentDataPath);
015.         //删除文件
016.         DeleteFile(Application.persistentDataPath,"FileName.txt");
017.   
018.         //创建文件,共写入3次数据
019.         CreateFile(Application.persistentDataPath,"FileName.txt","dingxiaowei");
020.         CreateFile(Application.persistentDataPath,"FileName.txt","丁小未");
021.         //CreateFile(Application.persistentDataPath ,"Filename.assetbundle","丁小未");
022.         //下载模型
023.         StartCoroutine(loadasset("http://192.168.1.180/3DShowResource/Products/AssetBundles/HX_DY02.assetbundle"));
024.         //得到文本中每一行的内容
025.         infoall = LoadFile(Application.persistentDataPath,"FileName.txt");
026.  
027.          
028.     }
029.     //写入模型到本地
030.     IEnumerator loadasset(string url)
031.     {
032.         WWW w = new WWW(url);
033.         yield return w;
034.         if (w.isDone)
035.         {
036.             byte[] model = w.bytes;
037.             int length = model.Length;
038.             //写入模型到本地
039.             CreateModelFile(Application.persistentDataPath, "Model.assetbundle", model,length);
040.         }
041.     }
042.  
043.     void CreateModelFile(string path, string name, byte[] info, int length)
044.     {
045.         //文件流信息
046.         //StreamWriter sw;
047.         Stream sw;
048.         FileInfo t = new FileInfo(path + "//" + name);
049.         if (!t.Exists)
050.         {
051.             //如果此文件不存在则创建
052.             sw = t.Create();
053.         }
054.         else
055.         {
056.             //如果此文件存在则打开
057.             //sw = t.Append();
058.             return;
059.         }
060.         //以行的形式写入信息
061.         //sw.WriteLine(info);
062.         sw.Write(info, 0, length);
063.         //关闭流
064.         sw.Close();
065.         //销毁流
066.         sw.Dispose();
067.     }
068.   
069.    /**
070.    * path:文件创建目录
071.    * name:文件的名称
072.    *  info:写入的内容
073.    */
074.    void CreateFile(string path,string name,string info)
075.    {
076.       //文件流信息
077.       StreamWriter sw;
078.       FileInfo t = new FileInfo(path+"//"+ name);
079.       if(!t.Exists)
080.       {
081.         //如果此文件不存在则创建
082.         sw = t.CreateText();
083.       }
084.       else
085.       {
086.         //如果此文件存在则打开
087.         sw = t.AppendText();
088.       }
089.       //以行的形式写入信息
090.       sw.WriteLine(info);
091.       //关闭流
092.       sw.Close();
093.       //销毁流
094.       sw.Dispose();
095.    }
096.  
097.     
098.   
099.   /**
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.        print("file:///" + path + "/" + name);
135.        WWW w = new WWW("file:///"+path + "/" + name);
136.        yield return w;
137.        if (w.isDone)
138.        {
139.            Instantiate(w.assetBundle.mainAsset);
140.        }
141.    }
142.  
143.   
144.   /**
145.    * path:删除文件的路径
146.    * name:删除文件的名称
147.    */
148.   
149.    void DeleteFile(string path,string name)
150.    {
151.         File.Delete(path+"//"+ name);
152.    }
153.   
154.    void OnGUI()
155.    {
156.         //用新的皮肤资源,显示中文
157.         GUI.skin = skin;
158.         //读取文件中的所有内容
159.         foreach(string str in infoall)
160.         {
161.             //绘制在屏幕当中
162.             GUILayout.Label(str);
163.         }
164.         if (GUILayout.Button("加载模型"))
165.         {
166.             StartCoroutine(LoadModelFromLocal(Application.persistentDataPath, "Model.assetbundle"));
167.         }
168.    }
169.   
170. }

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

猜你喜欢

转载自blog.csdn.net/feizxiang3/article/details/43671233