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 Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD>㼰չʾ<EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>Bundle<6C>ж<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>չʾ<D5B9><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool loadSpotsFromBundle;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>Bundle<6C>ж<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>չʾ<D5B9><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool loadTipPointsFromBundle;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// չʾ<D5B9><CABE><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public Transform tranMainScene;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
if (loadSpotsFromBundle)
|
|||
|
|
{
|
|||
|
|
Invoke("LoadSpots", 0.5f);
|
|||
|
|
}
|
|||
|
|
if (loadTipPointsFromBundle)
|
|||
|
|
{
|
|||
|
|
Invoke("LoadTipPoints", 0.5f);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>չʾ<D5B9><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
void LoadSpots()
|
|||
|
|
{
|
|||
|
|
LoadBundle("Spots", "Spots" + (Application.isEditor ? "_win" : ""), OnBundleLoaded);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>չʾ<D5B9><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
void LoadTipPoints()
|
|||
|
|
{
|
|||
|
|
LoadBundle("TipPoints", "TipPoints" + (Application.isEditor ? "_win" : ""), OnBundleLoaded);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
void OnBundleLoaded()
|
|||
|
|
{
|
|||
|
|
CurrentBundleObject.transform.parent = tranMainScene;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>AssetBundle<6C><65>Դ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="prefabName">Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="bundleName">AssetBundle<6C><65>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="BundleLoaded"><3E><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></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>
|
|||
|
|
/// <20><>ǰ<EFBFBD><C7B0><EFBFBD>ص<EFBFBD>AssetBundle<6C><65>Դ
|
|||
|
|
/// </summary>
|
|||
|
|
static GameObject CurrentBundle;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>AssetBundle<6C><65>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static GameObject CurrentBundleObject;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public Slider sliderProgress;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
void ShowSlider()
|
|||
|
|
{
|
|||
|
|
if (sliderProgress.value != 1)
|
|||
|
|
{
|
|||
|
|
sliderProgress.gameObject.SetActive(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>AssetBundle(Ԥ<><D4A4><EFBFBD><EFBFBD>)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="abPath">AssetBundle<6C><65>Դ·<D4B4><C2B7></param>
|
|||
|
|
/// <param name="prefabName">Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="BundleLoaded"><3E><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
IEnumerator LoadAssetBundle_Prefab(string abPath, string prefabName, UnityAction BundleLoaded)
|
|||
|
|
{
|
|||
|
|
// Debug.Log(path);
|
|||
|
|
sliderProgress.value = 0;
|
|||
|
|
|
|||
|
|
Invoke("ShowSlider", 0.5f);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ AssetBundle <20>ļ<EFBFBD>
|
|||
|
|
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(abPath);
|
|||
|
|
www.SendWebRequest();
|
|||
|
|
|
|||
|
|
while (!www.isDone)
|
|||
|
|
{
|
|||
|
|
// print("<22><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD>Ϊ<EFBFBD><CEAA>" + 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);
|
|||
|
|
// <20><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
|||
|
|
CurrentBundle = bundle.LoadAsset<GameObject>(prefabName);
|
|||
|
|
// ʵ<><CAB5><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
|
|||
|
|
CurrentBundleObject = Instantiate(CurrentBundle);
|
|||
|
|
// Destroy(prefab, 5f);
|
|||
|
|
|
|||
|
|
// <20>ͷ<EFBFBD> AssetBundle <20><>Դ
|
|||
|
|
bundle.Unload(false);
|
|||
|
|
|
|||
|
|
if (BundleLoaded != null)
|
|||
|
|
{
|
|||
|
|
BundleLoaded();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>AssetBundle<6C><65>Դ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="prefabName">Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="bundleName">AssetBundle<6C><65>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="BundleLoaded"><3E><><EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></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>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>AssetBundle(<28><><EFBFBD><EFBFBD>)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="index"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></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);
|
|||
|
|
|
|||
|
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD>س<EFBFBD><D8B3><EFBFBD>
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|