Unity3D IOS下保存和读取资源(保存到文件夹目录)
更新:HHH   时间:2023-1-7


Unity3D IOS下保存和读取资源(保存到文件夹目录)


using UnityEngine;
   
using System.Collections;
   
using System.IO;
   
using System;
   
   
public class NvTestSave : MonoBehaviour
   
{
   
   
   
private string showtext = "not txt has been loaded!";
   
   
   
public string JsonPath
   
    {
   
        get{
   
            string    path=null;
   
            if(Application.platform==RuntimePlatform.IPhonePlayer)
   
            {
   
                path= Application.dataPath.Substring (0, Application.dataPath.Length - 5);
   
                path = path.Substring(0, path.LastIndexOf('/'))+"/Documents/";   
   
            }
   
            else
   
            {
   
                path=Application.dataPath+"/Resource/GameData/";
   
            }
   
            return path;
   
        }    
   
    }
   
   
   
// Use this for initialization
   
void Start ()
   
{
   
SaveJson(" i love coding!!","MyText.txt");
   
StartCoroutine(WaitForRead());
   
}
   
   
   
// Update is called once per frame
   
void Update ()
   
{
   
   
   
}
   
   
   
void OnGUI()
   
{
   
GUI.Label(new Rect(10, 10, 500, 20), showtext);
   
}
   
   
   
void ShowText(string text)
   
{
   
showtext = text;
   
}
   
   
   
IEnumerator WaitForRead( )
   
    {
   
yield return new WaitForSeconds(0.5f);
   
StartCoroutine(InstanceText("MyText.txt"));
   
}
   
 //Unity3D教程手册:www.unitymanual.com
   
IEnumerator InstanceText(string fileName)
   
    {
   
        string path="file://"+JsonPath+fileName;
   
   
   
Debug.LogError("======path:  "+path);
   
        WWW wwwText=new WWW(path);
   
        yield return wwwText;
   
Debug.LogError("======ShowText");
   
        ShowText(wwwText.text);
   
    }
   
 //Unity3D教程手册:www.unitymanual.com
   
void  SaveJson(string txt , string filepathandname)
   
{
   
string file = JsonPath+"//" + filepathandname;
   
   
   
StreamWriter sw;
   
FileInfo t = new FileInfo(file);
   
if(!t.Exists)
   
{
   
sw = t.CreateText();
   
}
   
else
   
{
   
sw = t.AppendText();
   
}
   
sw.WriteLine(txt);
   
sw.Close();
   
sw.Dispose();
   
}
   
}
   

   


返回游戏开发教程...