[Unity] 资源工作流程 - 辅助工具

一、前言#

备忘用,内容较杂

二、项目资源检查#

(一)资源 Importer 属性检查#

  1. AssetImporter.GetAtPath
    https://docs.unity.cn/cn/2021.2/ScriptReference/AssetImporter.GetAtPath.html
  2. TextureImporter
    https://docs.unity.cn/cn/2021.2/ScriptReference/TextureImporter.html
// 项目资源 Importer 属性检查
[MenuItem("资源检查/查询开启 Read/Write 的图片")]
static void TextureCheck()
{
    string[] paths = AssetDatabase.GetAllAssetPaths();
    for (int i = 0; i < paths.Length; i++)
    {
        AssetImporter importer = AssetImporter.GetAtPath(paths[i]);
        TextureImporter textureImporter = importer as TextureImporter;
        if (textureImporter && textureImporter.isReadable)
        {
            Debug.Log(paths[i]);
        }
    }
}

(二)获取资源在 Library 目录下的位置#

参考自:https://gist.github.com/Unity-Javier/50408443ddda2f64d029eb3a5e71cfe4

Unity 使用 .meta 文件来保存资源的唯一识别码和各种配置属性。

Library 目录,是已导入资源的本地缓存位置,项目 Assets 中的资源主要缓存在 Library\Artifacts 中,按 Hash 值划分目录。

使用版本控制系统时,应该忽略 Library 和 UserSettings 目录。但要确保资源导入器在导入时会产生一致的结果。

可以使用‘binary2text’工具(位于 Unity 编辑器安装位置下的 Data/Tools 文件夹中)检查 Library 文件夹的内容以查看导入器生成的结果。

主要是通过 AssetDatabaseExperimental.LookupArtifact 根据资源的 GUID,找到缓存的 Hash 值和对应路径

[MenuItem("资源检查/查询对象 Library 缓存路径")]
public static void GetSelectionLibraryPath()
{
    UnityEngine.Object obj = Selection.activeObject;
    if (obj == null) return;
    StringBuilder assetPathInfo = new StringBuilder();

    string assetPath = AssetDatabase.GetAssetPath(obj);
    GUID guid = AssetDatabase.GUIDFromAssetPath(assetPath);
    ArtifactID hash = AssetDatabaseExperimental.LookupArtifact(new ArtifactKey(guid));

    AssetDatabaseExperimental.GetArtifactPaths(hash, out var paths);

    assetPathInfo.Append(assetPath + "\n");
    assetPathInfo.Append("GUID: " + guid + "\n");

    foreach (var curVirtualPath in paths)
    {
        //.info files are the asset previews which you can see as part
        //of the asset icon
        if (curVirtualPath.EndsWith(".info"))
            continue;

        //The virtual path redirects somewhere, so we get the 
        //actual path on disk (or on the in memory database, accordingly)
        var curPath = Path.GetFullPath(curVirtualPath);
        assetPathInfo.Append(curPath + "\n");
    }
    Debug.Log("Path info for select asset:\n" + assetPathInfo.ToString());
}

作者:wildmelon

出处:https://www.cnblogs.com/wildmelon/p/16086059.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)」许可协议进行许可。

作品中所包含的外部插件源代码、图片、资源和链接库等,如希望在其他场景使用,请务必确认符合其本身授权规范。

posted @   野生西瓜  阅读(562)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示