unity assetbundle builder

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

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 bool m_IsClearFolder = false;

    [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_IsClearFolder = EditorGUILayout.Toggle("清空文件夹", m_IsClearFolder);

        var targetPath = "D:/PackProject/AssetBundle/" + m_BuildTarget.ToString() + "/" + m_Version + "/manifest";
        EditorGUILayout.LabelField("保存路径: ", targetPath);

        if (GUILayout.Button("Build"))
            Build(targetPath, m_Version, m_BuildTarget);

        if (GUILayout.Button("Clear All AssetBundleNames"))
            ClearAssetBundleName();
    }

    static void Build(string targetPath, string version, BuildTarget target)
    {
        ClearAssetBundleName();
        SetAssetBundleName();
        CreateAssetBundle(targetPath);
    }

    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 + "UIImage/", ".png", false);
        SetAssetName(m_ResourcesPath, ".playable", false);
        SetAssetName(m_ResourcesPath, ".mat", false);
        SetAssetName(m_ResourcesPath, ".prefab", false);
        SetAssetName(m_ResourcesPath, ".controller", false);
        SetAssetName(m_ResourcesPath, ".unity", false);
    }

    static void CreateAssetBundle(string targetPath)
    {
        var option = BuildAssetBundleOptions.ChunkBasedCompression;
        var dirInfo = new DirectoryInfo(targetPath);
        if (!dirInfo.Exists)
            dirInfo.Create();

        BuildPipeline.BuildAssetBundles(targetPath, 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;
            var tempPath = path.Replace("\\", "/");
            var name = tempPath.Remove(tempPath.LastIndexOf('.'));
            SetName(tempPath, name);

            if (excludeDependences)
                SetDenpendencesName(tempPath);

            EditorUtility.DisplayProgressBar("SetName...", path, progress);
        }

        EditorUtility.ClearProgressBar();
    }

    static void SetDenpendencesName(string target)
    {
        var denpendences = AssetDatabase.GetDependencies(target, true);
        foreach (var dependencePath in denpendences)
        {
            var extension = Path.GetExtension(dependencePath);
            if (m_IgnoreSuffixs.Contains(extension))
                continue;

            var name = target.Remove(target.LastIndexOf('.'));
            SetName(dependencePath, name);
        }
    }

    static void SetName(string path, string name)
    {
        var import = AssetImporter.GetAtPath(path);
        if (string.IsNullOrEmpty(import.assetBundleName))
        {
            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;
    }

}

猜你喜欢

转载自www.cnblogs.com/liucUP/p/12803018.html