BundleBuilderTimeline

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

public class BundleBuilderTimeline : BundleBuilder
{
    //[MenuItem("====Build Tools====/[]打包 Timeline 相关")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<BundleBuilderTimeline>(true);
    }
    /// <summary>滚动条坐标</summary>
    private Vector2 scrollPos;

    /// <summary>
    /// Avatar预设文件夹,按照功能文件夹进行打包.
    /// </summary>
    private static string[] ActorAssetFoldersFiles = new string[]
    {
        "Assets/Content/OperaMod",
    };

    private static List<AssetEntry> allFolders = new List<AssetEntry>();
    private static string[] buildFolders;

    static void ListAll()
    {
        allFolders.Clear();
        foreach (string assetFolder in ActorAssetFoldersFiles)
        {
            foreach (string folder in Directory.GetDirectories(assetFolder, "*", SearchOption.AllDirectories))
            {
                allFolders.Add(new AssetEntry() { path = folder.Replace('\\', '/') });
            }
        }
    }

    static void GetBuildFilesAndFolders()
    {
        // 需要生成的文件夹
        List<AssetEntry> folders = allFolders.FindAll((entry) => { return entry.selected; });
        buildFolders = new string[folders.Count];
        for (int i = 0; i < folders.Count; ++i)
        {
            buildFolders[i] = folders[i].path;
        }
    }

    private static Dictionary<string, bool> RefList = new Dictionary<string, bool>();

    static void Build()
    {
        // 没有需要生成的内容,退出
        if (buildFolders.Length == 0)
            return;
        outputPath = FileSystem.PackagePath();
        builds.Clear();

        for (int i = 0; i < buildFolders.Length; ++i)
        {
            string[] files = System.Array.FindAll(Directory.GetFiles(buildFolders[i]), (file) =>
            {
                string ext = Path.GetExtension(file);
                if (ext == ".meta" || ext == ".unity")
                    return false;
                return true;
            });
            string pathName = buildFolders[i].Replace("Assets/Content/", "");
            pathName = pathName.Replace("/", "").ToLower();

            string[] deps = AssetDatabase.GetDependencies(pathName);

            AssetImporter temp = AssetImporter.GetAtPath(pathName);
            builds.Add(new AssetBundleBuild()
            {
                assetBundleName = pathName + ".unity3d",
                assetNames = files,
            }
            );

            GetDependencies(buildFolders[i], ".shader", ref RefList);//获取需要单独提取的资源.
        }

        //if (target != mBuildTarget)
        //{
        //    EditorUserBuildSettings.SwitchActiveBuildTarget(target);
        //}

        string tempFolder = Application.dataPath + "/../BuildTemp" + "/ryzTimeline";
        DebugHelper.Log(tempFolder);
        //if (Directory.Exists(tempFolder))
        //    Directory.Delete(tempFolder, true);
        //Directory.CreateDirectory(tempFolder);
        if (!Directory.Exists(tempFolder))
        {
            Directory.CreateDirectory(tempFolder);
        }
        AssetBundleManifest abm = BuildPipeline.BuildAssetBundles(tempFolder, builds.ToArray(),
          BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression,
          target);

        if (abm)
        {
            DebugHelper.Log("<color=green>================ BuildTimeline Successsssssssssssssssss ================</color>");
            //EditorUtility.DisplayDialog("", "Build Actor Completed", "OK");
        }
        else
        {
            DebugHelper.LogError("BuildTimeline  Faileddddddddddddd");
            //EditorUtility.DisplayDialog("", "Build Actor Failed", "OK");
        }
        FileSystem.CreateDirIfNotExist(outputPath);
        foreach (string file in Directory.GetFiles(tempFolder))
        {
            if (Path.GetExtension(file) == ".unity3d")
            {
                if (Path.GetFileName(file) == Path.GetFileName(tempFolder))
                    continue;
                string des = Path.Combine(outputPath, Path.GetFileName(file));
                if (File.Exists(des))
                    File.Delete(des);
                File.Copy(file, des);
            }
        }
    }

    void OnEnable()
    {
        target = EditorUserBuildSettings.activeBuildTarget;// BuildTarget.Android;
        titleContent = new GUIContent("Timeline");
        minSize = new Vector2(640f, 600f);
        maxSize = new Vector2(640f, 1200f);
        ListAll();
    }

    void OnGUI()
    {
        //BuildTargetSetting();
        #region Timeline Playables;
        GUILayout.Label("Timeline Playables", EditorStyles.boldLabel);

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Select All"))
        {
            SelectAll(allFolders);

        }
        if (GUILayout.Button("Unselect All"))
        {
            UnselectAll(allFolders);
        }
        GUILayout.EndHorizontal();
        // 生成按钮
        if (GUILayout.Button("Build"))
        {
            GetBuildFilesAndFolders();
            Build();
        }

        scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Height(500));
        foreach (var folder in allFolders)
        {
            folder.selected = GUILayout.Toggle(folder.selected, folder.path);
        }
        GUILayout.EndScrollView();
        #endregion Timeline Playables;
    }

    public static void CommandBuild()
    {
        ListAll();
        GetBuildFilesAndFolders();
        Build();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值