底层加载资源框架学习(八):打AB包

代码:

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class BundleEditor  
{
	#region 路径
	public static string m_BundleTargetPath=Application.streamingAssetsPath;
	public static string ABCONFIGPATH="Assets/Editor/ABConfig.asset";
	
	#endregion

	/// <summary>
	/// 存放所有的文件夹
	/// 所有的文件夹ab包的Dic
	/// </summary>
	/// <typeparam name="string">key ab包名</typeparam>
	/// <typeparam name="string">value 路径</typeparam>
	/// <returns></returns>
	private static Dictionary<string,string> m_AllFileDir=new Dictionary<string, string>();

	/// <summary>
	/// 过滤的List
	/// 在打AB包时,如果已经存在文件夹里了,那么在打包单个文件夹的时候就不需要再进行打包了 
	/// </summary>
	/// <typeparam name="string"></typeparam>
	/// <returns></returns>
	private static List<string> m_AllFileABList=new List<string>();

	/// <summary>
	/// 单个Prefab的ab包
	/// </summary>
	/// <typeparam name="string">key ab包名</typeparam>
	/// <typeparam name="string">value 所有的依赖项</typeparam>
	/// <returns></returns>
	private static Dictionary<string,List<string>> m_AllPrefabDir=new Dictionary<string, List<string>>();

	[MenuItem("Tools/打包")]
	public static void Build()
	{
		m_AllFileDir.Clear();
		m_AllFileABList.Clear();
		m_AllPrefabDir.Clear();
		ABConfig abConfig=AssetDatabase.LoadAssetAtPath<ABConfig>(ABCONFIGPATH);
		//把配置表中的文件夹里的所有的要打的ab包加到文件夹字典当中
		foreach(ABConfig.FileDieABName fileDir in abConfig.m_AllFileDirAB)
		{
			if(m_AllFileDir.ContainsKey(fileDir.ABName))
			{
				Debug.LogError("AB包配置名字重复,请检查");
			}
			else{
				m_AllFileDir.Add(fileDir.ABName,fileDir.Path);
				m_AllFileABList.Add(fileDir.Path);
			}			
		}
		//返回的为GUID 查找到指定路径下的所有Prefab
		string[] allStr=AssetDatabase.FindAssets("t:Prefab",abConfig.m_AllPrefabPath.ToArray());
		for(int i=0;i<allStr.Length;i++)
		{
			string path=AssetDatabase.GUIDToAssetPath(allStr[i]);
			EditorUtility.DisplayProgressBar("查找Prefab","Prefab:"+path,i*1.0f/allStr.Length);
			if(!ContainAllFileAB(path))
			{
				//根据路径加载出这个prefab
				GameObject obj=AssetDatabase.LoadAssetAtPath<GameObject>(path);
				//得到这个prefab的所有的依赖项
				string[] allDepend=AssetDatabase.GetDependencies(path);	
				//存储所有的依赖项
				List<string> allDependPath=new List<string>();
				//遍历所有的依赖
				for(int j=0;j<allDepend.Length;j++)
				{	
					//判断当前依赖项是否已经被打包了或者是不是脚本
					if (!ContainAllFileAB(allDepend[j])&&!allDepend[j].EndsWith(".cs"))
					{
						m_AllFileABList.Add(allDepend[j]);
						allDependPath.Add(allDepend[j]);						
					}
				}
				if (m_AllPrefabDir.ContainsKey(obj.name))
				{
					Debug.LogError("存在相当名字的Prefab:"+obj.name);
				}
				else
				{
					//把当前预知以及依赖添加到字典当中
					m_AllPrefabDir.Add(obj.name,allDependPath);
				}
			}
		}

		//对文件夹进行设置ab名字
		foreach (string name in m_AllFileDir.Keys)
		{
			SetABName(name,m_AllFileDir[name]);
		}
		//对单个文件设置ab名字
		foreach (string name in m_AllPrefabDir.Keys)
		{
			SetABName(name,m_AllPrefabDir[name]);
		}

		//进行打包操作
		BuildAssetBundle();

		//清除AB包名,防止meta文件进行改变
		string[] oldABNames=AssetDatabase.GetAllAssetBundleNames();
		for (int i = 0; i < oldABNames.Length; i++)
		{
			AssetDatabase.RemoveAssetBundleName(oldABNames[i],true);
			EditorUtility.DisplayProgressBar("清除AB包名","名字:"+oldABNames[i],i*1.0f/oldABNames.Length);
		}

		AssetDatabase.Refresh();
		EditorUtility.ClearProgressBar();
	}

	

	/// <summary>
	/// 设置AB名字
	/// </summary>
	/// <param name="name">ab包的名字</param>
	/// <param name="path">ab包的路径</param>
	static void SetABName(string name,string path)
	{
		AssetImporter assetImporter=AssetImporter.GetAtPath(path);
		if (assetImporter == null)
		{
			Debug.LogError("不存在此路径文件:Path----"+path);
		}else
		{
			assetImporter.assetBundleName=name;
		}
	}
	/// <summary>
	/// 设置AB名字
	/// </summary>
	/// <param name="name">ab包的名字</param>
	/// <param name="paths">所有打进这个ab包的路径</param>
	static void SetABName(string name,List<string> paths)
	{
		 for (int i = 0; i < paths.Count; i++)
		 {
			 SetABName(name,paths[i]);
		 }
	}

	/// <summary>
	/// 打包
	/// </summary>
	static void BuildAssetBundle()
	{
		//获取所有的AB包名
		string[] allBundls=AssetDatabase.GetAllAssetBundleNames();
		//Key 全路径 value 包名
		Dictionary<string,string> resPathDic=new Dictionary<string, string>();
		for (int i = 0; i < allBundls.Length; i++)
		{
			//根据AB包名 获取全路径
			string[] allBundlePath=AssetDatabase.GetAssetPathsFromAssetBundle(allBundls[i]);
			for (int j = 0; j < allBundlePath.Length; j++)
			{
				 if (allBundlePath[j].EndsWith(".cs"))
				 {
					 continue;
				 }
				 resPathDic.Add(allBundlePath[j],allBundls[i]);
			}
		}

		//删除没用的AB包
		DeleteAB();

		//生成自己的配置表
		

		//进行打包
		BuildPipeline.BuildAssetBundles(m_BundleTargetPath,BuildAssetBundleOptions.ChunkBasedCompression,
		EditorUserBuildSettings.activeBuildTarget);
	}


	/// <summary>
	/// 删除没用的AB
	/// </summary>
	static void DeleteAB()
	{
		//获取所有的AB包名
		string[] allBundleNames=AssetDatabase.GetAllAssetBundleNames();
		//根据路径获取所有的文件夹
		DirectoryInfo directory=new DirectoryInfo(m_BundleTargetPath);
		FileInfo[] files=directory.GetFiles("*",SearchOption.AllDirectories); 
		//判断已经打好的AB包中是否有和当前文件夹名字一样的
		//名字一样代表已经存在AB包中了
		//如果有和AB包名字不一样的 则代表此次打包不需要改ab文件,应该是上次打包遗留的,应该删除掉
		for (int i = 0; i < files.Length; i++)
		{
			if (ContainInABName(files[i].Name, allBundleNames)||files[i].Name.EndsWith(".meta"))
			{
				continue;
			}
			else
			{
				Debug.Log("此AB包已经被删或者改名了:"+files[i].Name);
				if (File.Exists(files[i].FullName))
				{
					File.Delete(files[i].FullName);
				}
			}

		}
	}

	/// <summary>
	/// 判断是否已经在AB中了
	/// 判断已经打好(上次打包后的)的AB包中是否有和当前文件夹名字一样的
	/// 如果一样,说明本次打包还是需要此AB包
	/// 如果有和AB包名字不一样的 则代表此次打包不需要改ab文件,应该是上次打包遗留的,应该删除掉
	/// </summary>
	/// <param name="name">ab包名</param>
	/// <param name="strs">所有的全路径</param>
	/// <returns></returns>
	static bool ContainInABName(string name,string[] strs)
	{
		for (int i = 0; i < strs.Length; i++)
		{
			if (name==strs[i])
			{
				return true;
			}
		}
		return false;
	}

	/// <summary>
	/// 判断是否已经存在过滤的List中了
	/// </summary>
	/// <param name="path"></param>
	/// <returns></returns>
	private static bool ContainAllFileAB(string path)
	{
		for(int i=0;i<m_AllFileABList.Count;i++)
		{
			if(m_AllFileABList[i]==path||path.Contains(m_AllFileABList[i]))
			{
				return true;
			}
		}
		return false;
	}


}

