BundleBuilderFX

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

public class BundleBuilderFX : BundleBuilder
{
    //[MenuItem("====Build Tools====/[5]打包 特效 FX 相关")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<BundleBuilderFX>(true);
    }

    /// <summary>滚动条坐标</summary>
    private Vector2 scrollPos;

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

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

    private static void ListAll()
    {
        allFolders.Clear();
        foreach (string assetFolder in ActorAssetFoldersFiles)
        {
            foreach (string folder in Directory.GetFiles(assetFolder, "*", SearchOption.AllDirectories))
            {
                string ext = Path.GetExtension(folder);
                if (ext == ".meta" || ext == ".unity")
                    continue;
                allFolders.Add(new AssetEntry() { path = folder.Replace('\\', '/') });
            }
        }
    }

    private 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 void Build()
    {
        // 没有需要生成的内容,退出
        if (buildFolders.Length == 0)
            return;

        builds.Clear();
        outputPath = FileSystem.PackagePath();
        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();
            //pathName = Path.GetFileNameWithoutExtension(pathName);
            pathName = pathName.Replace(".prefab", "");
            builds.Add(new AssetBundleBuild()
            {
                assetBundleName = pathName + ".unity3d",
                assetNames = files,
            }
            );
        }
        //if (target != mBuildTarget)
        //{
        //    EditorUserBuildSettings.SwitchActiveBuildTarget(target);
        //}

        string tempFolder = Application.dataPath + "/../BuildTemp" + "/ryzfx";
        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>================ BuildFX Successsssssssssssssssss ================</color>");
            //EditorUtility.DisplayDialog("", "Build FX Completed", "OK");
        }
        else
        {
            DebugHelper.LogError("BuildFX  Faileddddddddddddd");
            //EditorUtility.DisplayDialog("", "Build FX 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);
            }
        }
    }

    private void OnEnable()
    {
        target = EditorUserBuildSettings.activeBuildTarget;// BuildTarget.Android;
        titleContent = new GUIContent("FX");
        minSize = new Vector2(640f, 600f);
        maxSize = new Vector2(640f, 1200f);
        ListAll();
    }
    private void OnGUI()
    {
        //BuildTargetSetting();
        #region Avatar Prefabs;

        GUILayout.Label("FX Prefabs", 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 Avatar Prefabs;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值