unity3d笔记之通过代码批量设置AssetBundle名称

在实际项目中可能会有成千上百个资源需要打包,如果一个一个去设置名称,设置后缀就有点太费事了。

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

public class BuilderAssetsBunlds
{
    //打包、、、、、、、、、、、、、、、、、、、、、、
    [MenuItem("Tools/打包")]
    public static void BundlerAssets()
    {
        // Debug.LogError("打包Assets");
        BuildPipeline.BuildAssetBundles(GetOutAssetsDirecotion(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }

    public static string GetOutAssetsDirecotion()
    {
        string assetBundleDirectory = Application.streamingAssetsPath;
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        return assetBundleDirectory;
    }

    //  设置资源名称/

    [MenuItem("Tools/设置Assetbundle名字")]
    public static void SetAssetBundellabls()
    {
        CheckFileSystemInfo();
    }

    public static void CheckFileSystemInfo()  //检查目标目录下的文件系统
    {
        AssetDatabase.RemoveUnusedAssetBundleNames(); //移除没有用的assetbundlename
        Object obj = Selection.activeObject;    // Selection.activeObject 返回选择的物体
        string path = AssetDatabase.GetAssetPath(obj);//选中的文件夹
        CoutineCheck(path);
    }

    public static void CheckFileOrDirectory(FileSystemInfo fileSystemInfo, string path) //判断是文件还是文件夹
    {
        FileInfo fileInfo = fileSystemInfo as FileInfo;
        if (fileInfo != null)
        {
            SetBundleName(path);
        }
        else
        {
            CoutineCheck(path);
        }
    }

    public static void CoutineCheck(string path)   //是文件夹,继续向下
    {
        DirectoryInfo directory = new DirectoryInfo(@path);
        FileSystemInfo[] fileSystemInfos = directory.GetFileSystemInfos();

        foreach (var item in fileSystemInfos)
        {
            // Debug.Log(item);
            int idx = item.ToString().LastIndexOf(@"\");//得到最后一个'\'的索引
            string name = item.ToString().Substring(idx + 1);//截取后面的作为名称

            if (!name.Contains(".meta"))
            {
                CheckFileOrDirectory(item, path + "/" + name);  //item  文件系统,加相对路径
            }
        }
    }

    public static void SetBundleName(string path)  //设置assetbundle名字
    {
        var importer = AssetImporter.GetAtPath(path);
        string[] strs = path.Split('.');
        string[] dictors = strs[0].Split('/');
        string name = "";
        for (int i = 1; i < dictors.Length; i++)
        {
            if (i < dictors.Length - 1)
            {
                name += dictors[i] + "/";
            }
            else
            {
                name += dictors[i];
            }
        }
        if (importer != null)
        {
            importer.assetBundleVariant = "bytes";
            importer.assetBundleName = name;
        }
        else
            Debug.Log("importer是空的");
    }

}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值