zoukankan      html  css  js  c++  java
  • unity assetbundle builder

    
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System;
    using System.IO;
    using ICSharpCode.SharpZipLib.Zip;
    using System.Text.RegularExpressions;
    
    public class AssetBundleBuilder : EditorWindow
    {
        static string m_Version = "1.1.1";
        static string m_ResourcesPath = "Assets/Resources/";
        static BuildTarget m_BuildTarget = BuildTarget.StandaloneWindows64;
        static List<string> m_IgnoreSuffixs = new List<string> { ".cs", ".shader", ".unity" };
    
        static string m_PlatformPath;
        static string m_ManifestPath;
        static string m_AssetBundlePath;
    
        [MenuItem("Tools/AssetBundleBuilder")]
        static void Init()
        {
            var window = (AssetBundleBuilder)GetWindow(typeof(AssetBundleBuilder));
            window.Show();
        }
    
        void OnGUI()
        {
            GUILayout.Label("AssetBundleBuilder", EditorStyles.boldLabel);
            EditorGUILayout.LabelField("打包平台", AssetBundleUtil.GetPlatform());
            m_Version = EditorGUILayout.TextField("版本号", m_Version);
            m_BuildTarget = (BuildTarget)EditorGUILayout.EnumPopup("目标平台", m_BuildTarget);
    
            m_PlatformPath = "D:/PackProject/AssetBundle/" + m_BuildTarget.ToString();
            m_ManifestPath = m_PlatformPath + "/manifest";
            m_AssetBundlePath = m_PlatformPath + "/" + m_Version;
    
            EditorGUILayout.LabelField("保存路径: ", m_AssetBundlePath);
    
            if (GUILayout.Button("Clear Manifest Folder"))
                ClearStorageFolder();
    
            if (GUILayout.Button("Clear All AssetBundleNames"))
                ClearAssetBundleName();
    
            if (GUILayout.Button("Build"))
                Build();
        }
    
        static void Build()
        {
            ClearAssetBundleName();
            SetAssetBundleName();
            CreateAssetBundle();
            MoveAssetBundle();
            BuildZipFile();
        }
    
        static void MoveAssetBundle()
        {
            var info = new DirectoryInfo(m_AssetBundlePath);
            if (info.Exists)
                info.Delete(true);
    
            info.Create();
            var index = 0;
            var assetFolder = new DirectoryInfo(m_ManifestPath);
            var allFiles = assetFolder.GetFiles("*", SearchOption.AllDirectories);
            foreach (var file in allFiles)
            {
                if (file.FullName.Contains(".manifest"))
                    continue;
    
                var name = file.FullName.Replace("\", "/");
                name = name.Replace(m_ManifestPath, "");
                var t = m_AssetBundlePath + name;
                var folder = t.Remove(t.LastIndexOf("/"));
                var tempfolder = new DirectoryInfo(folder);
                if (! tempfolder.Exists)
                    tempfolder.Create();
    
                file.MoveTo(t);
                EditorUtility.DisplayProgressBar("移动文件。。", name, (float)index / allFiles.Length);
            }
    
            EditorUtility.ClearProgressBar();
        }
    
        static void ClearStorageFolder()
        {
            var dirInfo = new DirectoryInfo(m_ManifestPath);
            if (dirInfo.Exists)
                dirInfo.Delete(true);
        }
    
        static void BuildZipFile()
        {
            var infos = new DirectoryInfo(m_AssetBundlePath);
            var compressTotal = infos.GetFiles("*.*", SearchOption.AllDirectories).Length;
            if (compressTotal <= 0)
                return;
    
            var folders = new string[] { m_AssetBundlePath };
            var outputZip = m_PlatformPath + "/res_all_" + m_Version + ".zip";
            ZipUtility.Zip(folders, outputZip, "aiteachu", new AssetBundleZipCallBack(compressTotal));
            EditorUtility.ClearProgressBar();
        }
    
        static void ClearAssetBundleName()
        {
            var names = AssetDatabase.GetAllAssetBundleNames();
            var index = 0;
            foreach (var name in names)
            {
                index++;
                var progress = (float)index / names.Length;
                EditorUtility.DisplayProgressBar("清除AssetBundle标记", name, progress);
                AssetDatabase.RemoveAssetBundleName(name, true);
            }
    
            EditorUtility.ClearProgressBar();
        }
    
        static void SetAssetBundleName()
        {
            //SetAssetName(m_ResourcesPath, ".json", false);
            SetAssetName(m_ResourcesPath, ".playable", false);
            //SetAssetName(m_ResourcesPath + "UIImage/", ".png", false);
            //SetAssetName(m_ResourcesPath + "UIImage/", ".jpg", false);
            //SetAssetName(m_ResourcesPath, ".mat", false);
            //SetAssetName(m_ResourcesPath, ".prefab", false);
            //SetAssetName(m_ResourcesPath, ".controller", false);
            //SetAssetName(m_ResourcesPath, ".unity", false);
        }
    
        static void CreateAssetBundle()
        {
            var info = new DirectoryInfo(m_ManifestPath);
            if (! info.Exists)
                info.Create();
    
            var option = BuildAssetBundleOptions.ChunkBasedCompression;
            BuildPipeline.BuildAssetBundles(m_ManifestPath, option, m_BuildTarget);
        }
    
        static void SetAssetName(string resourcesPath, string filter, bool excludeDependences)
        {
            var paths = GetPaths(resourcesPath, filter);
            var index = 0;
            foreach (var path in paths)
            {
                index++;
                var progress = (float)index / paths.Count;
                if (!CheckPath(path))
                {
                    Debug.LogError("path has error : " + path);
                    continue;
                }
    
                var tempPath = path.Replace("\", "/");
                var name = tempPath.Remove(tempPath.LastIndexOf('.'));
                SetName(tempPath, name);
    
                if (excludeDependences)
                    SetDenpendencesName(tempPath);
    
                EditorUtility.DisplayProgressBar("SetName...", path, progress);
            }
    
            EditorUtility.ClearProgressBar();
        }
    
        // 去除掉不符合命名规则的文件 文件名只包含数字,大小写字符,下滑线 空格 & . -
        public static bool CheckPath(string path)
        {
            var name = Path.GetFileNameWithoutExtension(path);
            var rex = new Regex(@"^[0-9A-Za-z_&][0-9A-Za-z_s&.-]*$");
            var ma = rex.Match(name);
            return ma.Success;
        }
    
        static void SetDenpendencesName(string target)
        {
            var denpendences = AssetDatabase.GetDependencies(target);
            foreach (var dependencePath in denpendences)
            {
                var extension = Path.GetExtension(dependencePath);
                if (m_IgnoreSuffixs.Contains(extension))
                    continue;
    
                var name = dependencePath.Remove(dependencePath.LastIndexOf('.'));
                SetName(dependencePath, name);
            }
        }
    
        static void SetName(string path, string name)
        {
            var import = AssetImporter.GetAtPath(path);
            if (string.IsNullOrEmpty(import.assetBundleName))
            {
                name = name.Replace(" ", "");
                name = name.Replace("-", "_");
                import.SetAssetBundleNameAndVariant(name, null);
            }
        }
    
        static List<string> GetPaths(string dirPath, string suffix)
        {
            var ls = new List<string>();
            foreach (string path in Directory.GetFiles(dirPath))
            {
                if (Path.GetExtension(path) == suffix)
                    ls.Add(path.Substring(path.IndexOf("Assets")));
            }
    
            var dirs = Directory.GetDirectories(dirPath);
            if (dirs.Length > 0)
            {
                foreach (string path in dirs)
                    ls.AddRange(GetPaths(path, suffix));
            }
    
            return ls;
        }
    
        class AssetBundleZipCallBack : ZipUtility.ZipCallback
        {
            int m_Index;
            int m_Total;
    
            public AssetBundleZipCallBack(int total)
            {
                m_Total = total;
            }
    
            // 压缩单个文件或文件夹后执行的回调
            public override void OnPostZip(ZipEntry entry)
            {
                m_Index++;
                var progress = (float)m_Index / m_Total;
                EditorUtility.DisplayProgressBar("压缩文件夹", entry.Name, progress);
            }
    
            public override void OnFinished(bool result)
            {
                m_Index = 0;
                m_Total = 0;
                if(result)
                    Debug.Log("======压缩完成=========");
                else
                    Debug.Log("======压缩失败=========");
            }
        }
    }
    
    
    
     
  • 相关阅读:
    shell脚本--php执行普通shell命令
    shell脚本--eval执行shell命令
    shell脚本--CGI获取请求数据(GET / POST)
    shell脚本--编写CGI代码(shell结合html)以及环境变量
    shell脚本--初识CGI
    数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
    写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。
    输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
    字符串分隔 ->连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
  • 原文地址:https://www.cnblogs.com/liucUP/p/12803018.html
Copyright © 2011-2022 走看看