obsidian/笔记文件/2.笔记/Unity重新导入资源.md
2025-03-26 00:02:56 +08:00

1.8 KiB
Raw Permalink Blame History

#unity/日常积累

unity进游戏的时候有时候可能会遇到这些报错但逻辑上并没有报错

!Pasted image 20230302111745.png

这时候很可能就是Library库出现了问题 一般定位到是哪个资源出现问题重新导入一遍资源就好用reimport

reimport的代码简单使用例如只重新导入所有模型

    [MenuItem("Tools/Reimport All Models")]
    public static void ReimportAllModel()
    {
        string[] assetPaths = AssetDatabase.GetAllAssetPaths();
        foreach (string assetPath in assetPaths)
        {
            string normalizedAssetPath = assetPath.ToLower();
            if (!normalizedAssetPath.EndsWith(".fbx")
                && !normalizedAssetPath.EndsWith(".obj")
                && !normalizedAssetPath.EndsWith(".3ds")
                && !normalizedAssetPath.EndsWith(".asset"))
            {
                continue;
            }

            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate);
        }
    }

例如重新导入所有Asset资源

        /// <summary>
        /// 完全重新导入全部 Assets
        /// </summary>
        /// <param name="buildSettings"></param>
        private static void StepReimportAllAssets(BuildSettings buildSettings)
        {
            if (GLog.IsLogInfoEnabled) GLog.LogInfo("[BuildToolV2.StepReimportAllAssets] Start");

            if (buildSettings.ReimportAllAssets)
            {
                AssetDatabase.ImportAsset("Assets", ImportAssetOptions.ForceUpdate | ImportAssetOptions.ImportRecursive | ImportAssetOptions.DontDownloadFromCacheServer);
            }

            if (GLog.IsLogInfoEnabled) GLog.LogInfo("[BuildToolV2.StepReimportAllAssets] OK");
        }