Version: 2022.3

AssetDatabase.GetImplicitAssetBundleName

切换到手册
public static string GetImplicitAssetBundleName (string assetPath);

参数

assetPath 资源的路径。

返回

string 返回给定资源所属的 AssetBundle 的名称。有关更多详细信息,请参阅此方法描述。

描述

返回给定资源所属的 AssetBundle 的名称。

如果资源已显式分配给 AssetBundle,则返回该值。 如果资源不属于 AssetBundle,则会遍历其父文件夹,直到找到属于 AssetBundle 的资源。如果找到与此条件匹配的文件夹,则返回其 AssetBundle 名称。如果未找到,则返回空字符串。

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
public class AssetDatabaseExamples : MonoBehaviour
{
    [MenuItem("AssetDatabase/Check Bundle Names")]
    static void CheckBundleNames()
    {
        var assetsWithIncorrectBundle = new List<string>();
        var correctBundleName = "metaltexturebundle";
        //Check if any of the assets have an incorrect bundle set to them
        for (var i = 0; i < 10; i++)
        {
            var texturePath = $"Assets/Textures/Metal/Metal{i}.png";
            if(AssetDatabase.GetImplicitAssetBundleName(texturePath) != correctBundleName)
                assetsWithIncorrectBundle.Add(texturePath);
        }

//If no such assets exist then return if (!assetsWithIncorrectBundle.Any()) return; //If there are such assets then print them out var incorrectAssetNames = ""; foreach (var asset in assetsWithIncorrectBundle) { incorrectAssetNames += $"\"{asset}\" "; } Debug.LogWarning($"Assets with an incorrect Asset Bundle {incorrectAssetNames}"); } }