Esempio n. 1
0
 private int OnSortCallList(LoadAssetRequest x, LoadAssetRequest y)
 {
     return(x.LoadIndex.CompareTo(y.LoadIndex));
 }
Esempio n. 2
0
    IEnumerator OnLoadAsset(Type t, string abName)
    {
        //为了确保异步,强制添加一个返回
        yield return(null);

        List <LoadAssetRequest> list = null;

        AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName);

        if (bundleInfo == null)
        {
            yield return(StartCoroutine(OnLoadAssetBundle(abName, t)));

            bundleInfo = GetLoadedAssetBundle(abName);

            if (bundleInfo == null)
            {
                //加载资源失败了
                if (m_LoadRequests.TryGetValue(abName, out list))
                {
                    if (list != null)
                    {
                        int count = list.Count;
                        for (int i = 0; i < count; ++i)
                        {
                            LoadAssetRequest r = list[i];
                            if (r.sharpFunc != null)
                            {
                                //r.sharpFunc(null);
                                //r.sharpFunc = null;
                                r.Error   = true;
                                r.ObjList = null;
                                m_CallBackList.Add(list[i]);
                                //m_CallBackList.Sort(OnSortCallList);
                            }
                        }
                    }
                    m_LoadRequests.Remove(abName);
                }
                Debug.LogError("OnLoadAsset--->>>" + abName);
                yield break;
            }
        }

        //这个位置的异常处理可能有问题,没有合理通知逻辑层资源有问题。
        if (!m_LoadRequests.TryGetValue(abName, out list))
        {
            yield break;
        }
        else
        {
            if (list == null)
            {
                m_LoadRequests.Remove(abName);
                yield break;
            }
        }


        for (int i = 0; i < list.Count; i++)
        {
            string[] assetNames = list[i].assetNames;
            //List<UnityEngine.Object> result = new List<UnityEngine.Object>();
            ABObject result = PooledClassManager <ABObject> .CreateClass();

            AssetBundle ab = bundleInfo.m_AssetBundle;

            for (int j = 0; j < assetNames.Length; j++)
            {
                string assetPath = assetNames[j];

                AssetBundleRequest request = ab.LoadAssetAsync(assetPath, list[i].assetType);
                yield return(request);

                UnityEngine.Object obj = request.asset;
#if UNITY_EDITOR
                if (obj is GameObject)
                {
                    RecoveryShader((GameObject)obj);
                }
#endif
                result.m_UObjectList.Add(obj);
            }
            //Debug.Log("OnLoadAsset " + abName);
            bundleInfo.m_ReferencedCount++;
            //处理依赖
            string[] deps = null;
            if (m_Dependencies.TryGetValue(abName, out deps))
            {
                foreach (var dependency in deps)
                {
                    AssetBundleInfo depABInfo = GetLoadedAssetBundle(dependency);
                    depABInfo.m_ReferencedCount++;
                }
            }

            //将加载完成的id和abName绑定
            m_LoadedIdABName.Add(list[i].LoadIndex, abName);


            //这段代码不能去掉,因为我们的AssetBundleManifest依赖它来回调
            if (list[i].sharpFunc != null)
            {
                //list[i].sharpFunc(result);
                //list[i].sharpFunc = null;
                list[i].ObjList = result;
                m_CallBackList.Add(list[i]);
                //m_CallBackList.Sort(OnSortCallList);
            }
        }

        m_LoadRequests.Remove(abName);

        string[] Dependencies = null;
        if (m_Dependencies.TryGetValue(abName, out Dependencies))
        {
            foreach (var dependency in Dependencies)
            {
                //释放依赖AB
                //AssetBundleInfo depABInfo = GetLoadedAssetBundle(dependency);
                //depABInfo.m_ReferencedCount++;
                m_LoadRequests.Remove(dependency);
            }
        }
    }
Esempio n. 3
0
        public void LoadAsset <T>(string abName, string[] assetNames, Action <UObject[]> action = null, LuaFunction func = null) where T : UObject
        {
#if UNITY_EDITOR
            if (AppDef.DebugMode)
            {
                List <UObject> result = new List <UObject>();
                var            exts   = type2Ext(typeof(T));
                abName = abName.ToLower();
                AssetBundleInfo abi = GetLoadedAssetBundle(abName);
                if (abi == null)
                {
                    abi = AddLoadedAssetBundle(abName, null);
                }
                foreach (string assetName in assetNames)
                {
                    UObject obj;
                    if (abi.loadedObject.TryGetValue(assetName, out obj))
                    {
                        result.Add(obj);
                        continue;
                    }
                    string hasExt    = Path.GetExtension(assetName);
                    string nameNoExt = Path.GetFileNameWithoutExtension(assetName).ToLower();
                    string mergeName = abName.Replace(nameNoExt, "");
                    foreach (var path in gameAssetPaths)
                    {
                        string prefix = path;
                        {
                            prefix += mergeName;
                            if (!prefix.EndsWith("/"))
                            {
                                prefix += "/";
                            }
                        }
                        if (!string.IsNullOrEmpty(hasExt) || exts == null)
                        {
                            string ff = string.Format("{0}{1}", prefix, assetName);
                            obj = AssetDatabase.LoadAssetAtPath <T>(ff);
                            if (obj)
                            {
                                result.Add(obj);
                                abi.loadedObject[assetName] = obj;
                                continue;
                            }
                        }
                        else
                        {
                            foreach (var ext in exts)
                            {
                                string ff = string.Format("{0}{1}{2}", prefix, assetName, ext);
                                obj = AssetDatabase.LoadAssetAtPath <T>(ff);
                                if (obj)
                                {
                                    result.Add(obj);
                                    abi.loadedObject[assetName] = obj;
                                    continue;
                                }
                            }
                        }
                    }
                }

                if (result.Count == 0)
                {
                    Debug.LogWarning("Editor LoadAsset, no asset:  " + string.Join(",", assetNames));
                }

                if (action != null)
                {
                    action(result.ToArray());
                    action = null;
                }
                if (func != null)
                {
                    if (result.Count > 0)
                    {
                        func.Call((object)result.ToArray());
                    }
                    else
                    {
                        func.Call();
                    }
                    func.Dispose();
                    func = null;
                }
                return;
            }
#endif
            abName = GetRealAssetPath(abName);
            LoadAssetRequest request = new LoadAssetRequest();
            request.assetType  = typeof(T);
            request.assetNames = assetNames;
            request.luaFunc    = func;
            request.sharpFunc  = action;

            List <LoadAssetRequest> requests = null;
            if (!m_LoadRequests.TryGetValue(abName, out requests))
            {
                requests = new List <LoadAssetRequest>();
                requests.Add(request);
                m_LoadRequests.Add(abName, requests);
#if NET_4_6
                await OnLoadAsset <T>(abName);
#else
                StartCoroutine(OnLoadAsset <T>(abName));
#endif
            }
            else
            {
                requests.Add(request);
            }
        }