money 发表于 2013-1-6 16:45

Unity3D Android IO操作代码(附源码)

using UnityEngine;
using System.Collections;
using System.IO;

public class FileIO : MonoBehaviour {
private string outputString = "";
private string pdpResult = "";
private string tcpResult = "";

// Use this for initialization
void OnGUI() {
    GUI.Label(new Rect(25, 25, 50, 25), "Output:");
    outputString = GUI.TextField(new Rect(75, 25, 200, 25), outputString);
    GUI.Label(new Rect(25, 55, 180, 25), "Result (persistentDataPath):");
    GUI.Label(new Rect(205, 55, 200, 25), pdpResult);
    GUI.Label(new Rect(25, 90, 180, 25), "Result (temporaryCachePath):");
    GUI.Label(new Rect(205, 90, 200, 25), tcpResult);
   
    if (GUI.Button(new Rect(25, 140, 50, 25), "Write")) {
      pdpResult = writeFile(Application.persistentDataPath, outputString);
      tcpResult = writeFile(Application.temporaryCachePath, outputString);
    }
   
    if (GUI.Button(new Rect(85, 140, 50, 25), "Read")) {
      pdpResult = readFile(Application.persistentDataPath);
      tcpResult = readFile(Application.temporaryCachePath);
    }
   
    if (GUI.Button(new Rect(145, 140, 50, 25), "Delete")) {
      pdpResult = deleteFile(Application.persistentDataPath);
      tcpResult = deleteFile(Application.temporaryCachePath);
    }
}

string writeFile(string dirPath, string output) {
    string result = "";
   
    if (dirPath != null && dirPath.Length > 0) {
      StreamWriter writer = File.CreateText(dirPath + "/foo.txt");
      writer.WriteLine(output);
      writer.Close();
      result = "done";
    } else {
      result = "path empty";
    }
   
    return result;
}

string readFile(string dirPath) {
    string result = "";
   
    if (dirPath != null && dirPath.Length > 0) {
      StreamReader reader = File.OpenText(dirPath + "/foo.txt");
      result = reader.ReadLine();
      reader.Close();
    } else {
      result = "path empty";
    }
   
    return result;
}

string deleteFile(string dirPath) {
    string result = "";
   
    if (dirPath != null && dirPath.Length > 0) {
      string fileName = dirPath + "/foo.txt";
      if (File.Exists(fileName)) {
      File.Delete(fileName);
      if (File.Exists(fileName))
          result = "failed";
      else result = "deleted";
      } else {
      result = "file does not exist. nothing to delete";
      }
    } else {
      result = "path empty";
    }
   
    return result;
}
}

hls 发表于 2013-2-24 11:31

看不懂说的都是些什么。

迷雾森林 发表于 2017-3-14 12:38

楼主是超人

嘴中的巧克力 发表于 2017-3-14 12:50

好帖就是要顶

sohot2012 发表于 2017-3-14 12:31

很好哦

seawingzhang 发表于 2017-3-14 12:30

不错不错

完整星空 发表于 2017-3-14 12:10

LZ真是人才

yixiess 发表于 2017-5-30 14:31

很不错

迅影视觉 发表于 2017-5-30 14:10

真心顶

天行我素 发表于 2017-5-30 14:11

难得一见的好帖
页: [1]
查看完整版本: Unity3D Android IO操作代码(附源码)