AssetBundle 初探(二) 读取管理

接着前面的文章,我们打好包后还得需要读取资源文件,那我们就得需要一个资源读取管理器,因为对AssetBundle不是特别懂,我先使用下面的代码进行资源在内存中的管理

public class AssetLoadManager : MonoBehaviour
{
	private static AssetLoadManager mInstance = null;
	public static AssetLoadManager GetInstance()
	{
		if (null == mInstance) {
			GameObject obj = new GameObject ();
			mInstance = obj.AddComponent<AssetLoadManager> ();
		}
		return mInstance;
	}
	
	private  AssetBundleManifest manifest = null;
	private Dictionary<string,AssetBundle> abDict = new Dictionary<string, AssetBundle>();
	public AssetBundle LoadAb(string abPath)
	{
		if (abDict.ContainsKey (abPath)) {
			return abDict [abPath];
		}
		if (null == manifest) {
			string assetBundlePath = AssetBundleConfig.AssetBundle_PATH + AssetBundleConfig.ASSET_FILENAME;
			AssetBundle manifestBundle = AssetBundle.LoadFromFile (assetBundlePath);
			if (null != manifestBundle) {
				manifest = (AssetBundleManifest)manifestBundle.LoadAsset ("AssetBundleManifest");
			}
		}
		if (null != manifest) {
			string[] cubeDepends = manifest.GetAllDependencies (abPath);
			for (int i = 0; i < cubeDepends.Length; ++i) {
				LoadAb (cubeDepends [i]);
			}
		}
		abDict [abPath] = AssetBundle.LoadFromFile (AssetBundleConfig.AssetBundle_PATH + abPath);
		return abDict [abPath];
	}
	public  Object LoadPrefab(string abName)
	{
		string abPath = abName + AssetBundleConfig.SUFFIX;
		int index = abName.LastIndexOf ("/");
		if (index == -1)
			index = abName.Length;
		string realPath = abName.Substring (0, index-1);
		string realName = abName.Substring (index + 1, abName.Length - index - 1);
		//Debug.Log ("Begin" +Time.unscaledTime);
		LoadAb (abPath);

		//Debug.Log ("End" +Time.unscaledTime);
		if (abDict.ContainsKey (abPath) && abDict [abPath] != null) {


			return abDict [abPath].LoadAsset (realName);
		}

		return null;
	}


}
还有很多不足的地方,我接下来会对AssetBundle是否需要缓存进行验证。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值