246 lines
6.4 KiB
C#
246 lines
6.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class BundleLoader : MonoBehaviour
|
|
{
|
|
public static BundleLoader Instance;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
#region 预加载碰撞点及展示点
|
|
/// <summary>
|
|
/// 是否从Bundle中动态加载展示点数据
|
|
/// </summary>
|
|
public bool loadSpotsFromBundle;
|
|
|
|
/// <summary>
|
|
/// 是否从Bundle中动态加载展示触发点数据
|
|
/// </summary>
|
|
public bool loadTipPointsFromBundle;
|
|
|
|
/// <summary>
|
|
/// 展示点数据父对象
|
|
/// </summary>
|
|
public Transform tranMainScene;
|
|
|
|
void Start()
|
|
{
|
|
if (loadSpotsFromBundle)
|
|
{
|
|
Invoke("LoadSpots", 0.5f);
|
|
}
|
|
if (loadTipPointsFromBundle)
|
|
{
|
|
Invoke("LoadTipPoints", 0.5f);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载展示点数据
|
|
/// </summary>
|
|
void LoadSpots()
|
|
{
|
|
LoadBundle("Spots", "Spots" + (Application.isEditor ? "_win" : ""), OnBundleLoaded);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载展示点数据
|
|
/// </summary>
|
|
void LoadTipPoints()
|
|
{
|
|
LoadBundle("TipPoints", "TipPoints" + (Application.isEditor ? "_win" : ""), OnBundleLoaded);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载结束设置父对象
|
|
/// </summary>
|
|
void OnBundleLoaded()
|
|
{
|
|
CurrentBundleObject.transform.parent = tranMainScene;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 动态加载AssetBundle资源
|
|
/// </summary>
|
|
/// <param name="prefabName">预设体名称</param>
|
|
/// <param name="bundleName">AssetBundle资源名称</param>
|
|
/// <param name="BundleLoaded">加载结束事件处理程序</param>
|
|
public void LoadBundle(string prefabName, string bundleName = null, UnityAction BundleLoaded = null)
|
|
{
|
|
if (bundleName == null)
|
|
{
|
|
bundleName = prefabName;
|
|
}
|
|
|
|
string path = CommonData.DataServer + "/Bundles/" + bundleName + ".assetbundle";
|
|
|
|
StartCoroutine(LoadAssetBundle_Prefab(path, prefabName, BundleLoaded));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前加载的AssetBundle资源
|
|
/// </summary>
|
|
static GameObject CurrentBundle;
|
|
|
|
/// <summary>
|
|
/// 当前加载AssetBundle资源所创建的对象
|
|
/// </summary>
|
|
public static GameObject CurrentBundleObject;
|
|
|
|
/// <summary>
|
|
/// 加载进度条
|
|
/// </summary>
|
|
public Slider sliderProgress;
|
|
|
|
/// <summary>
|
|
/// 显示进度条
|
|
/// </summary>
|
|
void ShowSlider()
|
|
{
|
|
if (sliderProgress.value != 1)
|
|
{
|
|
sliderProgress.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载AssetBundle(预设体)
|
|
/// </summary>
|
|
/// <param name="abPath">AssetBundle资源路径</param>
|
|
/// <param name="prefabName">预设体名称</param>
|
|
/// <param name="BundleLoaded">加载结束事件处理程序</param>
|
|
/// <returns></returns>
|
|
IEnumerator LoadAssetBundle_Prefab(string abPath, string prefabName, UnityAction BundleLoaded)
|
|
{
|
|
// Debug.Log(path);
|
|
sliderProgress.value = 0;
|
|
|
|
Invoke("ShowSlider", 0.5f);
|
|
|
|
// 从网络读取 AssetBundle 文件
|
|
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(abPath);
|
|
www.SendWebRequest();
|
|
|
|
while (!www.isDone)
|
|
{
|
|
// print("当前的下载进度为:" + www.downloadProgress);
|
|
sliderProgress.value = www.downloadProgress;
|
|
yield return 0;
|
|
}
|
|
|
|
sliderProgress.value = 1;
|
|
sliderProgress.gameObject.SetActive(false);
|
|
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
else
|
|
{
|
|
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
|
|
// 加载预制体资源
|
|
CurrentBundle = bundle.LoadAsset<GameObject>(prefabName);
|
|
// 实例化预制体
|
|
CurrentBundleObject = Instantiate(CurrentBundle);
|
|
// Destroy(prefab, 5f);
|
|
|
|
// 释放 AssetBundle 资源
|
|
bundle.Unload(false);
|
|
|
|
if (BundleLoaded != null)
|
|
{
|
|
BundleLoaded();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 动态加载AssetBundle资源
|
|
/// </summary>
|
|
/// <param name="prefabName">预设体名称</param>
|
|
/// <param name="bundleName">AssetBundle资源名称</param>
|
|
/// <param name="BundleLoaded">加载结束事件处理程序</param>
|
|
public void LoadScene(string abName, string sceneName, UnityAction BundleLoaded = null)
|
|
{
|
|
string abPath = CommonData.DataServer + "/Bundles/" + abName + ".unity3d";
|
|
|
|
StartCoroutine(LoadAssetBundle_Scene(abPath, sceneName, BundleLoaded));
|
|
}
|
|
|
|
AssetBundle oldSceneBundle;
|
|
AssetBundle currentSceneBundle;
|
|
string oldSceneName;
|
|
string currentSceneName;
|
|
/// <summary>
|
|
/// 加载AssetBundle(场景)
|
|
/// </summary>
|
|
/// <param name="index">场景索引</param>
|
|
/// <returns></returns>
|
|
public IEnumerator LoadAssetBundle_Scene(string abPath, string sceneName,
|
|
UnityAction BundleLoaded = null, LoadSceneMode mode = LoadSceneMode.Additive)
|
|
{
|
|
sliderProgress.value = 0;
|
|
ShowSlider();
|
|
|
|
// Caching.ClearCache();
|
|
|
|
UnityWebRequest download = UnityWebRequestAssetBundle.GetAssetBundle(abPath);
|
|
download.SendWebRequest();
|
|
while (!download.isDone)
|
|
{
|
|
sliderProgress.value = download.downloadProgress;
|
|
yield return null;
|
|
}
|
|
yield return download;
|
|
|
|
oldSceneBundle = currentSceneBundle;
|
|
currentSceneBundle = DownloadHandlerAssetBundle.GetContent(download);
|
|
|
|
// 异步加载场景
|
|
var sceneAsync = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
|
|
currentSceneName = sceneName;
|
|
while (!sceneAsync.isDone)
|
|
{
|
|
yield return null;
|
|
}
|
|
|
|
sliderProgress.value = 1;
|
|
sliderProgress.gameObject.SetActive(false);
|
|
|
|
if (BundleLoaded != null)
|
|
{
|
|
BundleLoaded();
|
|
}
|
|
|
|
oldSceneName = sceneName;
|
|
}
|
|
|
|
public void UnLoadOldScene()
|
|
{
|
|
if (oldSceneBundle != null && oldSceneName != null)
|
|
{
|
|
oldSceneBundle.Unload(true);
|
|
SceneManager.UnloadSceneAsync(oldSceneName);
|
|
}
|
|
}
|
|
|
|
public void UnLoadCurrentScene()
|
|
{
|
|
if (currentSceneName != null)
|
|
{
|
|
currentSceneBundle.Unload(true);
|
|
SceneManager.UnloadSceneAsync(currentSceneName);
|
|
}
|
|
}
|
|
|
|
}
|