using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// 自定义面板 显示在菜单栏上
/// </summary>
public class Menu
{
static configParse parse; //调用解析类
[MenuItem("Build/CreateAssetBundle")]
//打开的方法
public static void OpenAssetBunlde()
{
AssetBundleWindow window = EditorWindow.GetWindow<AssetBundleWindow>();
window.titleContent=new GUIContent("创建ab包");
//实例解析数据类
parse = new configParse();
//调用解析的方法
parse.parse("Goods");
window.Show();
}
}
<?xml version='1.0' encoding='utf-8'?>
<root>
<AssetBundle>
<item BundleName="aaa" BundleType="prefab">
<path>Cube.prefab</path>
</item>
<item BundleName="bbb" BundleType="prefab">
<path>Sphere1.prefab</path>
</item>
<item BundleName="ccc" BundleType="prefab">
<path>Sphere2.prefab</path>
</item>
<item BundleName="ddd" BundleType="prefab">
<path>Sphere3.prefab</path>
</item>
<item BundleName="eee" BundleType="prefab">
<path>Sphere4.prefab</path>
</item>
</AssetBundle>
</root>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Linq;
/// <summary>
/// 数据类
/// </summary>
public class goods
{
public string BundleName; //包名称
public string BundleType; //包类型
public string path; //地址路径
public List<string> Pathlist; //创建集合存地址path
public goods()
{
Pathlist = new List<string>();
}
}
/// <summary>
/// 解析数据
/// </summary>
public class configParse
{
//创建集合存数据
public static List<goods> goodsList = new List<goods>();
// 判断是否打钩 用字典存数据
public static Dictionary<goods, bool> dic = new Dictionary<goods, bool>();
/// <summary>
/// 解析的方法
/// </summary>
/// <param name="path"></param>
public void parse(string path)
{
//清空集合 否则会累加
goodsList.Clear();
dic.Clear();
//加载text文件
TextAsset text = Resources.Load(path) as TextAsset;
//获取文本
XDocument doc = XDocument.Parse(text.text);
//获取root根节点
XElement root = doc.Root;
//获取下面的子节点
XElement assetbundle = root.Element("AssetBundle");
//获取子节点下面的所有节点
IEnumerable paths = assetbundle.Elements("item");
//循环遍历item里面的属性
foreach (XElement pathitem in paths)
{
//实例化数据类
goods goods = new goods();
//获取子节点下面的属性 item的属性
goods.BundleName = pathitem.Attribute("BundleName").Value.ToString();
goods.BundleType = pathitem.Attribute("BundleType").Value.ToString();
IEnumerable pathss = pathitem.Elements("path");
//遍历获取 path中的数据
foreach (XElement ele in pathss)
{
//添加到path集合中
goods.Pathlist.Add(ele.Value.ToString());
}
goodsList.Add(goods);//添加到存储数据中
dic.Add(goods,true);//添加到字典中
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
using System;
/// <summary>
/// AB包窗体
/// </summary>
public class AssetBundleWindow : EditorWindow
{
//下标
int index = 0;
Vector2 vec; //2维
BuildTarget target; //打包平台
//下拉菜单中的选项
string[] str = new string[] { "windows", "Android", "IOS" };
/// <summary>
/// 在OnGUI 中实现布局
/// </summary>
private void OnGUI()
{
//下拉列表
index = EditorGUILayout.Popup(index,str);
//设置开始盒子包裹 里面的文字
EditorGUILayout.BeginHorizontal("box");
//包裹的文字
GUILayout.Label("包名");
GUILayout.Label("包类型");
//结束包裹
EditorGUILayout.EndHorizontal();
//开始 滑动的位置 在标题后面
vec = GUILayout.BeginScrollView(vec);
//遍历解析数据类中的数据
for (int i = 0; i < configParse.goodsList.Count; i++)
{
//开始包裹
EditorGUILayout.BeginHorizontal("box");
//设置复选框,调用数据类中字典 键为goodlist,值为bool值
//返回字典的键list
configParse.dic[configParse.goodsList[i]] = GUILayout.Toggle(configParse.dic[configParse.goodsList[i]], "是否打包");
//设置lable中显示
GUILayout.Label(configParse.goodsList[i].BundleName);
GUILayout.Label(configParse.goodsList[i].BundleType);
//结束包裹
EditorGUILayout.EndHorizontal();
//遍历地址path
foreach (string item in configParse.goodsList[i].Pathlist)
{
EditorGUILayout.BeginHorizontal("box");
GUILayout.Space(300);
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.goodsList)
{
Build(item);//调用打包的方法
}
}
}
//打包的方法
private void Build(goods item)
{
AssetBundleBuild build = new AssetBundleBuild();
string[] datapath = new string[item.Pathlist.Count];
for (int i = 0; i < item.Pathlist.Count; i++)
{
datapath[i] = "Assets/Resources/" + item.Pathlist[i];
}
build.assetNames = datapath;
build.assetBundleName = item.BundleName + ".ab";
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);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
private CharacterController cc; //角色控制器
private float speed = 5; //速度
void Start()
{
cc = this.GetComponent<CharacterController>();//获取组件
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
if (h != 0 || v != 0)
{
Vector3 vec = new Vector3(h, 0, v);
//移动物体
cc.SimpleMove(vec * 3f);
}
//遍历 LoadLevel类中的集合
foreach (GameObject item in LoadLevel.lists)
{
//设置距离
float dis = Vector3.Distance(this.transform.position, item.transform.position);
//距离小于3f时 物体消失 否则就显示
if (dis < 3f)
{
item.SetActive(false);
}
else
{
item.SetActive(true);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadLevel : MonoBehaviour
{
string[] str = { "aaa", "bbb", "ccc", "ddd", "eee" };
public GameObject[] games;
public static List<GameObject> lists = new List<GameObject>();
private void Start()
{
StartCoroutine(Load());
}
private IEnumerator Load()
{
foreach (string item in str)
{
WWW www =new WWW("http://127.0.0.1:1512/AB/" + item + ".ab");
//等待加载
yield return www;
//判断没有错误
if (www.error == null)
{
AssetBundle ab = www.assetBundle;
games = ab.LoadAllAssets<GameObject>();
foreach (GameObject go in games)
{
GameObject game = Instantiate(go);
if (go.name == "Cube")
{
game.AddComponent<PlayerMove>();
Debug.Log(go.name);
}
else
{
lists.Add(game);
}
// Debug.Log(lists.Count);
ab.Unload(false);
}
}
}
}
}