Unity AssetsBundle一键为需要打包的预制体设置assetBundle名

直接看代码:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetBundleNameGenerate
{

    [MenuItem("AssetBundles/generic ABName of assets", false, 100)]
    public static void GenericAssetBundleNames()
    {
        GenericAssetBundleNamesInPath(Application.dataPath + "/prefabs", ".prefab", "prefab", SearchOption.AllDirectories, "", null);
    }

    /// <summary>
    /// 对目录内指定后缀名的文件设置assetBundleName
    /// </summary>
    /// <param name="fullPath">文件路径</param>
    /// <param name="fileExt">文件后缀</param>
    /// <param name="typeName">类型名</param>
    /// <param name="searchOption">搜索文件的类别</param>
    /// <param name="suffixName">assetBundle后缀名</param>
    /// <param name="filterStr">名字过滤(包含)</param>
    private static void GenericAssetBundleNamesInPath(string fullPath, string fileExt, string typeName, SearchOption searchOption, string suffixName, string filterStr)
    {
        if (Directory.Exists(fullPath))
        {
            EditorUtility.DisplayProgressBar("设置AssetBundle名称", "正在设置AssetName名称中...", 0f);
            var dir = new DirectoryInfo(fullPath);
            var files = dir.GetFiles("*" + fileExt, searchOption);

            for (int i = 0; i < files.Length; i++)
            {
                var fileInfo = files[i];
                EditorUtility.DisplayProgressBar("设置AssetBundle名称", "正在设置AssetName名称中...", 1f * i / files.Length);

                string assetBundleName = string.Empty;
                if (string.IsNullOrEmpty(typeName) == false)
                {
                    if (string.IsNullOrEmpty(filterStr) || fileInfo.Name.IndexOf(filterStr) >= 0)
                    {
                        string name = fileInfo.Name.Substring(0, fileInfo.Name.Length - fileExt.Length);
                        name = name.ToLower();
                        if (name.Contains(" "))
                        {
                            Debug.LogErrorFormat("资源名字存在空格符path = {0}", fileInfo.FullName);
                        }
                        assetBundleName = string.Format("{0}/{1}{2}.unity3d", typeName, name, suffixName);
                    }
                }

                var path = fileInfo.FullName.Substring(Application.dataPath.Length - "Assets".Length).Replace('\\', '/');
                var importer = AssetImporter.GetAtPath(path);
                if (importer != null && importer.assetBundleName != assetBundleName)
                {
                    importer.assetBundleName = assetBundleName;
                }
                if (importer.assetBundleName != null && importer.assetBundleName != "")
                {
                    importer.assetBundleVariant = null;
                }
            }
            EditorUtility.ClearProgressBar();
        }
        else
        {
            Debug.LogErrorFormat("文件夹路径名出错:{0}", fullPath);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值