找回密码
 立即注册
楼主: money

[脚本] Unity3D Android IO操作代码(附源码)

  [复制链接]
发表于 2013-1-6 16:45 | 显示全部楼层 |阅读模式
  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. }
复制代码

本帖子中包含更多资源

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

×
发表于 2013-2-24 11:31 | 显示全部楼层
看不懂说的都是些什么。
发表于 2017-3-14 12:38 | 显示全部楼层
楼主是超人
发表于 2017-3-14 12:50 | 显示全部楼层
好帖就是要顶
发表于 2017-3-14 12:31 | 显示全部楼层
很好哦
发表于 2017-3-14 12:30 | 显示全部楼层
不错不错
发表于 2017-3-14 12:10 | 显示全部楼层
LZ真是人才
发表于 2017-5-30 14:31 | 显示全部楼层
很不错
发表于 2017-5-30 14:10 | 显示全部楼层
真心顶
发表于 2017-5-30 14:11 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-5 16:57 , Processed in 0.168607 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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