unity的assetbundle加载
2017-12-04 17:42
726 查看
最近在写assetbundle的编辑器,顺便写了一下unity的资源管理器。
研究了一下王者农药的热更新机制,发现它的首页动画界面也可以热更新,但是这个时候才在启动进度条界面。
猜想了1下,他的资源加载路径是变化的。
优先加载缓存,发现缓存没得就加载包。
下次重新进入游戏,因为新数据已经down下来了,这个时候是直接加载缓存,就会发现初始动画改编了。
这里就需要动态设置加载路径。
说跑题了, 现先上传代码
/*************************************************************************************
**文 件 名:ResourceManager
**创建时间:2017/9/22 星期五 下午 1:55:46
**作 者:worthgod(qq: 243515320)
**工 号:
**说 明:
**版 本:V1.0.0
**修改时间:
**修 改 人:
*************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using WorthGodFramework;
using WorthGodFramework.Config;
using WorthGodFramework.Core;
using WorthGodFramework.Log;
namespace WorthGod
{
public class ResourceManager : SingletonMono<ResourceManager>
{
public Dictionary<string, AssetBundle> ObjectPool = new Dictionary<string, AssetBundle>();
void Awake()
{
DontDestroyOnLoad(this);
}
private string _baseDownloadingURL = "";
public string BaseDownloadingURL
{
get
{
if (string.IsNullOrEmpty(_baseDownloadingURL))
{
#if !UNITY_EDITOR
_baseDownloadingURL = Util.GetRelativePath()+ "WorthGodFramework/";
#else
_baseDownloadingURL = Util.DataPath + "WorthGodFramework/";
#endif
}
return _baseDownloadingURL;
}
set
{
_baseDownloadingURL = value;
}
}
// The base downloading url which is used to generate the full downloading url with the assetBundle names.
public void LoadAssetAsync<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T:UnityEngine.Object
{
if (ObjectPool.ContainsKey(abname))
{
var a = ObjectPool[abname].AllAssetNames();
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
if (func != null)
{
func(m_Request.asset as T);
}
}
else {
StartCoroutine(LoadingAb(abname, assetname, func));
}
}
IEnumerator LoadingAb<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T : UnityEngine.Object
{
var filePath = BaseDownloadingURL + abname;
if (!File.Exists(filePath))
{
filePath = Util.DataPath + "WorthGodFramework/" + abname;
}
var download = new WWW(filePath);
yield return download;
if (download.error != null)
{
//todo 弹出下载失败的提示 --> 输出
_Log.Error("下载初始动画界面,路径--》 " + BaseDownloadingURL + abname);
yield return null;
}
else
{
if (download.isDone)
{
var EncryptByte = download.bytes;
if (EncryptByte != null)
{
////反解密
var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);
AssetBundle bundle = AssetBundle.LoadFromMemory(BundleBytes);
ObjectPool.Add(abname, bundle);
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
if (func != null)
{
func(m_Request.asset as T);
}
}
}
}
download.Dispose();
download = null;
yield break;
}
public T LoadAsset<T>(string abname, string assetname) where T : UnityEngine.Object
{
if (ObjectPool.ContainsKey(abname))
{
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
return m_Request.asset as T;
}
else
{
var filePath = BaseDownloadingURL + abname;
if (!File.Exists(filePath))
{
filePath = Util.DataPath + "WorthGodFramework/" + abname;
}
Debug.LogWarning("$#@$#@$#@ " + filePath);
if (!File.Exists(filePath)) return null as T;
var EncryptByte = File.ReadAllBytes(filePath);
////反解密
var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);
var bundle = AssetBundle.LoadFromMemory(BundleBytes);
ObjectPool.Add(abname, bundle);
var m_Request = bundle.LoadAsset(assetname, typeof(T));
return m_Request as T;
}
}
public void UnloadAb(string abname) {
if (ObjectPool.ContainsKey(abname))
{
ObjectPool[abname].Unload(false);
ObjectPool.Remove(abname);
}
}
}
}
研究了一下王者农药的热更新机制,发现它的首页动画界面也可以热更新,但是这个时候才在启动进度条界面。
猜想了1下,他的资源加载路径是变化的。
优先加载缓存,发现缓存没得就加载包。
下次重新进入游戏,因为新数据已经down下来了,这个时候是直接加载缓存,就会发现初始动画改编了。
这里就需要动态设置加载路径。
说跑题了, 现先上传代码
/*************************************************************************************
**文 件 名:ResourceManager
**创建时间:2017/9/22 星期五 下午 1:55:46
**作 者:worthgod(qq: 243515320)
**工 号:
**说 明:
**版 本:V1.0.0
**修改时间:
**修 改 人:
*************************************************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using WorthGodFramework;
using WorthGodFramework.Config;
using WorthGodFramework.Core;
using WorthGodFramework.Log;
namespace WorthGod
{
public class ResourceManager : SingletonMono<ResourceManager>
{
public Dictionary<string, AssetBundle> ObjectPool = new Dictionary<string, AssetBundle>();
void Awake()
{
DontDestroyOnLoad(this);
}
private string _baseDownloadingURL = "";
public string BaseDownloadingURL
{
get
{
if (string.IsNullOrEmpty(_baseDownloadingURL))
{
#if !UNITY_EDITOR
_baseDownloadingURL = Util.GetRelativePath()+ "WorthGodFramework/";
#else
_baseDownloadingURL = Util.DataPath + "WorthGodFramework/";
#endif
}
return _baseDownloadingURL;
}
set
{
_baseDownloadingURL = value;
}
}
// The base downloading url which is used to generate the full downloading url with the assetBundle names.
public void LoadAssetAsync<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T:UnityEngine.Object
{
if (ObjectPool.ContainsKey(abname))
{
var a = ObjectPool[abname].AllAssetNames();
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
if (func != null)
{
func(m_Request.asset as T);
}
}
else {
StartCoroutine(LoadingAb(abname, assetname, func));
}
}
IEnumerator LoadingAb<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T : UnityEngine.Object
{
var filePath = BaseDownloadingURL + abname;
if (!File.Exists(filePath))
{
filePath = Util.DataPath + "WorthGodFramework/" + abname;
}
var download = new WWW(filePath);
yield return download;
if (download.error != null)
{
//todo 弹出下载失败的提示 --> 输出
_Log.Error("下载初始动画界面,路径--》 " + BaseDownloadingURL + abname);
yield return null;
}
else
{
if (download.isDone)
{
var EncryptByte = download.bytes;
if (EncryptByte != null)
{
////反解密
var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);
AssetBundle bundle = AssetBundle.LoadFromMemory(BundleBytes);
ObjectPool.Add(abname, bundle);
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
if (func != null)
{
func(m_Request.asset as T);
}
}
}
}
download.Dispose();
download = null;
yield break;
}
public T LoadAsset<T>(string abname, string assetname) where T : UnityEngine.Object
{
if (ObjectPool.ContainsKey(abname))
{
var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));
return m_Request.asset as T;
}
else
{
var filePath = BaseDownloadingURL + abname;
if (!File.Exists(filePath))
{
filePath = Util.DataPath + "WorthGodFramework/" + abname;
}
Debug.LogWarning("$#@$#@$#@ " + filePath);
if (!File.Exists(filePath)) return null as T;
var EncryptByte = File.ReadAllBytes(filePath);
////反解密
var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);
var bundle = AssetBundle.LoadFromMemory(BundleBytes);
ObjectPool.Add(abname, bundle);
var m_Request = bundle.LoadAsset(assetname, typeof(T));
return m_Request as T;
}
}
public void UnloadAb(string abname) {
if (ObjectPool.ContainsKey(abname))
{
ObjectPool[abname].Unload(false);
ObjectPool.Remove(abname);
}
}
}
}
相关文章推荐
- Unity的资源加载以及AssetBundle的一些坑
- Unity的动态加载AssetBundle资源笔记。
- Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
- Unity AssetBundle加载音频,无法播放音效并报错的坑
- Unity游戏开发使用Assetbundle加载场景的原理
- Unity 加载AssetBundle中AB包的几种方式
- unity5.4版本打包AssetBundle与加载(避免材质丢失网格丢失)
- Unity 关于AssetBundle(资源束)打包和加载(一)
- Unity5.3.5的AssetBundle加载
- Unity游戏开发使用Assetbundle加载场景的原理
- unity5.x assetbundle打包和加载
- Unity中如何从AssetBundle中加载和卸载对象
- Unity游戏开发使用Assetbundle加载场景的实战
- unity动态加载之AssetBundle应用
- Unity加载AssetBundle后shader丢失问题
- Unity游戏开发使用Assetbundle加载场景实战
- Unity教程之-Unity3d打包Assetbundle并加载(全面)
- Unity Assetbundle的加载方式的效率和内存占用
- 【unity】关于AssetBundle的加载与卸载
- Unity创建Assetbundle与加载