AssetBundle解析xml打包

//xml
<?xml version='1.0' encoding='utf-8'?>
<Root>
  <AssetBundle>
    <Item BundleName="RollerBall" BundleType="Prefab">
      <Path>Cube.prefab</Path>
    </Item>
    <Item BundleName="bb" BundleType="scene">
      <Path>b.unity</Path>
    </Item>
  </AssetBundle>
</Root>

//解析xml
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Linq;
public class ConfigParse
{
    public static List<AssetsBundleEntry> entries = new List<AssetsBundleEntry>();
    public static Dictionary<AssetsBundleEntry, bool> isSelects = new Dictionary<AssetsBundleEntry, bool>();
    public void Parse(string path)
    {
        entries.Clear();
        isSelects.Clear();
        TextAsset textAsset = Resources.Load(path) as TextAsset;
        XDocument xDocument = XDocument.Parse(textAsset.text);
        XElement root = xDocument.Root;
        XElement element = root.Element("AssetBundle");
        IEnumerable enumerable = element.Elements("Item");
        foreach (XElement item in enumerable)
        {
            AssetsBundleEntry entry = new AssetsBundleEntry();
            entry.BundleName = item.Attribute("BundleName").Value.ToString();
            entry.BundleType = item.Attribute("BundleType").Value.ToString();
            IEnumerable paths = item.Elements("Path");
            foreach (XElement pathitem in paths)
            {
                entry.paths.Add(pathitem.Value.ToString());
            }
            entries.Add(entry);
            isSelects.Add(entry,true);
        }
    }
}
//AssetBundle信息类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AssetsBundleEntry
{
    public string BundleName;
    public string BundleType;
    public List<string> paths;
    public AssetsBundleEntry()
    {
        paths = new List<string>();
    }
}
//添加菜单项
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Menu
{
    static ConfigParse parse;
    [MenuItem("自定义面板/创建AB包")]
    public static void a() {
        AssetBundleWindow window = EditorWindow.GetWindow<AssetBundleWindow>();
        window.titleContent = new GUIContent("创建AB包");
        parse = new ConfigParse();
        parse.Parse("AssetsBundleConfig");
        window.Show();
    }
}
//窗口
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetBundleWindow : EditorWindow
{
    int index = 0;
    Vector2 vector2;
    BuildTarget target;
    string[] str = new string[] { "Window", "Android", "IOS" };
    private void OnGUI()
    {
        index = EditorGUILayout.Popup(index, str);
        EditorGUILayout.BeginHorizontal("box");
        GUILayout.Label("");
        GUILayout.Label("包名");
        GUILayout.Label("包类型");
        EditorGUILayout.EndHorizontal();
        vector2 = GUILayout.BeginScrollView(vector2);
        for (int i = 0; i < ConfigParse.entries.Count; i++)
        {
            EditorGUILayout.BeginHorizontal("box");
            ConfigParse.isSelects[ConfigParse.entries[i]] = GUILayout.Toggle(ConfigParse.isSelects[ConfigParse.entries[i]], "是否打包");
            GUILayout.Label(ConfigParse.entries[i].BundleName);
            GUILayout.Label(ConfigParse.entries[i].BundleType);
            EditorGUILayout.EndHorizontal();
            foreach (string item in ConfigParse.entries[i].paths)
            {
                EditorGUILayout.BeginHorizontal("box");
                GUILayout.Space(150);
                GUILayout.Label(item);
                EditorGUILayout.EndHorizontal();
            }
        }
        GUILayout.EndScrollView();
        if (GUILayout.Button("开始打包", GUILayout.Width(200)))
        {
            switch (index)
            {
                case 0:
                    target = BuildTarget.StandaloneWindows64;
                    break;
                case 1:
                    target = BuildTarget.Android;
                    break;
                case 2:
                    target = BuildTarget.iOS;
                    break;
            }
            foreach (var item in ConfigParse.entries)
            {
                Build(item);
            }
        }
    }
    private void Build(AssetsBundleEntry entry)
    {
        AssetBundleBuild build = new AssetBundleBuild();
        string[] datapath = new string[entry.paths.Count];
        for (int i = 0; i < entry.paths.Count; i++)
        {
            datapath[i] = "Assets/Resources/" + entry.paths[i];
        }
        build.assetNames = datapath;
        build.assetBundleName = entry.BundleName;
        if (entry.BundleType == "scene")
        {
            build.assetBundleVariant = "u3d";
        }
        else
        {
            build.assetBundleVariant = "assetbundle";
        }
        AssetBundleBuild[] builds = new AssetBundleBuild[1];
        builds[0] = build;
        string path = Application.dataPath + "/.." + "/AssetBundle/" + target.ToString()   ;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        BuildPipeline.BuildAssetBundles(path, builds, BuildAssetBundleOptions.None, target);
    }
}

猜你喜欢

转载自blog.csdn.net/itliruochong/article/details/80302104