注意:
在进行打包时,应该进行判断上次打的AB包是否需要删除

/// <summary>
/// 删除没用的AB
/// </summary>
static void DeleteAB()
{
	//获取所有的AB包名
	string[] allBundleNames=AssetDatabase.GetAllAssetBundleNames();
	//根据路径获取所有的文件夹
	DirectoryInfo directory=new DirectoryInfo(m_BundleTargetPath);
	FileInfo[] files=directory.GetFiles("*",SearchOption.AllDirectories); 
	//判断已经打好的AB包中是否有和当前文件夹名字一样的
	//名字一样代表已经存在AB包中了
	//如果有和AB包名字不一样的 则代表此次打包不需要改ab文件,应该是上次打包遗留的,应该删除掉
	for (int i = 0; i < files.Length; i++)
	{
		if (ContainInABName(files[i].Name, allBundleNames)||files[i].Name.EndsWith(".meta"))
		{
			continue;
		}
		else
		{
			Debug.Log("此AB包已经被删或者改名了:"+files[i].Name);
			if (File.Exists(files[i].FullName))
			{
				File.Delete(files[i].FullName);
			}
		}

	}
}

/// <summary>
/// 判断是否已经在AB中了
/// 判断已经打好(上次打包后的)的AB包中是否有和当前文件夹名字一样的
/// 如果一样,说明本次打包还是需要此AB包
/// 如果有和AB包名字不一样的 则代表此次打包不需要改ab文件,应该是上次打包遗留的,应该删除掉
/// </summary>
/// <param name="name">ab包名</param>
/// <param name="strs">所有的全路径</param>
/// <returns></returns>
static bool ContainInABName(string name,string[] strs)
{
	for (int i = 0; i < strs.Length; i++)
	{
		if (name==strs[i])
		{
			return true;
		}
	}
	return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值