?入口业务
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AbManager : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
void Start()
{
List<string> assets_bundle_keys = new List<string>();
if (assets_bundle_keys != null)
{
//填写需要加载的包
assets_bundle_keys.Add("prefabs");
assets_bundle_keys.Add("scenes");
}
AssetPack.instance.loadAssetBundle(assets_bundle_keys, onLoadedScenes);
}
void onLoadedScenes()
{
SceneManager.sceneLoaded += OnSceneLoaded;
//包加载完后跳转场景
SceneManager.LoadScene("threeD");
}
public void OnSceneLoaded(Scene scene, LoadSceneMode _mode)
{
SceneManager.sceneLoaded -= OnSceneLoaded;
AssetPack.instance.unLoadAssetBundle("threeD");
//这里可以为场景中添加UI资源
}
}
(ab包加载工具类)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.Networking;
public class AssetPack : MonoBehaviour
{
public static AssetPack instance;
protected Dictionary<string, object> m_bundles = new Dictionary<string, object>();
string _uri = Application.streamingAssetsPath;
public Text loadingText;
public Text progressText;
public int prograss;
private void Awake()
{
instance = this;
}
//加载指定的ab包
public void loadAssetBundle(List<string> _keys, UnityAction ua)
{
StartCoroutine(loadAb(_keys, ua));
}
//卸载指定的ab包
public void unLoadAssetBundle(string abName)
{
if (m_bundles.ContainsKey(abName))
{
AssetBundle ab = m_bundles[abName] as AssetBundle;
if (ab != null)
{
ab.Unload(false);
}
m_bundles.Remove(abName);
return;
}
}
IEnumerator loadAb(List<string> _keys, UnityAction ua)
{
foreach (var it in _keys)
{
print(_uri);
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(_uri + "/" + it);
//request.Send();
request.SendWebRequest();
while (!request.isDone)
{
loadingText.text = "正在加载\"" + it + "\"资源包";
prograss = (int)(request.downloadProgress * 100);
progressText.text = prograss + "";
yield return 1;
}
if (request.isDone)
{
prograss = (int)(request.downloadProgress * 100);
loadingText.text = "加载完成\"" + it + "\"资源包";
progressText.text = prograss + "";
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
m_bundles.Add(it, ab);
}
}
if (ua != null)
{
ua.Invoke();
}
}
//加载ab包中某个资源到对应的场景
public GameObject loadAssetToScene(string abName,string assetName)
{
GameObject obj=null;
if (m_bundles.ContainsKey(abName))
{
AssetBundle ab = m_bundles[abName] as AssetBundle;
if (ab != null)
{
obj = Instantiate(ab.LoadAsset<GameObject>(assetName));
}
}
return obj;
}
}
?第二个场景中实例化一些资源
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class threeD : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
AssetPack.instance.loadAssetToScene("prefabs", "fenshan");
}
// Update is called once per frame
void Update()
{
}
}
准备两个场景
入口场景
?下面是跳转的场景
最后打包后运行如下:
?备注:上面风扇? 发布webgl后 在浏览器上浏览是正常的
打ab包可以用unity自带的AssetBundleBrowser
首次用AssetBundleBrowser,需要在包管理器添加
FR:徐海涛(hunk Xu) QQ技术交流群:386476712
|