引擎版本更新 , ab打包加载方式也在发生变化, 之前的脚本还需要再改改才能继续用
这个没有太多功能 就是简单地打包和加载
废话不多说 直接上代码 先看打包 编辑器拓展脚本要放Editor 文件夹下
using UnityEngine;
using UnityEditor;
public class CreateAB : Editor {
/// <summary>
/// 将符合条件的全部分包打包(基本没什么卵用)
/// </summary>
[MenuItem("AB工具/单独打包")]
static void GreatBundle()
{
Object[] selects = Selection.objects;
foreach (Object item in selects)
{
Debug.Log(item);
BuildPipeline.BuildAssetBundles(Application.dataPath + "/", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
AssetDatabase.Refresh();//打包后刷新,不加这行代码的话要手动刷新才可以看得到打包后的Assetbundle包
}
}
/// <summary>
/// 将选定的多个游戏对象打包一个
/// </summary>
[MenuItem("AB工具/整体打包")]
static void GreatAllBundle()
{
AssetBundleBuild[] builds = new AssetBundleBuild[1];
Object[] selects = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
string[] TestAsset = new string[selects.Length];
for (int i = 0; i < selects.Length; i++)
{
TestAsset[i] = AssetDatabase.GetAssetPath(selects[i]);
Debug.Log(TestAsset[i]);
}
builds[0].assetNames = TestAsset;
builds[0].assetBundleName = selects[0].name;
BuildPipeline.BuildAssetBundles(Application.dataPath + "/", builds, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
AssetDatabase.Refresh();
}
}
然后是加载ab
using System.Collections;
using UnityEngine;
public class LoadAB : MonoBehaviour {
void Start ()
{
///两种选其一
StartCoroutine(Load(Application.dataPath + "/cube"));
//StartCoroutine(LoadA(Application.dataPath + "/cube"));
}
IEnumerator Load(string url)
{
WWW www = new WWW(url);
yield return www;
if (!string.IsNullOrEmpty(www.error)) print(www.error+url);
else
{
GameObject go = Instantiate(www.assetBundle.LoadAsset<GameObject>("cube"));
www.assetBundle.Unload(false);
}
}
IEnumerator LoadA(string path)
{
WWW bundle = WWW.LoadFromCacheOrDownload(path, 5);
yield return bundle;
yield return Instantiate(bundle.assetBundle.LoadAsset<GameObject>("cube"));
bundle.assetBundle.Unload(false);
}
}
两种加载的方式都差不多 第一个正常加载 第二种是从缓存中加载
有关缓存 想清理的话加一句
Caching.ClearCache();//清理全部缓存 想具体清理某一个 请参考点击这里看如何删除本地指定的缓存
第一个单独打包好像有点问题 先记录下 有时间再改