Unity 导出 Anroid 之文本文件读取问题

来源:互联网 发布:薛之谦林俊杰知乎 编辑:程序博客网 时间:2024/06/05 22:32

老样子,先给出用到的参考文章

参考1:Unity Assets目录下的特殊文件夹名称(作用和是否会被打包到build中)


http://blog.csdn.net/ycl295644/article/details/47683603

参考2:unity3d项目文件目录发布后,对应的ios/android应用目录

http://blog.csdn.net/w88193363/article/details/41288449


目的:游戏最初的一些配置信息,希望从文件中去读取,那么又希望不被修改,那么从“”参考1“”来看,我作出的选择是 StreamingAssets(自动被build包含,只读)


参考2中所列目录有一些小问题,我自己获取的路径如下


PC端输出文件:

Person : internet, 32Person : ttyo, 77[{"name":"internet","age":32},{"name":"ttyo","age":77}]data path :G:/PYM/Demos/PYM_Git/PYM_GIT/Assetsstream path :G:/PYM/Demos/PYM_Git/PYM_GIT/Assets/StreamingAssetscache path :C:/Users/ADMINI~1/AppData/Local/Temp/DefaultCompany/PYM_GITpst path :C:/Users/Administrator/AppData/LocalLow/DefaultCompany/PYM_GIT

ANDROID端输出文件:

Person : internet, 32Person : ttyo, 77[{"name":"internet","age":32},{"name":"ttyo","age":77}]data path :/data/app/com.DAV.PYM-1/base.apkstream path :jar:file:///data/app/com.DAV.PYM-1/base.apk!/assetscache path :/storage/emulated/0/Android/data/com.DAV.PYM/cachepst path :/storage/emulated/0/Android/data/com.DAV.PYM/files

说明:

data path      = dataPath

stream path  = StreamingAssetsPath

cache path   = temporaryCachePath

pst path        = PersistentDataPath

ANDROID端 StreamingAssetsPath 路径存在 jar:file:///

而PC端 StreamingAssetsPath 则不存在,


存在协议的部分需要使用 WWW 读取文件,PC端如果使用 WWW 读取文件,只需要增加 file:/// 协议即可

    string readFileTemp = Application.streamingAssetsPath + "/" + readFile;
#if UNITY_ANDROID
     //readFileTemp = readFileTemp;
#elif UNITY_STANDALONE
     readFileTemp = "file:///" + readFileTemp;
#endif

【上面这段C#插入的时候会异常,最后使用OTHER添加】

这里一定要注意!!!如果没有添加 file:/// 协议,会报 unsupported protocal


那么剩下的事情就是使用 WWW 读取,并获取文本即可

            WWW cfgFileWWW = new WWW(readFileTemp);            yield return cfgFileWWW;            if (null == cfgFileWWW || null != cfgFileWWW.error)            {                Debug.LogFormat("www download file error : {0}", cfgFileWWW.error);                yield break;            }             dataString = cfgFileWWW.text;  // 使用这种方式来获取文本文件内容
使用 WWW 时,注意需要该函数返回值写为 IEnumerator (迭代器,这里不展开)

最后,附完整代码

        class Person        {            public Person(string name, int age)            {                this.name = name;                this.age = age;            }            public string name = "default";            public int age = 0;            public override string ToString()            {                return "Person : " + name + ", " + age.ToString();            }        }       IEnumerator Start()        {            string output = "";   //  准备输出的字符串            string dataString = "";  //从文件中读取的内容            string readFileTemp = Application.streamingAssetsPath + "/" + readFile;            string writeFileTemp = Application.persistentDataPath + "/" + writeFile;              try            {                wfs = new FileStream(writeFileTemp, FileMode.Create);                wt = new StreamWriter(wfs);            }            catch            {                                   Debug.Log("write file open error.");          yield break;            }            if (null == wfs || null == wt)            {                                   Debug.Log("write file null.");                yield break;            }#if UNITY_ANDROID            //readFileTemp = readFileTemp;#elif UNITY_STANDALONE            readFileTemp = "file:///" + readFileTemp;#endif             WWW cfgFileWWW = new WWW(readFileTemp);            yield return cfgFileWWW;            if (null == cfgFileWWW || null != cfgFileWWW.error)            {                Debug.LogFormat("www download file error : {0}", cfgFileWWW.error);                yield break;            }            else            {                                   Debug.Log("www download file success.");            }             dataString = cfgFileWWW.text;  // 使用这种方式来获取文本文件内容             Person[] persons = JsonConvert.DeserializeObject<Person[]>(dataString);            foreach (var p in persons)            {                output += p.ToString() + '\n';            }            output += JsonConvert.SerializeObject(persons) + '\n';            wt.Write(output);            wt.WriteLine("data path :" + Application.dataPath);            wt.WriteLine("stream path :" + Application.streamingAssetsPath);            wt.WriteLine("cache path :" + Application.temporaryCachePath);            wt.WriteLine("pst path :" + Application.persistentDataPath);            wt.Flush();            if (null != rfs) rfs.Close();            if (null != rd) rd.Close();            if (null != wfs) wfs.Close();            if (null != wt) wt.Close();        }

这里也是测试下 JSON.NET 能不能在 ANDROID 下如愿工作,答案是可以的(大神勿喷,我才入门)



原创粉丝点击