unity的一些特殊目录

Hidden Folders

Folders that start with a dot (e.g. ".UnitTests/", ".svn/") are ignored by Unity. Any assets in there are not imported, and any scripts in there are not compiled. They will not show up in the Project view.


"Standard Assets"

Scripts in here are always compiled first. Scripts are output to either Assembly-CSharp-firstpass, Assembly-UnityScript-firstpass, or Assembly-Boo-firstpass, depending on the language. Seehttp://docs.unity3d.com/Documentation/Manual/ScriptCompileOrderFolders.html

Scripts inside the Standard Assets folder will be compiled earlier than your other scripts. So, placing scripts in Standard Assets is one way for C# scripts to be able to access .js scripts or vice-versa.


"Pro Standard Assets"

Same with Standard Assets, only files here are meant for the Pro version. This means assets here make use of Pro-only features like render textures and screen-space effects.

Again, scripts here are compiled earlier, allowing them to be accessed by other scripts (of any language) that are outside the Pro Standard Assets folder.


"Editor"

The Editor folder name is a special name which allows your scripts access to the Unity Editor Scripting API. If your script uses any classes or functionality from the UnityEditor namespace, it has to be placed in a folder called Editor.

Scripts inside an Editor folder will not be included in your game's build. They are only used in the Unity Editor.

You can have multiple Editor folders throughout your project.

Note: An Editor folder not located in another special folder can be placed/nested anywhere in the project. However, if it's in "Standard Assets", "Pro Standard Assets", or "Plugins", it must be a direct child of these folders. Otherwise, it will not get processed. For example, it's ok to have a path like "My Extension/Scripts/Editor", but if placed in a special folder, it must always be "Standard Assets/Editor/My Extension/Scripts", or "Pro Standard Assets/Editor/My Extension/Scripts", or "Plugins/Editor/My Extension/Scripts".


"Plugins"

The "Plugins" folder is where you must put any native plugins, which you want to be accessible by your scripts. They will also be automatically included in your build. Take note that this folder may not be in any subfolder (it has to reside within the top-level Assets folder).

In Windows, native plugins exist as .dll files, in Mac OS X, they are .bundle files, and in Linux, they are .so files.

Like the Standard Assets folder, any scripts in here are compiled earlier, allowing them to be accessed by other scripts (of any language) that are outside the Plugins folder.


"Plugins/x86"

If you are building for 32-bit or a universal (both 32 and 64 bit) platform, and if this subfolder exists, any native plugin files in this folder will automatically be included in your build. If this folder does not exist, Unity will look for native plugins inside the parent Plugins folder instead.


"Plugins/x86_64"

If you are building for 64-bit or a universal (both 32 and 64 bit) platform, and if this subfolder exists, any native plugin files in this folder will automatically be included in your build. If this folder does not exist, Unity will look for native plugins inside the parent Plugins folder instead.


If you are making a universal build, it's recommended you make both the x86 and x86_64 subfolders. Then have the 32-bit and 64-bit versions of your native plugins in the proper subfolder correspondingly.


"Plugins/Android"

Place here any Java .jar files you want included in your Android project, used for Java-based plugins. Any .so file (when having Android NDK-based plugins) will also be included. Seehttp://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html


"Plugins/iOS"

A limited, simple way to automatically add (as symbolic links) any .a, .m, .mm, .c, or .cpp files into the generated Xcode project. See http://docs.unity3d.com/Documentation/Manual/PluginsForIOS.html

If you need more control how to automatically add files to the Xcode project, you should make use of the PostprocessBuildPlayer feature. Doing so does not require you to place such files in the Plugins/iOS folder. Seehttp://docs.unity3d.com/Documentation/Manual/BuildPlayerPipeline.html


"Resources"

The Resources folder is a special folder which allows you to access assets by file path and name in your scripts, rather than by the usual (and recommended) method of direct references (as variables in scripts, wherein you use drag-and-drop in the Unity Editor).

For this reason, caution is advised when using it. All assets you put in the Resources folder are always included in your build (even if it turned out that they are unused), because, since you are using them via scripts, Unity then has no way of determining which Resources-based assets are used or not.

You can have multiple Resources folders throughout your project, so it is not recommended to have an asset in one Resources folder and have another asset with that same name in another Resources folder.

Once your game is built, all assets in all Resources folders get packed into the game's archive for assets. This means the Resources folder technically doesn't exist anymore in your final build, even though your code will still access them via the paths that existed while it was in your project.

