当前位置:网站首页>Unity AssetBundle load
Unity AssetBundle load
2022-06-22 07:36:00 【Liam666】
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Common.Event;
using UnityEngine;
using SandboxMap.Data;
using Object = UnityEngine.Object;
public enum AssetBundleType
{
WoldMap,
Construction,
UI,
NPC, //NPC Components of the model
NPCModel,// A number of AB A combination of components NPC Model ,3D Character model
FILMSCENE,// Movie scene
}
/// <summary>
/// Resource loader
/// </summary>
public class AssetBundleTool : MonoManagerBase<AssetBundleTool>
{
/// <summary>
/// Preload resources
/// </summary>
public void ReadyLoad()
{
ReadyLoadAsset = new List<string>();
_elapsedTime = 0;
StartCoroutine(ElapsedTimeEnumer());
AddReadyLoadAsset("labelpanel", AssetBundleType.UI);
AddReadyLoadAsset("commen_npc", AssetBundleType.NPC);
AddReadyLoadAsset("commen_construction", AssetBundleType.Construction);
AddReadyLoadAsset("StoryDialoguePanel", AssetBundleType.UI);
AddReadyLoadAsset("TaskReceiveWindow", AssetBundleType.UI);
AddReadyLoadAsset("TaskCommitWindow", AssetBundleType.UI);
ReadyLoadBuild();
Debug.Log("******AssetBundleReadyLoadAssetCount*******" + ReadyLoadAsset.Count);
}
/// <summary>
/// After resource update Preloading
/// </summary>
public void PreLoad()
{
LoadResourceAsync<Font>("Font", AssetBundleType.UI, "DENG");
}
private float _elapsedTime;
private List<string> ReadyLoadAsset;
private void AddReadyLoadAsset(string data,AssetBundleType type)
{
data = data.ToLower();
ReadyLoadAsset.Add(data);
LoadResource(data, type, null);
}
public void ReadyLoadEnd(string data)
{
if (ReadyLoadAsset == null) return;
if (ReadyLoadAsset.Contains(data))
{
ReadyLoadAsset.Remove(data);
}
else
{
Debug.Log(" Not in preloaded collection *********" + data);
}
if (ReadyLoadAsset.Count == 0)
{
ReadyLoadAsset = null;
GameManager.Instance.ToStartGame();
Debug.Log("******AssetBundleReadyLoadAssetEnd*******ElapsedTime:" + _elapsedTime);
}
}
private IEnumerator ElapsedTimeEnumer()
{
while (ReadyLoadAsset != null)
{
_elapsedTime += Time.deltaTime;
yield return 1;
}
}
/// <summary>
/// Preload building
/// </summary>
/// <param name="data"></param>
private void ReadyLoadBuild()
{
BuildingConfigData data = ConfigManager.Instance.load<global::BuildingConfigData>();
if (data == null || data.datas == null) return;
for (int i = 0; i < data.datas.Length; i++)
{
ConstructionConfig building = data.datas[i];
AddReadyLoadAsset(building.modeID + "_" + building.level , AssetBundleType.Construction);
}
}
/*
/// <summary>
/// Get resources synchronously
/// </summary>
/// <param name="data"></param>
/// <param name="type"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public GameObject GetReadyObject(string data, AssetBundleType type)
{
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type
+ "/" + data + ".data";
try
{
return AssetTool.GetAssetByName<GameObject>(ResourcesDic[resUrl],data) as GameObject;
}
catch (Exception e)
{
throw new Exception(data + ": No preloading " + e.Message);
}
}
*/
private EventDispatcher _eventManager;
public EventDispatcher EventManager => _eventManager;
private Queue<Dependence> _dependences;
/// <summary>
/// Relying on resources
/// </summary>
private List<string> DependencesList;
/// <summary>
/// Resource cache Dictionary
/// </summary>
public Dictionary<string, UnityEngine.Object[]> AssetsDic;
public bool CheckAssetsContains(string url)
{
if (AssetsDic!=null && AssetsDic.ContainsKey(url))
{
return true;
}
return false;
}
public void AddAssets2Dic(string key,UnityEngine.Object[] assets)
{
if (AssetsDic == null)
{
AssetsDic=new Dictionary<string, UnityEngine.Object[]>();
}
if (AssetsDic.ContainsKey(key))
{
Debug.LogError(" Already exists ResourcesDic*********" + key + "*********" + assets);
}
else
{
AssetsDic.Add(key, assets);
}
}
public Object[] GetAssets(string key)
{
if (AssetsDic != null && AssetsDic.ContainsKey(key))
{
return AssetsDic[key];
}
return null;
}
/// <summary>
/// Loading
/// </summary>
public List<string> InLoadResources;
private AssetBundleManifest _woldMapManifest; //
private AssetBundleManifest _constructionManifest;
private AssetBundleManifest _uiManifest;
private AssetBundleManifest _npcManifest;
private string PATHRESROOTDIR;
public void unload(string assetBundleName, AssetBundleType type = AssetBundleType.UI)
{
assetBundleName = assetBundleName.ToLower();
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + assetBundleName + ".data";
UnityEngine.Object[] ress = GetAssets(resUrl);
AssetsDic.Remove(resUrl);
Resources.UnloadUnusedAssets();
GC.Collect();
}
/// <summary>
/// load item Convenient and practical
/// </summary>
/// <param name="assetBundleName"></param>
/// <param name="type"></param>
/// <param name="assetName"></param>
/// <returns></returns>
public RectTransform Load(string assetBundleName, string assetName = "")
{
GameObject asset = LoadResource<GameObject>(assetBundleName, AssetBundleType.UI, assetName);
return Instantiate(asset).transform as RectTransform;
}
/// <summary>
/// Synchronous loading
/// </summary>
/// <param name="assetBundleName"></param>
/// <param name="type"></param>
/// <param name="back"></param>
/// <param name="assetName"></param>
/// <returns></returns>
public T LoadResource<T>(string assetBundleName, AssetBundleType type, string assetName = "")
where T : UnityEngine.Object
{
assetBundleName = assetBundleName.ToLower();
assetName = assetName.ToLower();
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + assetBundleName + ".data";
if (string.IsNullOrEmpty(assetName))
assetName = assetBundleName;
if (CheckAssetsContains(resUrl))
{
return AssetTool.GetAsset<T>(GetAssets(resUrl), assetName) as T;
}
else
{
AssetBundle ab = null;
ab = AssetBundle.LoadFromFile(resUrl);
if (ab == null)
{
Debug.LogError(" file does not exist ! fileName : " + resUrl);
return null;
}
UnityEngine.Object[] assets = ab.LoadAllAssets();
AddAssets2Dic(resUrl, assets);
T obj = AssetTool.GetAsset<T>(assets,assetName) as T;
ab.Unload(false);
return obj;
}
}
public void LoadSceneAsync(string assetBundleName, AssetBundleType type, Action action)
{
StartCoroutine(LoadSceneAsyncFun(assetBundleName, type, action));
}
private IEnumerator LoadSceneAsyncFun(string assetBundleName, AssetBundleType type, Action action)
{
assetBundleName = assetBundleName.ToLower();
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + assetBundleName + ".data";
if (CheckAssetsContains(resUrl) && action != null)
{
action();
}
else
{
AssetBundleCreateRequest abReq;
AssetBundle ab = null;
abReq = AssetBundle.LoadFromFileAsync(resUrl);
yield return abReq;
ab = abReq.assetBundle;
if (ab == null)
{
Debug.LogError(" file does not exist ! fileName : " + resUrl);
if (action != null) action();
}
if (ab.isStreamedSceneAssetBundle)
{
AddAssets2Dic(resUrl, new Object[] { ab });
if (action != null) action();
yield break;
}
if (action != null) action();
}
}
/// <summary>
/// Asynchronous loading of resources , return Asset resources
/// </summary>
/// <param name="assetBundleName"> Package name </param>
/// <param name="type"> The resource type </param>
/// <param name="assetName"> The resource name in the package </param>
/// <param name="action"> Callback function after loading </param>
/// <param name="save"> Whether to keep in memory assetBundle Mirror resources </param>
/// <typeparam name="T"></typeparam>
public void LoadResourceAsync<T>(string assetBundleName, AssetBundleType type, string assetName = "",
Action<T> action = null,bool save = true) where T : UnityEngine.Object
{
StartCoroutine(LoadResourceAsyncFun(assetBundleName, type, assetName, action, save));
}
private IEnumerator LoadResourceAsyncFun<T>(string assetBundleName, AssetBundleType type, string assetName,
Action<T> action,bool save) where T : UnityEngine.Object
{
assetBundleName = assetBundleName.ToLower();
assetName = assetName.ToLower();
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + assetBundleName + ".data";
string key = resUrl + assetName;
if (CheckAssetsContains(key) && action != null)
{
Object obj = AssetTool.GetAsset<T>(GetAssets(resUrl), assetBundleName);
if (obj is T)
{
action(obj as T);
}
else if (obj is AssetBundle)
{
action(null);
}
}
else
{
AssetBundleCreateRequest abReq;
AssetBundle ab = null;
abReq = AssetBundle.LoadFromFileAsync(resUrl);
yield return abReq;
ab = abReq.assetBundle;
if (ab == null)
{
Debug.LogError(" file does not exist ! fileName : " + resUrl);
if(action != null) action(null);
}
if (string.IsNullOrEmpty(assetName))
assetName = assetBundleName;
if(ab.isStreamedSceneAssetBundle)
{
AddAssets2Dic(resUrl, new Object[] { ab });
if (action != null) action(null);
yield break;
}
AssetBundleRequest assets = ab.LoadAllAssetsAsync();
yield return assets;
AddAssets2Dic(resUrl,assets.allAssets);
T obj = AssetTool.GetAsset<T>(assets.allAssets,assetName) as T;
if (!save)
{
ab.Unload(false);
}
if (action != null) action(obj);
}
}
/// <summary>
/// according to name Load a preset file , Set to parent A child object of -- asynchronous --
/// </summary>
public void LoadResource(string data, AssetBundleType type, Action<GameObject> back)
{
data = data.ToLower();
if (_eventManager == null)
{
_eventManager = new EventDispatcher();
_eventManager.AddEventListener(AssetBundleEventType.LoadEnd, LoadEvent);
_dependences = new Queue<Dependence>();
DependencesList = new List<string>();
InLoadResources = new List<string>();
}
StartCoroutine(Load(data, type, back));
}
private IEnumerator Load(string data, AssetBundleType type, Action<GameObject> back)
{
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + data + ".data";
while (InLoadResources.Contains(resUrl))
{
yield return 1;
}
if (CheckAssetsContains(resUrl))
{
_eventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
GameObject obj = AssetTool.GetAsset<GameObject>(GetAssets(resUrl), data) as GameObject;
back?.Invoke(obj);
yield break;
}
if (!File.Exists(resUrl))
{
Debug.LogWarning(" file does not exist : " + resUrl);
ReadyLoadEnd(data);
_eventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
back?.Invoke(null);
yield break;
}
InLoadResources.Add(resUrl);
AssetBundleManifest manifest = null;
switch (type)
{
case AssetBundleType.WoldMap:
if (_woldMapManifest != null) manifest = _woldMapManifest;
break;
case AssetBundleType.Construction:
if (_constructionManifest != null) manifest = _constructionManifest;
break;
case AssetBundleType.UI:
if (_uiManifest != null) manifest = _uiManifest;
break;
case AssetBundleType.NPC:
if (_npcManifest != null) manifest = _npcManifest;
break;
}
if (manifest == null)
{
string manifestPath =
Path.Combine(ClientInfo.DataPath + "/AssetBundleDatas/", type.ToString());
manifestPath = Path.Combine(manifestPath, type.ToString());
// if (!File.Exists(manifestPath))
// {
// _eventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
// back?.Invoke(null);
// }
AssetBundle asset = AssetBundle.LoadFromFile(manifestPath);
manifest = asset.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
switch (type)
{
case AssetBundleType.WoldMap:
_woldMapManifest = manifest;
break;
case AssetBundleType.Construction:
_constructionManifest = manifest;
break;
case AssetBundleType.UI:
_uiManifest = manifest;
break;
case AssetBundleType.NPC:
_npcManifest = manifest;
break;
}
asset.Unload(false);
}
Dependence dep = new Dependence(data, type.ToString(), manifest, back);
_dependences.Enqueue(dep);
if (_isFrist)
{
_isFrist = false;
_eventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
}
}
private bool _isFrist = true;
private void LoadEvent(GameEventArgs e)
{
if (_dependences.Count > 0)
{
_dependences.Dequeue().Init();
}
else
{
_isFrist = true;
}
}
public bool GetDepContain(string key)
{
if (!DependencesList.Contains(key))
{
DependencesList.Add(key);
return false;
}
return true;
}
public void LoadScene(string sceneName, Action action)
{
StartCoroutine(LoadSceneFun(sceneName, action));
}
private IEnumerator LoadSceneFun(string sceneName, Action action)
{
sceneName = sceneName.ToLower();
string resUrl = ClientInfo.DataPath + "/AssetBundleDatas/WorldMap/" + sceneName + ".data";
string key = resUrl;
if (CheckAssetsContains(key) && action != null)
{
action();
}
else
{
AssetBundleCreateRequest abReq;
AssetBundle ab = null;
abReq = AssetBundle.LoadFromFileAsync(resUrl);
yield return abReq;
/*
ab = abReq.assetBundle;
if (ab == null)
{
Debug.LogError(" file does not exist ! fileName : " + resUrl);
if(action != null) action();
}
AssetBundleRequest assets = SceneManager.LoadScene()
yield return assets;
AddAssets2Dic(resUrl,assets.allAssets);
ab.Unload(false);
*/
if (action != null) action();
}
}
}
public static class AssetBundleEventType
{
public const string LoadEnd = "LOADEND";
}
public class Dependence
{
private readonly string _data;
private readonly string _url;
private readonly string _type;
private readonly AssetBundleManifest _manifest;
private readonly Action<GameObject> _back;
public Dependence(string data, string type, AssetBundleManifest manifest, Action<GameObject> back)
{
_data = data;
_url = ClientInfo.DataPath + "/AssetBundleDatas/" + type + "/" + data + ".data";
_type = type;
_manifest = manifest;
_back = back;
}
public void Init()
{
AssetBundleTool.instance.StartCoroutine(LoadDependences());
}
/// <summary>
/// Load dependencies
/// </summary>
/// <returns></returns>
private IEnumerator LoadDependences()
{
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(_url);
yield return request;
if (string.Equals(_data, "commen_npc") || string.Equals(_data, "commen_construction"))
{
AssetBundleTool.instance.ReadyLoadEnd(_data);
AssetBundleTool.instance.EventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
yield break;
}
AssetBundle assetBundle = request.assetBundle;
if (assetBundle == null)
{
Debug.LogError("SceneProp assetsBundle is null! fileName : " + _url);
yield break;
}
AssetBundleRequest result = assetBundle.LoadAllAssetsAsync();
yield return result;
object temp = AssetTool.GetAsset<GameObject>(result.allAssets,_data);
UnityEngine.Object obj = temp as UnityEngine.Object;
_back?.Invoke(obj as GameObject);
AssetBundleTool.instance.ReadyLoadEnd(_data);
AssetBundleTool.instance.AddAssets2Dic(_url, result.allAssets);
AssetBundleTool.instance.InLoadResources.Remove(_url);
AssetBundleTool.instance.EventManager.DispatchEvent(new GameEventArgs(AssetBundleEventType.LoadEnd));
if(_type == "Construction" || _type == "NPC")
{
assetBundle.Unload(false);
}
}
}
public class AssetTool
{
public static UnityEngine.Object GetAsset<T>(UnityEngine.Object[] assets, string name)
{
UnityEngine.Object temp = null;
for (int i = 0; i < assets.Length; i++)
{
temp = assets[i];
if (temp is T)
{
string nameValue = temp.ToString();
string tempName = nameValue.Substring(0, nameValue.IndexOf(" ")).ToLower();
if (tempName == name)
{
//Debug.Log(tempName);
return temp;
}
}
}
return null;
}
public static T[] GetAssetAll<T>(UnityEngine.Object[] assets)
{
List<T> tempResult= new List<T>();
System.Object temp = null;
for (int i = 0; i < assets.Length; i++)
{
temp = assets[i];
if (temp is T)
{
//Debug.Log(temp);
tempResult.Add((T)temp);
}
}
return tempResult.ToArray();
}
}
边栏推荐
- Get through version 4.3 mind map
- Docker command, docker installation sqlserver2019, docker installation MySQL (continuous update)
- 微信小游戏(四)
- 5g NR PWS system
- Reasons and solutions for Taobao's baby's prompt "sales attribute is required and parameter format is wrong"
- [v4.3] the applet fails to receive the subscription message, and the coupon time on the product details page is displayed incorrectly
- Application and problem solving of robotframework
- 5、 Image component
- Canoe learning notes (3) graphic window introduction diagram
- Matlab suddenly fails to open. It disappears after running. There is no solution for the task manager
猜你喜欢

