1.Resource:在Unity编辑器的Project窗口里创建,Resources文件夹下的资源全部会打包进.apk或者.ipa,并且打包时会将里面的资源压缩处理。加载方法是Resources.Load(文件名),需要注意:文件名不包括扩展名,打包后不能更改Resources下的资源内容。但是从Resources文件夹中加载出来的资源可以更改。

2.Application.dataPath:这个属性返回的是程序的数据文件所在文件夹的路径,例如在Editor中就是项目的Assets文件夹的路径,通过这个路径可以访问项目中任何文件夹中的资源,但是在移动端它是完全没用。

3.Application.streamingAssetsPath:这个属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。在Unity工程的Assets目录下起一个名为“StreamingAssets”的文件夹即可,然后用Application.streamingAssetsPath访问,这个文件夹中的资源在打包时会原封不动的打包进去,不会压缩,一般放置一些资源数据。在PC/MAC中可实现对文件的“增删改查”等操作,但在移动端是一个只读路径。

4.Application.persistentDataPath(推荐):此属性返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。这个路径可读、可写,但是只能在程序运行时才能读写操作,不能提前将数据放入这个路径。在IOS上是应用程序的沙盒,可以被iCloud自动备份,可以通过同步推送一类的助手直接取出文件;在Android上的位置是根据Project Setting里设置的Write Access路径,可以设置是程序沙盒还是sdcard,注意:如果在Android设置保存在沙盒中,那么就必须root以后才能用电脑取出文件,因此建议写入sdcard里。一般情况下,建议将获得的文件保存在这个路径下,例如可以从StreamingAsset中读取的二进制文件或者从AssetBundle读取的文件写入PersistentDatapath,比如游戏初始化时正在加载资源,之后数据的更新和读取都会从PersistentDatapath实现数据读写。

5.Application.temporaryCachePath 此属性返回一个临时数据的缓存目录,跟Application.persistentDataPath类似,但是在IOS上不能被自动备份。

6./sdcard/:表示Android手机的SD卡根目录。

7./storage/emulated/0/:表示Android手机的内置存储根目录。

  以上各路径中的资源加载方式都可以用WWW类加载,但要注意各个平台路径需要加的访问名称,例如Android平台的路径前要加"jar:file://",其他平台使用"file://"。以下是各路径在各平台中的具体位置信息:

Android平台
Application.dataPath :  /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath :  jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath :  /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath :  /data/data/xxx.xxx.xxx/cache
IOS平台
Application.dataPath :                    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath :    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
Windows Web Player

Application.dataPath :  file:///D:/MyGame/WebPlayer (即导包后保存的文件夹,html文件所在文件夹)

Application.streamingAssetsPath : 

Application.persistentDataPath : 

Application.temporaryCachePath :

