Unity中的特殊文件夹
文件夹
Editor
Editor文件夹可以在Assets
目录下,也可以在子目录里,只要名子叫Editor
就可以。例如目录Assets/Editor和Assets/Scripts/Editor
是一样的,无论多少个叫Editor
的文件夹都可以
Editor
文件夹下存放的所有资源文件或脚本文件都不会被打进包中,并且脚本也只能在编辑时使用。一般会把一些编辑器扩展的脚本放在这里,或者是一些编辑时用的DLL
Editor Default Resources
必须放在Assets目录下,可以把编辑器扩展用到的一些资源放在这里,比如图片、文本文件、等等。它和Editor文件夹一样都不会被打到包中,仅仅用于开发时使用 通过 EditorGUIUtility.Load 可以读取到该文件夹下的资源
using UnityEngine;
using UnityEditor;
public class LoadExample
{
[MenuItem("Examples/Load Editor Texture Example")]
static void loadExample()
{
Texture tex = (Texture)EditorGUIUtility.Load("aboutwindow.mainheader");
Debug.Log("Got: " + tex.name + " !");
Renderer r = GameObject.Find("Cube").GetComponent<Renderer>();
r.sharedMaterial.mainTexture = tex;
}
}
Gizmos
必须放在Assets
目录下,可以将绘制在场景中的Gizmo
素材放在这个文件夹下,使用Gizmos.DrawIcon
绘制的时候可以直接传入图片路径,打包时会打到包中
Plugins
可以将sdk、dll库文件、插件等放在这里,打完包后就会自动将这些文件打到包中
Resources
❗❗❗ 项目开发时不要使用它 ❗❗❗
它只是用来快速构建demo
Resources文件夹可以在Assets
目录下,也可以在子目录里,只要名子叫Resources
就可以,Resources
文件夹下的所有资源不管你用还是不用都会被打进包中
可以使用Resource.Load
读取此文件夹下的资源
因为Resource
文件夹下的所有资源都会被打进包中,所以一般开发时使用AssetDatabase.LoadAssetAtPath
,它可以读取Assets
目录下的任意文件夹下的资源,只能在编辑器下使用,它的路径是Assets/xx/xx.xxx
,需要注意路径带文件的后缀名
StreamingAssets
这个文件夹下的资源也都会被打进包中,它和Resources
的区别是Resources
会压缩文件,但是StreamingAssets
不会压缩而是原封不动的打包进去。并且它是一个只读的文件夹,就是程序运行时只能读不能写。它在各个平台下的路径是不同的
使用Application.streamingAssetsPath
会根据当前的平台选择对应的路径
其他文件夹: 如果是没有用到的资源,就不会被打到包中,如果是用到了的资源就会被打到包中
路径
Application.dataPath
- 这里是列表文本
Assets
文件夹路径, - 只在编辑器模式下有效,打包后该路径不存在
Application.streamingAssetsPath
StreamingAssets文件夹路径,不同平台读取方式不同,详细见图:
平台 | File读取 | UnityWebRequest读取 |
---|---|---|
PC | ✔️ 无前缀 | ✔️ |
安卓 | ❌ | ✔️ 加前缀:jar:file:// |
IOS | ✔️ 无前缀 | ✔️ 加前缀:file:// |
Mac | ✔️ 无前缀 | ✔️ 加前缀:file:// |
UnityWebRequest读取代码示例:
/// <summary>
/// 使用UnityWebRequest请求时的地址(根据各个平台处理)
/// 注:这个路径不能使用File来使用
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetRequestURL(string fileName)
{
string path = Path.Combine(Application.streamingAssetsPath, fileName);
#if UNITY_ANDROID && !UNITY_EDITOR
path ="jar:file://" + ptah;
#elif UNITY_EIDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE //苹果下是这样
path ="file://"+ ptah;
// #else // UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_IPHONE 这些的路径一样
// request = new UnityWebRequest(Application.streamingAssetsPath + "/" + fileName);
#endif
return path;
}
Application.persistentDataPath
这个文件夹在不同平台下的路径不同
平台 | 路径 | 读写方式 |
---|---|---|
PC | 与普通文件路径相同 | File直接读写 |
安卓 | /data/data/xxx.xxx.xxx/files | File直接读写(安卓端唯一可以用File读写的路径) 安卓手机连接电脑,可通过 Android/data/com.xxx.xxx 查看 |
IOS | Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents | File直接读写 |
读写案例
public UnityEngine.UI.Text infoText;
public void FileRead()
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Application.persistentDataPath);
infoText.text = "";
foreach (FileInfo file in di.GetFiles())
{
//Debug.Log(file.Name);
infoText.text += file.Name.ToString();
StreamReader sr = file.OpenText();
infoText.text += ":" + sr.ReadToEnd()+"\n";
sr.Close();
}
}
public void FileWrite()
{
string fileName = "Test.txt";
string path = Path.Combine(Application.persistentDataPath, fileName);
if (!File.Exists(path))
{
File.WriteAllText(path, "Persistent path test!");
infoText.text = "添加了 Test.txt 文件 ";
}
else
{
infoText.text = "已经存在 Test.txt,已删除 ";
File.Delete(path);
}
}
itextShap插创建字体案例
itextShap 插件是用于创建PDF文档的,但是需要使用我们自己的字体。
- 创建字体函数的方法
public static BaseFont CreateFont(string name, string encoding, bool embedded);//底层应该是通过 System.IO.File 来访问创建的
- 在PC或者IOS上,我们只需要将字体放到
StreamingAssets
文件夹可正常使用接口创建 - 在安卓端,是不能通过 System.Io.File 访问 StreamingAssets 的,唯一可以访问的是 PersistentData 文件夹,所以我们需要在程序首次启动的时候,将
StreamingAssets
中的字体文件 拷贝到PersistentData
中
const string SIMHEI = "SIMHEI.TTF";
/// <summary>
/// 安卓下调用
/// 将自己使用的字体从 StreamingAssets中复制到 persisentData 中
/// StreamingAssets 中的 在 安卓端 被压缩了 不能直接通过 IO读取
/// </summary>
private static void CopyFontToPersistentData()
{
string path = Path.Combine(Application.persistentDataPath, SIMHEI);
//如果持久化目录下没有配置文件,先从streamingAssets里复制一份到持久化目录
if (!File.Exists(path))
{
//先通过 UnityWebRequest 将 streamingAssetsPath 中的 字体读取出来
UnityWebRequest request = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, SIMHEI));
request.SendWebRequest();
while (request.isDone == false) { }
//写入 persistentDataPath
File.WriteAllBytes(path, request.downloadHandler.data);
}
}
注意事项
该路径常用来存储档案的注意事项
- 卸载软件后该路径会被 删除 ,再次安装数据被重置了(安卓端测试)
- 不卸载软件,直接重新安装,原数据还在(安卓端测试)
- 在PC端,有些时候是免安装版本,该路径是在程序所在的文件夹外,所以不会被删除
- 总结以上情况:在版本升级中,若对存储的数据结构进行改变了,那应先删除原来的数据,否则程序要出错
Application.temporaryCachePath
基本未使用过,未深入研究。
平台 | 路径 | 读写方式 |
---|---|---|
PC | - | - |
安卓 | /data/data/xxx.xxx.xxx/cache | - |
IOS | Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches | - |