找回密码
 立即注册
Unity开发者联盟 门户 查看主题

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

发布者: money | 发布时间: 2013-1-6 16:45| 查看数: 5017| 评论数: 1|帖子模式

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;

  4. public class FileIO : MonoBehaviour {
  5.   private string outputString = "";
  6.   private string pdpResult = "";
  7.   private string tcpResult = "";
  8.   
  9.   // Use this for initialization
  10.   void OnGUI() {
  11.     GUI.Label(new Rect(25, 25, 50, 25), "Output:");
  12.     outputString = GUI.TextField(new Rect(75, 25, 200, 25), outputString);
  13.     GUI.Label(new Rect(25, 55, 180, 25), "Result (persistentDataPath):");
  14.     GUI.Label(new Rect(205, 55, 200, 25), pdpResult);
  15.     GUI.Label(new Rect(25, 90, 180, 25), "Result (temporaryCachePath):");
  16.     GUI.Label(new Rect(205, 90, 200, 25), tcpResult);
  17.    
  18.     if (GUI.Button(new Rect(25, 140, 50, 25), "Write")) {
  19.       pdpResult = writeFile(Application.persistentDataPath, outputString);
  20.       tcpResult = writeFile(Application.temporaryCachePath, outputString);
  21.     }
  22.    
  23.     if (GUI.Button(new Rect(85, 140, 50, 25), "Read")) {
  24.       pdpResult = readFile(Application.persistentDataPath);
  25.       tcpResult = readFile(Application.temporaryCachePath);
  26.     }
  27.    
  28.     if (GUI.Button(new Rect(145, 140, 50, 25), "Delete")) {
  29.       pdpResult = deleteFile(Application.persistentDataPath);
  30.       tcpResult = deleteFile(Application.temporaryCachePath);
  31.     }
  32.   }
  33.   
  34.   string writeFile(string dirPath, string output) {
  35.     string result = "";
  36.    
  37.     if (dirPath != null && dirPath.Length > 0) {
  38.       StreamWriter writer = File.CreateText(dirPath + "/foo.txt");
  39.       writer.WriteLine(output);
  40.       writer.Close();
  41.       result = "done";
  42.     } else {
  43.       result = "path empty";
  44.     }
  45.    
  46.     return result;
  47.   }
  48.   
  49.   string readFile(string dirPath) {
  50.     string result = "";
  51.    
  52.     if (dirPath != null && dirPath.Length > 0) {
  53.       StreamReader reader = File.OpenText(dirPath + "/foo.txt");
  54.       result = reader.ReadLine();
  55.       reader.Close();
  56.     } else {
  57.       result = "path empty";
  58.     }
  59.    
  60.     return result;
  61.   }
  62.   
  63.   string deleteFile(string dirPath) {
  64.     string result = "";
  65.    
  66.     if (dirPath != null && dirPath.Length > 0) {
  67.       string fileName = dirPath + "/foo.txt";
  68.       if (File.Exists(fileName)) {
  69.         File.Delete(fileName);
  70.         if (File.Exists(fileName))
  71.           result = "failed";
  72.         else result = "deleted";
  73.       } else {
  74.         result = "file does not exist. nothing to delete";
  75.       }
  76.     } else {
  77.       result = "path empty";
  78.     }
  79.    
  80.     return result;
  81.   }
  82. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

最新评论

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
难得一见的好帖

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 11:02 , Processed in 0.105040 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表