打包策略:
1.正常打包:
编辑器下设置文件夹或文件AB包名---生成AB包---根据manifes进行依赖加载
2.项目打包策略
设置编辑器工具统一设置AB包名及路径管理---根据依赖关系生成不冗余AB包---根据基于Asset的全路径生成自己的依赖关系---根据自己的依赖关系表加载AB包,编辑器下直接加载资源
打包分为两种:单个文件打包/文件夹进行打包
一.根据Assets配置打包配置表:由于字典不能被序列化所有用结构体代替
[CreateAssetMenu(fileName = "ABConfig", menuName = "CreatAssets", order = 0)]
public class ABConfig : ScriptableObject
{
public List<string> m_AllPrefabPath=new List<string>();
public List<FileDirABName> m_AllFileDirAB = new List<FileDirABName>();
[System.Serializable]
public struct FileDirABName
{
public string ABName;
public string Path;
}
}
二.获取Assets配置面板的路径信息并储存起来
public static string ABCONFIGPATH = "Asset/Editor/ABConfig.asset";
//key是包名,value是路径,所有文件夹sb包dic
public static Dictionary<string, string> m_AllFileDir = new Dictionary<string, string>();
[MenuItem("Tools/打包")]
public static void Build()
{
m_AllFileDir.Clear();
ABConfig aBConfig = AssetDatabase.LoadAssetAtPath<ABConfig>(ABCONFIGPATH);
foreach (ABConfig.FileDirABName file in aBConfig.m_AllFileDirAB)
{
if (m_AllFileDir.ContainsKey(file.ABName))
{
Debug.LogError("AB包配置名字重复,请检查");
}
else
{
m_AllFileDir.Add(file.ABName,file.Path);
}
}
string[] allstr = AssetDatabase.FindAssets("t:Prefab",aBConfig.m_AllPrefabPath.ToArray());
for (int i = 0; i < allstr.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(allstr[i]);//把GUid转换路径
EditorUtility.DisplayProgressBar("查找预制体", "Prefab:" + path, i * 1.0f / allstr.Length);//进度条
}
EditorUtility.ClearProgressBar();//关闭进度条
}
三.获取预制体依赖和消除重复打包,获取预制体文件打包配置表
//加载预制体的依赖列表并且将物体名字和依赖缓存并且禁止重复打包
static void LoaddependsPrefab(string Path)
{
if (!ContainAllFilrAB(Path))
{
//加载预制体的依赖
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(Path);
string[] allDepend = AssetDatabase.GetDependencies(Path);
List<string> AllDependPath = new List<string>();
for (int i = 0; i < allDepend.Length; i++)
{
if (!ContainAllFilrAB(allDepend[i]) && !allDepend[i].EndsWith(".cs"))
{
m_AllFileAB.Add(allDepend[i]);
AllDependPath.Add(allDepend[i]);
}
}
if (m_AllPrefabDir.ContainsKey(obj.name))
{
Debug.LogError("存在相同名字的预制体");
}
else
{
m_AllPrefabDir.Add(obj.name,AllDependPath);
}
}
}
//判断是否重复打包
static bool ContainAllFilrAB(string path)
{
for (int i = 0; i < m_AllFileAB.Count; i++)
{
if (path==m_AllFileAB[i]||path.Contains(m_AllFileAB[i]))
{
Debug.Log("m_AllFileAB[i]==" + m_AllFileAB[i]+ " path= "+path+"值"+true);
return true;
}
}
return false;
}
四.根据配置表进行贴标签
#region MyRegion
foreach (string name in m_AllFileDir.Keys)
{
SetABName(name, m_AllFileDir[name]);
}
foreach (string name in m_AllPrefabDir.Keys)
{
SetABName(name,m_AllPrefabDir[name]);
}
//给文件夹打包的物体添加标签
static void SetABName(string name,string path)
{
AssetImporter assetImporter = AssetImporter.GetAtPath(path);
if (assetImporter==null)
{
Debug.LogError("不存在此路径的文件");
}
else
{
assetImporter.assetBundleName = name;
}
}
//给物体的依赖打包添加标签
static void SetABName(string name,List<string>path)
{
for (int i = 0; i < path.Count; i++)
{
SetABName(name,path[i]);
}
}
#endregion
五.获取添加标签的物体路径和标签名字并打包
static void BunildAssetBundle()
{
string[] allBundle = AssetDatabase.GetAllAssetBundleNames();//获取所有有标签的名字(每个标签下有很多物体)
Dictionary<string, string> rePathDic = new Dictionary<string, string>();
for (int i = 0; i < allBundle.Length; i++)
{
Debug.Log(allBundle[i]);
string[] allBundlePath = AssetDatabase.GetAssetPathsFromAssetBundle(allBundle[i]);
for (int j = 0; j <allBundlePath.Length; j++)
{
if (!allBundlePath[j].EndsWith(".cs"))
{
rePathDic.Add(allBundlePath[j], allBundle[i]);
}
}
}
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.ChunkBasedCompression,
EditorUserBuildSettings.activeBuildTarget);
}