AssetBundle.cs

来源:互联网 发布:tcp20端口和21端口 编辑:程序博客网 时间:2024/06/14 00:40
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}

原创粉丝点击