Unity3D IOS 下保存和读取资源(保存到文件夹目录) Posted on 2025 年 07 月 08 日 by U3d / Unity3D 脚本/插件 /被围观 33 次 001002using UnityEngine;003004using System.Collections;005006using System.IO;007008using System;009010011public class NvTestSave : MonoBehaviour012013{01401501601private string showtext = "not txt has been loaded!";7018019020021public string JsonPath022023 {024025 get{026027 string path=null;028029 if(Application.platform==RuntimePlatform.IPhonePlayer)030031 {032033 path= Application.dataPath.Substring (0, Application.dataPath.Length - 5);034035 path = path.Substring(0, path.LastIndexOf('/'))+"/Documents/";036037 }038039 else040041 {042043 path=Application.dataPath+"/Resource/GameData/";044045 }046047 return path;048049 }050051 }052053054055// Use this for initialization056057void Start ()058059{060061SaveJson(" i love coding!!","MyText.txt");062063StartCoroutine(WaitForRead());064065}066067068069// Update is called once per frame070071void Update ()072073{074075076077}078079080081void OnGUI()082083{084085GUI.Label(new Rect(10, 10, 500, 20), showtext);086087}088089090091void ShowText(string text)092093{094095showtext = text;096097}098099100101IEnumerator WaitForRead( )102103 {104105yield return new WaitForSeconds(0.5f);106107StartCoroutine(InstanceText("MyText.txt"));108109}110111 //Unity3D 教程手册:www.unitymanual.com112113IEnumerator InstanceText(string fileName)114115 {116117 string path="file://"+JsonPath+fileName;118119120121Debug.LogError("======path: "+path);122123 WWW wwwText=new WWW(path);124125 yield return wwwText;126127Debug.LogError("======ShowText");128129 ShowText(wwwText.text);130131 }132133 //Unity3D 教程手册:www.unitymanual.com134135void SaveJson(string txt , string filepathandname)136137{138139string file = JsonPath+"//" + filepathandname;140141142143StreamWriter sw;144145FileInfo t = new FileInfo(file);146147if(!t.Exists)148149{150151sw = t.CreateText();152153}154155else156157{158159sw = t.AppendText();160161}162163sw.WriteLine(txt);164165sw.Close();166167sw.Dispose();168169}170171}172173