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

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

参考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中所列目录有一些小问题,我自己获取的路径如下


扫描二维码关注公众号,回复: 2842562 查看本文章

PC端输出文件:

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

ANDROID端输出文件:

Person : internet, 32
Person : ttyo, 77
[{"name":"internet","age":32},{"name":"ttyo","age":77}]
data path :/data/app/com.DAV.PYM-1/base.apk
stream path :jar:file:///data/app/com.DAV.PYM-1/base.apk!/assets
cache path :/storage/emulated/0/Android/data/com.DAV.PYM/cache
pst 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 下如愿工作,答案是可以的(大神勿喷,我才入门)



猜你喜欢

转载自blog.csdn.net/davied9/article/details/78022476