Also see http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html

Take note when assets are accessed as variables of MonoBehaviour scripts, those assets get loaded into memory once that MonoBehaviour script is instantiated (i.e. its game object or prefab is now in the scene). This may be undesirable, if the asset is too large and you want more control of when it gets loaded into memory.

Consider putting such large assets in a Resources folder, and load them via Resources.Load. When not used anymore, you can free the memory it took up by calling Object.Destroy on the object, followed by Resources.UnloadUnusedAssets.


"Editor Default Resources"

This folder functions like a Resources folder, but is meant for editor scripts only. Use this if your editor plugin needs to load assets (e.g. icons, GUI skins, etc.) while making sure said assets won't get included in the user's build (putting such files in a normal Resources folder would have meant that those assets would be included in the user's game when built).

As editor scripts aren't MonoBehaviour scripts, you can't do the usual way of accessing assets (i.e. dragging-and-dropping via the Inspector). The "Editor Default Resources" is for a convenient way around this.

To access assets inside the "Editor Default Resources", you need to use EditorGUIUtility.Load.

Take note that unlike Resources.Load, EditorGUIUtility.Load requires you to specify the filename extension of the asset you're trying to load. So it has to be "myPlugin/mySkin.guiskin" instead of "myPlugin/mySkin".

To free memory used by EditorGUIUtility.Load, call Object.Destroy on the object, then call EditorUtility.UnloadUnusedAssets.

Afaik, only one "Editor Default Resources" folder can be present, and it has to be directly under the top Assets folder.

"Gizmos"

The gizmos folder holds all the texture/icon assets for use with Gizmos.DrawIcon. Texture assets placed inside this folder can be called by name, and drawn on-screen as a gizmo in the editor.


"WebPlayerTemplates"

Used to replace the default web page used for web builds. Any scripts placed here will not be compiled at all. This folder has to be in your top-level Assets folder (it should not be in any subfolder in your Assets directory).


"StreamingAssets"

Any files in here are copied to the build folder as is, without any changes (except for mobile and web builds, where they get embedded into the final build file). The path where they are can vary per platform but is accessible via Application.streamingAssetsPath (http://docs.unity3d.com/Documentation/ScriptReference/Application-streamingAssetsPath.html) Also see http://docs.unity3d.com/Documentation/Manual/StreamingAssets.html

<think>我们正在讨论Unity引擎中的特殊文件目录。根据引用内容,我们可以总结出以下几个关键目录及其用途: 1. **Resources**: 用于存放需要在运行时通过Unity资源加载机制访问的资源。这些资源会被打包到应用程序中,但在发布后不可写,只能通过Unity的API(如`Resources.Load`)读取。发布移动端后该路径不存在[^1][^3]。 2. **Editor Default Resources**: 存放编辑器模式下使用的自定义资源(如编辑器图标、布局文件等)。这些资源可以通过`EditorGUIUtility.Load`在编辑器脚本中加载[^1]。 3. **StreamingAssets**: 该目录下的资源在发布时会原封不动地打包到应用程序中。在运行时,可以通过`WWW`或`UnityWebRequest`来读取(在Android平台上路径需要特殊处理)。该目录可读不可写。 4. **Application.persistentDataPath**: 这是一个运行时路径,用于存放持久化数据。该目录可读可写,适合保存用户数据、配置文件或下载的资源。在不同平台上的具体路径不同。 此外,引用[3]还提到了在访问这些路径时可能遇到的异常情况,特别是Android平台上的路径访问问题(需要正确使用"jar:file://"协议)。 根据Unity官方文档,还有一些其他特殊目录: 5. **Plugins**: 用于存放本地插件(如DLL、SO、AAR等)。根据平台不同,可以放在Plugins/{平台名}子目录下。 6. **Gizmos**: 存放编辑器场景中使用的Gizmo图标,可以通过`Gizmos.DrawIcon`使用。 7. **Standard Assets**: 导入标准资源包时使用的目录Unity会为这些资源提供特殊的编译顺序。 8. **Editor**: 该目录下的脚本只在编辑器模式下运行,用于扩展编辑器功能。 9. **Hidden Assets**: 以.开头的资源会被Unity忽略,不会导入。 10. **WebGLTemplates**: 用于自定义WebGL构建的HTML模板。 11. **TextMesh Pro**: 如果使用TextMesh Pro,会有专门的资源目录。 为了全面性,我们列出Unity所有特殊文件夹(包括上述提到的): ### Unity特殊文件夹完整列表及用途 | 文件夹名称 | 用途描述 | 是否可写 | 打包行为 | |-----------------------------|--------------------------------------------------------------------------|----------|------------------------------| | **Assets** | 主资源目录,所有需要被Unity管理的资源都放在此目录下 | 是 | 根据规则打包 | | **Editor** | 存放编辑器扩展脚本,这些脚本只在Unity编辑器中运行 | 是 | 不包含在构建中 | | **Editor Default Resources**| 存放编辑器默认资源(如图标、样式等),通过`EditorGUIUtility.Load`访问 | 是 | 不包含在构建中 | | **Gizmos** | 存放场景视图使用的Gizmo图标 | 是 | 包含在构建中(仅编辑器使用) | | **Plugins** | 存放本地插件(DLL、SO、jar等) | 是 | 根据平台打包 | | **Resources** | 运行时通过`Resources.Load`动态加载的资源 | 否 | 打包到应用程序 | | **Standard Assets** | 标准资源包目录Unity会优先编译此目录脚本 | 是 | 打包到应用程序 | | **StreamingAssets** | 原始资源目录,打包时原样保留,通过`Application.streamingAssetsPath`访问 | 否 | 原样打包 | | **WebGLTemplates** | 存放WebGL平台的自定义HTML模板 | 是 | 打包时使用 | | **Hidden Assets** (如`.git`) | 以点开头的文件夹会被Unity忽略 | - | 不打包 | | **TextMesh Pro** | 存放TextMesh Pro资源(如果使用) | 是 | 打包到应用程序 | ### 运行时路径说明(非项目目录,而是应用安装后的路径) 1. **Application.dataPath**: - 只读路径,指向应用程序的数据文件夹(包含游戏资源) - 在编辑器下:项目Assets文件夹的绝对路径 - 在Android:`/data/app/<package.name>/base.apk`(实际是APK内部,不可写) 2. **Application.streamingAssetsPath**: - 只读路径,指向StreamingAssets文件夹的运行时位置 - Android:`jar:file://<Application.dataPath>!/assets/`(需要特殊处理) 3. **Application.persistentDataPath**: - 可读写路径,用于保存持久化数据(如用户配置、下载内容) - Android: `/storage/emulated/0/Android/data/<package.name>/files` - iOS: `Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents` 4. **Application.temporaryCachePath**: - 临时缓存目录(可读写),适合存储临时文件 ### 使用示例代码 ```csharp // 加载Resources目录下的资源 Texture2D resTex = Resources.Load<Texture2D>("Textures/Icon"); // 加载StreamingAssets目录下的资源(Android需特殊处理) IEnumerator LoadStreamingAsset(string path) { string fullPath = Path.Combine(Application.streamingAssetsPath, path); if (Application.platform == RuntimePlatform.Android) { fullPath = "jar:file://" + Application.dataPath + "!/assets/" + path; } using (WWW www = new WWW(fullPath)) { yield return www; // 使用www.bytes或www.text } } // 在持久化目录创建文件 string savePath = Path.Combine(Application.persistentDataPath, "save.dat"); File.WriteAllText(savePath, "data"); ``` ### 注意事项 - 在移动平台上访问文件路径时,注意使用`Application`类提供的路径属性,避免硬编码[^3]。 - Android平台访问APK内部资源(如StreamingAssets)需要使用`jar:file://`协议,且路径区分大小写。 - 在编辑器模式下,`Application.persistentDataPath`指向项目外的本地缓存目录(如`AppData/LocalLow`)。 以上是Unity中主要的特殊目录及其用途。如果你需要更详细的解释或特定平台的信息,请参考Unity官方文档[^2]。 §§相关问题§§ 1. 如何在Unity中正确读取Android平台的StreamingAssets资源? 2. Application.persistentDataPath在不同平台的具体路径是什么? 3. Unity中如何动态加载Resources目录外的资源? </think> ### Unity 特殊文件目录完整指南 以下是 Unity 引擎中所有特殊文件目录的详细说明,根据功能和使用场景分类整理: --- #### 🗂️ **核心资源目录** 1. **Assets 根目录** - Unity 项目的核心资源库 - 所有需要被 Unity 引擎管理的资源必须放在此目录下 - 支持子目录自由组织(如 `Assets/Textures`, `Assets/Scripts`) 2. **Resources** - **路径**: `Assets/Resources/` - **用途**: 运行时动态加载资源 - **特点**: - 打包时**自动压缩**并入应用 - 发布后路径**不可见不可写** - 只能通过 `Resources.Load()` 加载 [^1] - **示例**: ```csharp Texture2D tex = Resources.Load<Texture2D>("Icons/player"); ``` 3. **StreamingAssets** - **路径**: `Assets/StreamingAssets/` - **用途**: 存放原始未压缩资源 - **特点**: - 打包时**原样复制**到应用包 - 发布后**只读**,不可修改 - 需用 `WWW` 或 `UnityWebRequest` 加载 [^3] - **平台路径差异**: ```csharp // Android 特殊路径处理 string path = Application.streamingAssetsPath + "/config.json"; // 或 "jar:file://" + Application.dataPath + "!/assets/config.json" ``` --- #### 🛠️ **编辑器专用目录** 4. **Editor** - **路径**: `Assets/Editor/` - **用途**: 存放编辑器扩展脚本 - **特点**: - 脚本**仅在编辑器**中执行 - 不包含在最终构建中 - 用于创建自定义 Inspector、窗口工具等 5. **Editor Default Resources** - **路径**: `Assets/Editor Default Resources/` - **用途**: 存储编辑器 GUI 资源(图标/样式) - **访问方式**: ```csharp Texture2D icon = EditorGUIUtility.Load("Icons/custom_icon.png") as Texture2D; ``` - 通过 `EditorGUIUtility.Load()` 加载 6. **Gizmos** - **路径**: `Assets/Gizmos/` - **用途**: 存放场景视图的辅助图标 - **调用方式**: ```csharp Gizmos.DrawIcon(transform.position, "waypoint.png"); ``` --- #### 📱 **平台相关目录** 7. **Plugins** - **路径**: `Assets/Plugins/` - **用途**: 存放平台原生库 - **子目录规范**: - `x86/` - Windows 32位库 - `x86_64/` - Windows 64位库 - `Android/` - .so 或 .aar 文件 - `iOS/` - Objective-C 代码 8. **Standard Assets** - **路径**: `Assets/Standard Assets/` - **用途**: Unity 官方标准资源包 - **特点**: - 脚本**优先编译** - 包含预设的材质、模型、脚本等 --- #### 💾 **运行时路径** 9. **Application.persistentDataPath** - **动态路径**(非项目内固定目录) - **用途**: 读写用户数据 - **平台示例**: - Windows: `C:\Users\<user>\AppData\LocalLow\<company>\<product>` - Android: `/storage/emulated/0/Android/data/<package>/files` - iOS: `Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents` - **重要**: 唯一支持**读写**的运行时路径 [^3] 10. **Application.temporaryCachePath** - 临时缓存目录(可读写) - 适合存储短期临时文件 --- #### ⚠️ **特殊规则目录** 11. **Hidden Assets** - 命名规则: 以 `.` 开头的文件夹(如 `.git`) - 特点: Unity **完全忽略**这些目录 12. **WebGLTemplates** - **路径**: `Assets/WebGLTemplates/` - **用途**: 自定义 WebGL 输出的 HTML 页面 --- #### 🔧 使用注意事项 1. **路径访问异常处理**(Android 典型问题): ```csharp // 正确访问 StreamingAssets IEnumerator LoadConfig() { #if UNITY_ANDROID string path = "jar:file://" + Application.dataPath + "!/assets/config.json"; #else string path = Application.streamingAssetsPath + "/config.json"; #endif using (UnityWebRequest req = UnityWebRequest.Get(path)) { yield return req.SendWebRequest(); Debug.Log(req.downloadHandler.text); } } ``` 2. **目录创建规范**: ```csharp // 在 persistentDataPath 创建用户目录 void CreateUserFolder() { string userPath = Path.Combine(Application.persistentDataPath, "Saves"); if (!Directory.Exists(userPath)) { Directory.CreateDirectory(userPath); } } ``` --- > **关键总结**: > - **只读资源** → `Resources` 或 `StreamingAssets` > - **编辑器资源** → `Editor` 或 `Editor Default Resources` > - **运行时写入** → `Application.persistentDataPath` > - **原生插件** → `Plugins` 平台子目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值