在游戏运行期间,只有Resources 和 StreamingAssets 目录具有读取权限,其中Resources用来读取游戏资源,而StreamingAssets可使用File类来读取文件(除个别平台),但是都是只读的,并不能写。
只有Application.persistentDataPath 目录是可读 可写的。
using System;
using System.IO;
using System.Text;
using UnityEngine;
public class ReadWriteTextWhenRunningMyTools : MonoBehaviour
{
//--可读不可写
private string m_ResourcesTxt = string.Empty;
//--可读不可写
private string m_StreamingAssetsTxt = string.Empty;
//--可读可写
private string m_PersistentDataTxt = string.Empty;
private void Start()
{
m_ResourcesTxt = Resources.Load<TextAsset>("MyText/test").text;
//Debug.Log(m_ResourcesTxt);
m_StreamingAssetsTxt = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "MyText/test.txt"), Encoding.Default);
//Debug.Log(m_StreamingAssetsTxt);
}
private void OnGUI()
{
GUILayout.Label(string.Format("<size=50>{0}</size>", m_Resource