Crmeb mall order shipping function

Coursera self driving car Part4 motion planning finalproject principle and key code analysis

Bad slam resolution

DETR3D模型源码导读 & MMDetection3D构建流程
![[standard version 4.3] marketing activities (group bargaining second kill) error reporting under orders](/img/44/2e7789a97fa43d196ef770e8b3c4d4.jpg)
[standard version 4.3] marketing activities (group bargaining second kill) error reporting under orders

Taobao store backup one store upload to multiple stores precautions

How to import and upload a CSV generated by a third-party platform to a Taobao store

Matlab uses deep learning recurrent neural network RNN long-term and short-term memory LSTM to predict waveform time series data

Backup the method of uploading babies in Taobao stores to multiple stores

Wechat games (4)
随机推荐
How to batch copy babies by sales volume in Taoying
Notes on algebra 10.1: understanding of symmetric polynomials and derivation of cubic resolvents
Get through version - bargain activity
A training summary of Intranet penetration test
Open version - account information synchronization and unification
各大企业连连出现亏损,环保行业何去何从?
itertools 排列组合
Can Taobao batch copy babies synchronize the original baby inventory
Examples of Algebra: understanding of normal subgroups and quotient groups
Find and replace the spaces, Nan and special symbols contained in a column of data in the dataframe
MTK platform execution disable_ Interrupt signals masked during IRQ will be enabled when I execute enable_ Continue triggering after IRQ
Matlab uses deep learning recurrent neural network RNN long-term and short-term memory LSTM to predict waveform time series data
The pit on the school recruitment Road
Crmeb open source version, a free mall worth trying
微信小游戏(一)
Taobao store backup one store upload to multiple stores precautions
Téléchargement de toutes les versions de chromedriver
What are the functions of Taobao batch copy tool?
目标检测系列——开山之作RCNN原理详解
网站如何提高百度权重