using UnityEngine; using System.Collections.Generic; using CommonUnity3D.MFUnity.LoadUtil; using System; namespace CommonAIClient.Unity.Utils { /// <summary> /// 载入一个资源 /// </summary> public class AssetLoader { private string mAssetBundleName; private string mAssetName; private Action<bool, UnityEngine.Object, MFUnityAssetBundle> mCallback; private bool mASyncLoadAB; private bool mASyncLoadAsset; //工具 private List<string> mDeps; private int mDepsLoadIndex; /// <summary> /// /// </summary> /// <param name="assetBundleName"></param> /// <param name="assetName"></param> /// <param name="callback arg1 是否加载成功, arg2 资源, arg3 所属AB"></param> /// <param name="bASyncLoadAB"></param> /// <param name="bASyncLoadAsset"></param> public AssetLoader(string assetBundleName, string assetName, Action<bool, UnityEngine.Object, MFUnityAssetBundle> callback, bool bASyncLoadAB = true, bool bASyncLoadAsset = true) { if (callback == null) { Debug.LogError("callback == null"); return; } if (string.IsNullOrEmpty(assetBundleName) || string.IsNullOrEmpty(assetName)) { Debug.LogError("string.IsNullOrEmpty(assetBundleName) || string.IsNullOrEmpty(assetName)"); callback(false, null, null); return; } mAssetBundleName = assetBundleName; mAssetName = assetName; mCallback = callback; mASyncLoadAB = bASyncLoadAB; mASyncLoadAsset = bASyncLoadAsset; mDeps = MFUnityAssetBundleManager.GetInstance().GetDepList(mAssetBundleName); LoadDeps(mDepsLoadIndex); } public void ForeachDepFile(System.Action<string> action) { foreach (var elem in mDeps) { action(elem); } } private void DepsCallback(string name, UnityEngine.Object o, object userdata, bool isLoadOK) { LoadDeps(++mDepsLoadIndex); } void LoadDeps(int index) { //加载完毕 if (mDeps.Count == mDepsLoadIndex) { MFUnityAssetBundleManager.GetInstance().GetAssetBundle(mAssetBundleName , (ab) => { if (mCallback != null) { if (ab == null) { Debug.LogError("AB " + mAssetBundleName + " NOT EXIST"); mCallback(false, null, null); return; } ab.GetAsset(mAssetName, mASyncLoadAsset, (name_, res_, userdata_, succ_) => { if (mCallback != null) { mCallback(succ_, res_, ab); } }, null); } }, mASyncLoadAB); return; } MFUnityAssetBundleManager.GetInstance().LoadDep(mDeps[mDepsLoadIndex], mASyncLoadAB, mASyncLoadAsset, DepsCallback, null, mAssetBundleName); } } }