AssetBundle.cs

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

public class AssetBundle
{
    static string OutputPath { get { return "Assets/Bundle"; } }
    static string FileSuffixes { get { return "assetbundle"; } }

    [MenuItem("AssetBundle/Select Folder To Bytes")]
    static void SelectFolderToBytes()
    {
        var output = Application.dataPath + "/Bytes";
        var defaultPath = Application.streamingAssetsPath;
        var openPath = EditorUtility.OpenFolderPanel("Open Folder", defaultPath, null).Replace('\\', '/');
        if (string.IsNullOrEmpty(openPath)) return;
        var name = openPath.Substring(openPath.LastIndexOf('/') + 1);
        foreach (var filePath in GetFolderList(new List<string>(), openPath))
        {
            var file = new FileInfo(filePath);
            var rPath = file.DirectoryName.Substring(openPath.Length).Replace('\\', '/').TrimStart('/');
            var trgPath = output + "/" + name + "/" + rPath;
            if (!Directory.Exists(trgPath)) Directory.CreateDirectory(trgPath);
            var fileName = file.Name.Substring(0, file.Name.LastIndexOf('.'));
            var destFileName = trgPath + "/" + fileName + ".bytes";
            file.CopyTo(destFileName, true);
            Debug.Log("字节化:" + destFileName);
        }
        Debug.Log("[" + name + "]完成转换,输出目录:" + openPath);
    }

    [MenuItem("AssetBundle/Select Folder To Bundle For Android")]
    static void SelectFolderToBundleForAndroid()
    {
        var buildTarget = BuildTarget.Android;
        var defaultPath = Application.dataPath;
        var openPath = EditorUtility.OpenFolderPanel("Open Folder", defaultPath, null).Replace('\\', '/');
        if (string.IsNullOrEmpty(openPath)) return;
        var rootPath = defaultPath.Substring(0, defaultPath.IndexOf("Assets", StringComparison.Ordinal) - 1).Replace('\\', '/');
        var name = openPath.Substring(openPath.LastIndexOf('/') + 1);

        var builds = new List<AssetBundleBuild>();
        var abb = new AssetBundleBuild
        {
            assetBundleName = name + "." + FileSuffixes,
            assetNames = GetFolderList(new List<string>(), openPath, rootPath).ToArray()
        };
        builds.Add(abb);

        if (!Directory.Exists(OutputPath)) Directory.CreateDirectory(OutputPath);
        var outputPath = defaultPath.Substring(0, defaultPath.LastIndexOf("Assets", StringComparison.Ordinal)) + OutputPath;
        foreach (var filePath in builds.Select(ab => outputPath + "/" + ab.assetBundleName).Where(File.Exists))
        {
            File.Delete(filePath);
        }
        BuildPipeline.BuildAssetBundles(OutputPath, builds.ToArray(), BuildAssetBundleOptions.None, buildTarget);

        if (File.Exists(OutputPath + "/" + abb.assetBundleName))
        {
            foreach (var path in abb.assetNames) Debug.Log("打包资源:" + path);
            Debug.Log("资源[" + abb.assetBundleName + "]打包完成,目标平台:" + buildTarget + ",输出目录:" + OutputPath + "/" + abb.assetBundleName);
        }
        else
        {
            Debug.Log("资源[" + abb.assetBundleName + "]打包失败,未找到合适的打包资源");
        }

        ClearUselessFiles(outputPath);
    }

    [MenuItem("AssetBundle/Select Folder To Separate Bundle For Android")]
    static void SelectFolderToSeparateBundleForAndroid()
    {
        var buildTarget = BuildTarget.Android;
        var defaultPath = Application.dataPath;
        var openPath = EditorUtility.OpenFolderPanel("Open Folder", defaultPath, null).Replace('\\', '/');
        if (string.IsNullOrEmpty(openPath)) return;

        var builds = (from dirPath in Directory.GetDirectories(openPath)
                      let dirName = dirPath.Substring(dirPath.Replace('\\', '/').LastIndexOf('/') + 1).ToLower()
                      let dirRoot = dirPath.Substring(0, dirPath.IndexOf("Assets", StringComparison.Ordinal) - 1)
                      select new AssetBundleBuild
                      {
                          assetBundleName = dirName + "." + FileSuffixes,
                          assetNames = GetFolderList(new List<string>(), dirPath, dirRoot).ToArray()
                      }).ToList();

        if (!Directory.Exists(OutputPath)) Directory.CreateDirectory(OutputPath);
        var outputPath = defaultPath.Substring(0, defaultPath.LastIndexOf("Assets", StringComparison.Ordinal)) + OutputPath;
        foreach (var filePath in builds.Select(ab => outputPath + "/" + ab.assetBundleName).Where(File.Exists))
        {
            File.Delete(filePath);
        }
        BuildPipeline.BuildAssetBundles(OutputPath, builds.ToArray(), BuildAssetBundleOptions.None, buildTarget);

        foreach (var abb in builds)
        {
            if (File.Exists(OutputPath + "/" + abb.assetBundleName))
            {
                foreach (var path in abb.assetNames) Debug.Log("打包资源:" + path);
                Debug.Log("资源[" + abb.assetBundleName + "]打包完成,目标平台:" + buildTarget + ",输出目录:" + OutputPath + "/" + abb.assetBundleName);
            }
            else
            {
                Debug.Log("资源[" + abb.assetBundleName + "]打包失败,未找到合适的打包资源");
            }
        }

        ClearUselessFiles(outputPath);
    }

    #region 目录获取
    static List<string> GetFileList(List<string> list, string srcPath, string rootPath = null)
    {
        var filter = new[] { "meta" }.ToList();
        if (!Directory.Exists(srcPath)) Directory.CreateDirectory(srcPath);
        list.AddRange(from path in Directory.GetFiles(srcPath) where !filter.Contains(path.Substring(path.LastIndexOf('.') + 1)) select string.IsNullOrEmpty(rootPath) ? path.Replace('\\', '/') : path.Substring(rootPath.Length + 1).Replace('\\', '/'));
        return list;
    }

    static List<string> GetFolderList(List<string> list, string srcPath, string rootPath = null)
    {
        if (!Directory.Exists(srcPath)) Directory.CreateDirectory(srcPath);
        list = GetFileList(list, srcPath, rootPath);
        return Directory.GetDirectories(srcPath).Aggregate(list, (current, path) => GetFolderList(current, path, rootPath));
    }

    #endregion

    #region 目录操作
    static void DeleteAll(string path)
    {
        foreach (var file in Directory.GetFiles(path).Select(filePath => new FileInfo(filePath)).Where(file => file.Exists))
        {
            file.Delete();
        }
    }

    static void ClearUselessFiles(string path)
    {
        foreach (var file in from filePath in Directory.GetFiles(path)
                             select new FileInfo(filePath) into file
                             let suffix = file.Name.Substring(file.Name.LastIndexOf('.') + 1)
                             where suffix != FileSuffixes
                             select file)
        {
            file.Delete();
        }
    }

    #endregion

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值