unity3d读取android文本文件,职场小白求助Unity项目Android端读取CSV文本问题
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
本人最近在項目中遇到一些問題,就是讀寫CSV格式的文本文檔,在pc端讀寫是沒問題的,但是安卓端就讀不到了,下附代碼,希望能得到大佬們的幫助
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class CheckDataManager : MonoBehaviour {
string _loadPath;
string _savePath;
[SerializeField]
private string _fileName = "UserDataTable";
private const string EXTENSION = ".csv";
private CSVTable _table;
// Bind Component
//不同平臺的路徑也不相同
void Awake()
{
#if UNITY_EDITOR
Debug.Log("這里是編輯器模式");
_loadPath = Application.streamingAssetsPath + "/";
_savePath = Application.persistentDataPath + "/";
#elif UNITY_IOS
Debug.Log("這里是蘋果端");
_loadPath = "file://"+Application.dataPath + "/Raw/";
_savePath = Application.persistentDataPath + "/Raw/";
#elif UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("這里是安卓端");
//_loadPath = "jar:file://" + Application.dataPath + "/!/assets/";
//_savePath = "jar:file://" + Application.persistentDataPath + "/";
//_loadPath = "jar:file://" + Application.dataPath + "!/assets/";
//_savePath = "jar:file://" + Application.persistentDataPath + "/";
_loadPath = "jar:file://" + Application.dataPath + "/!/assets/";
_savePath = Application.persistentDataPath + "/";
#endif
Load();
}
///
/// 加載文件
///
void Load()
{
if (!Directory.Exists(_loadPath))
{
Debug.LogError("The file not be found in this path. path:" + _loadPath);
return;
//yield return "";
}
string fullFileName = _loadPath + _fileName + EXTENSION;
//StreamReader sr;
sr = File.OpenText(fullFileName);
//sr = new StreamReader(fullFileName, Encoding.UTF8);
//string content = sr.ReadToEnd();
//Debug.Log("AAAAAAAAAAAAAAA" + content.Length);
//sr.Close();
//sr.Dispose();
WWW www;
www = new WWW(fullFileName);
while (!http://www.isDone) { }
string content = http://www.text;
_table = CSVTable.CreateTable(_fileName, content);
//添加測試
Test();
}
///
/// 存儲文件
///
private void Save()
{
if (_table == null)
{
Debug.LogError("The table is null.");
return;
}
string tableContent = _table.GetContent();
if (!Directory.Exists(_savePath))
{
Debug.Log("未找到路徑, 已自動創(chuàng)建");
Directory.CreateDirectory(_savePath);
}
string fullFileName = _savePath + _fileName + EXTENSION;
//string fullFileName = _loadPath + _fileName + EXTENSION;
StreamWriter sw;
//sw = File.CreateText(fullFileName);
sw = new StreamWriter(fullFileName, false, Encoding.UTF8);
sw.Write(tableContent);
sw.Close();
sw.Dispose();
/******************************/
if (!Directory.Exists(_savePath))
{
Debug.LogError("The file not be found in this path. path:" + _loadPath);
return;
}
StreamReader sr;
//sr = File.OpenText(fullFileName);
sr = new StreamReader(fullFileName, Encoding.UTF8);
string content = sr.ReadToEnd();
Debug.Log("BBBBBBBBBBBBB" + content.Length);
sr.Close();
sr.Dispose();
/************************************/
_table = null;
}
///
/// 測試方法
///
private void Test()
{
Debug.Log(_table.ToString());
// 修改某一數(shù)據(jù)
//_table["1"]["Age"] = "10000";
if (!System.IO.File.Exists(_savePath + _fileName + EXTENSION))
{
print("不存在該文件");
Save();
}
else
{
print("已存在該文件");
}
//Save();
}
}
總結
以上是生活随笔為你收集整理的unity3d读取android文本文件,职场小白求助Unity项目Android端读取CSV文本问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 手机关机代码非root,
- 下一篇: android面试之fragment,当