unity实现按文件夹自动打包assetbundle

直接上源码,功能:选中文件下所有带_ab的文件夹分为一个ab包,自动命名打包

public class EDxAssetBundle : MonoBehaviour
{
    //[MenuItem("Tool/Create AssetBundle All")]
    //private static void CreateAssetBundle()
    //{
    //    Debug.Log("Create AssetBundle...");
    //    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/assetbundle", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    //    AssetDatabase.Refresh();
    //}

    [MenuItem("Tool/Create AssetBunldes")]
    [System.Obsolete]
    private static void CreateAssetBunldesMain()
    {
        //获取在Project视图中选择的所有游戏对象  
        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        //遍历所有的游戏对象  
        foreach (Object obj in SelectedAsset)
        {

            string sourcePath = AssetDatabase.GetAssetPath(obj);
            string filename = GetFileName(sourcePath);
            string parentname = GetParentName(sourcePath);

            //Debug.Log(obj.name + " : " + sourcePath);

            if (!string.IsNullOrEmpty(filename))
            {
                Debug.Log("打包 " + obj.name + " : " + sourcePath);

                AssetImporter ai = AssetImporter.GetAtPath(sourcePath);
                if (ai.assetBundleName != parentname + ".assetbundle")
                {
                    ai.assetBundleName = parentname + ".assetbundle";
                }
            }
        }

        string path = Application.streamingAssetsPath + "/assetbundle";
        if (!Directory.Exists(path)) Directory.CreateDirectory(path);
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/assetbundle", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        
        //刷新编辑器  
        AssetDatabase.Refresh();
    }
    private static string GetParentName(string path)
    {
        string[] strs = path.Split('/');
        //string parent = strs[strs.Length - 2];
        for (int i = 0; i < strs.Length; i++)
        {
            if (strs[i].Contains("_ab")) return strs[i];
        }
        return "";
    }

    private static string GetFileName(string path)
    {
        string[] strs = path.Split('/');
        string filename = strs[strs.Length - 1];

        if (filename.Contains("."))
        {
            return filename;
        }
        else
        {
            return "";

        }
    }
}

工程地址:https://download.csdn.net/download/u014261855/13099912

猜你喜欢

转载自blog.csdn.net/u014261855/article/details/109583480
今日推荐