附上一个文件操作类FileHelper.cs

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
/// <summary>
/// 使用Application.persistentDataPath方式来创建文件,读写Xml文件.
/// 注Application.persistentDataPath末尾没有“/”符号
/// </summary>
public class FileHelper: MonoBehaviour
{
/// <summary>
/// 动态创建文件夹.
/// </summary>
/// <returns>The folder.</returns>
/// <param name="path">文件创建目录.</param>
/// <param name="FolderName">文件夹名(不带符号).</param>
public string CreateFolder(string path,string FolderName)
{
string FolderPath = path+FolderName;
if(!Directory.Exists(FolderPath))
{ 
Directory.CreateDirectory(FolderPath);
} 
return FolderPath;
}
 
/// <summary>
/// 创建文件.
/// </summary>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">文件的名称.</param>
/// <param name="info">写入的内容.</param>
public void CreateFile(string path,string name,string info)
{
//文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path+name);
if(!t.Exists)
{
//如果此文件不存在则创建
sw = t.CreateText();
}
else
{
//如果此文件存在则打开
sw = t.AppendText();
}
//以行的形式写入信息
sw.WriteLine(info);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
} 
 
/// <summary>
/// 读取文件.
/// </summary>
/// <returns>The file.</returns>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">读取文件的名称.</param>
public ArrayList LoadFile(string path,string name)
{
//使用流的形式读取
StreamReader sr =null;
try{
sr = File.OpenText(path+name);
}catch(Exception e)
{
//路径与名称未找到文件则直接返回空
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的读取
//将每一行的内容存入数组链表容器中
arrlist.Add(line);
}
//关闭流
sr.Close();
//销毁流
sr.Dispose();
//将数组链表容器返回
return arrlist;
} 
//写入模型到本地
IEnumerator loadassetbundle(string url)
{
WWW w = new WWW(url);
yield return w;
if (w.isDone)
{
byte[] model = w.bytes;
int length = model.Length;
//写入模型到本地
CreateassetbundleFile(Application.persistentDataPath, "Model.assetbundle", model,length);
}
}
/// <summary>
/// 获取文件下所有文件大小
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public int GetAllFileSize(string filePath)
{
int sum = 0;
if (!Directory.Exists(filePath))
{
return 0;
}
 
DirectoryInfo dti = new DirectoryInfo(filePath);
 
FileInfo[] fi = dti.GetFiles();
 
foreach (FileInfo f in fi)
{
 
sum += Convert.ToInt32(f.Length / 1024);
}
 
DirectoryInfo[] di = dti.GetDirectories();
 
if (di.Length > 0)
{
for (int i = 0; i < di.Length; i++)
{
sum += GetAllFileSize(di[i].FullName);
}
}
return sum;
}
/// <summary>
/// 获取指定文件大小
/// </summary>
/// <param name="FilePath"></param>
/// <param name="FileName"></param>
/// <returns></returns>
public int GetFileSize(string FilePath, string FileName)
{
int sum = 0;
if (!Directory.Exists(FilePath))
{
return 0;
}
else
{
FileInfo Files = new FileInfo(@FilePath + FileName);
sum += Convert.ToInt32(Files.Length / 1024);
}
return sum;
}
void CreateassetbundleFile(string path, string name, byte[] info, int length)
{
//文件流信息
//StreamWriter sw;
Stream sw;
FileInfo t = new FileInfo(path + "//" + name);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.Create();
}
else
{
//如果此文件存在则打开
//sw = t.Append();
return;
}
//以行的形式写入信息
sw.Write(info, 0, length);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
//读取本地AssetBundle文件
IEnumerator LoadAssetbundleFromLocal(string path, string name)
{
print("file:///" + path + "/" + name);
 
WWW w = new WWW("file:///"+path + "/" + name);
 
yield return w;
 
if (w.isDone)
 
{
Instantiate(w.assetBundle.mainAsset);
}
}
 
/// <summary>
/// 删除文件.
/// </summary>
/// <param name="path">删除完整文件夹路径.</param>
/// <param name="name">删除文件的名称.</param>
public void DeleteFile(string path, string name)
{
File.Delete(path + name);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path"></param>
/// <param name="filesName"></param>
/// <returns></returns>
public bool DeleteFiles(string path, string filesName)
{
bool isDelete = false;
try
{
if (Directory.Exists(path))
{
if (File.Exists(path + "\\" + filesName))
{
File.Delete(path + "\\" + filesName);
isDelete = true;
}
}
}
catch
{
return isDelete;
}
return isDelete;
}
}



2.随机操作类
RandomHelper.cs

using UnityEngine;
using System.Collections;
/// <summary>
/// 随机操作类
/// </summary>
public class RandomHelper 
{
private static char[] constant = { \'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'i\', \'j\', \'k\', \'l\', \'m\', \'n\', \'o\', \'p\', \'q\', \'r\', \'s\', \'t\', \'u\', \'v\', \'w\', \'x\', \'y\', \'z\', \'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'G\', \'H\', \'I\', \'J\', \'K\', \'L\', \'M\', \'N\', \'O\', \'P\', \'Q\', \'R\', \'S\', \'T\', \'U\', \'V\', \'W\', \'X\', \'Y\', \'Z\' };
/// <summary>
/// 字符串随机
/// </summary>
/// <param name="Length">要随机的位数</param>
/// <returns></returns>
public string GenerateRandomNumber(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[Random.Range(0,62)]);
}
return newRandom.ToString();
}
private static char[] constant1 = { \'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\' };
/// <summary>
/// 数字随机
/// </summary>
/// <param name="Length">要随机的位数</param>
/// <returns></returns>
public string GenerateNumber(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(10);
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant1[Random.Range(0, 10)]);
}
return newRandom.ToString();
}
/// <summary>
/// 字符串数组随机
/// </summary>
/// <param name="chars">数组</param>
/// <param name="Length">随机的位数</param>
/// <returns></returns>
public string GetStrRandomSurname(string[] chars, int Length)
{
int count = chars.Length;
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(count);
 
for (int i = 0; i < Length; i++)
{
newRandom.Append(chars[Random.Range(0, count)]);
}
return newRandom.ToString();
}
/// <summary>
/// 字符串*截取
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public string[] getStringToList(string str)
{
return str.Split(\'*\');
}
}