Esempio n. 1
0
 /// <summary>
 /// 获取并绑定指定Asste资源(上层获取并绑定特定类型Asset的接口)
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="owner"></param>
 /// <param name="assetname"></param>
 /// <returns></returns>
 public override T getAsset <T>(UnityEngine.Object owner, string assetname)
 {
     if (owner != null)
     {
         var asset = loadAsset <T>(assetname);
         if (asset != null)
         {
             // 绑定owner对象,用于判定是否还有有效对象引用AB资源
             retainOwner(owner);
             updateLastUsedTime();
             return(asset);
         }
         else
         {
             ResourceLogger.logWar(string.Format("AB : {0}里不存在Asset : {1},获取Asset失败!", AssetBundleName, assetname));
             return(null);
         }
     }
     else
     {
         ResourceLogger.logErr(string.Format("不能绑定Asset到空对象上!加载AB:{0} Asset:{1}失败!", AssetBundleName, assetname));
         return(null);
     }
 }
    /// <summary>
    /// AB加载携程
    /// </summary>
    /// <returns></returns>
    private IEnumerator assetBundleLoadAsync()
    {
        while (true)
        {
            if (ABAsyncQueue.Count > 0)
            {
                CurrentLoadingAssetBundleLoader = ABAsyncQueue.Dequeue();
                //检查是否已经同步加载完成
                //如果异步加载AB时,同步请求来了,打断异步后续逻辑
                //LoadState == ResourceLoadState.None表明同步加载该资源已经完成,无需再异步返回
                if (CurrentLoadingAssetBundleLoader.LoadState == ResourceLoadState.None)
                {
                    ResourceLogger.logWar("有资源还未开始异步加载就被同步加载打断!");
                }
                else
                {
                    CurrentLoadingAssetBundleLoader.LoadState = ResourceLoadState.Loading;
                    var abname = CurrentLoadingAssetBundleLoader.AssetBundleName;
                    var abpath = AssetBundlePath.GetABLoadFullPath(abname);
                    AssetBundleCreateRequest abrequest = null;
#if UNITY_EDITOR
                    //因为资源不全,很多资源丢失,导致直接报错
                    //这里临时先在Editor模式下判定下文件是否存在,避免AssetBundle.LoadFromFileAsync()直接报错
                    if (System.IO.File.Exists(abpath))
                    {
                        Debug.Log(string.Format("开始异步加载AB : {0}!", CurrentLoadingAssetBundleLoader.AssetBundleName));
                        abrequest = AssetBundle.LoadFromFileAsync(abpath);
                    }
                    else
                    {
                        Debug.LogError(string.Format("AB : {0}文件不存在!", CurrentLoadingAssetBundleLoader.AssetBundleName));
                    }
#else
                    abrequest = AssetBundle.LoadFromFileAsync(abpath);
#endif
                    Debug.Log(string.Format("等待异步加载AB : {0}!", abname));
                    yield return(abrequest);

                    //如果异步加载AB时,同步请求来了,打断异步后续逻辑
                    //LoadState == ResourceLoadState.None表明同步加载该资源已经完成,无需再异步返回
                    if (CurrentLoadingAssetBundleLoader.LoadState == ResourceLoadState.None)
                    {
                        ResourceLogger.log(string.Format("资源 : {0}加载已完成,异步加载被打断!", abname));
                    }
                    else
                    {
                        var assetbundle = abrequest.assetBundle;
                        if (assetbundle == null)
                        {
                            ResourceLogger.logErr(string.Format("Failed to load AssetBundle : {0}!", CurrentLoadingAssetBundleLoader.AssetBundleName));
                        }
                        CurrentLoadingAssetBundleLoader.onSelfABLoadComplete(assetbundle);
                    }
                }
                CurrentLoadingAssetBundleLoader = null;
            }
            else
            {
                yield return(null);
            }
        }
    }
 public override T loadAsset <T>(string assetname)
 {
     if (mIsReady)
     {
         if (mLoadedAssetMap.ContainsKey(assetname))
         {
             return(mLoadedAssetMap[assetname] as T);
         }
         else
         {
             if (AssetsPath == null)
             {
                 ResourceLogger.logErr(string.Format("资源名 : {0}资源丢失,不存在!", AssetBundleName));
                 return(null);
             }
             else
             {
                 // 图集需要全部加载才能加载到指定Sprite
                 // 暂时通过加载AssetBundle里所有的资源来加载查找指定sprite
                 if (typeof(T) == typeof(Sprite))
                 {
                     loadAllAsset <T>();
                     if (mLoadedAssetMap.ContainsKey(assetname))
                     {
                         return(mLoadedAssetMap[assetname] as T);
                     }
                     else
                     {
                         ResourceLogger.logErr(string.Format("找不到资源名 : {0}里 Asset资源 : {1}!", AssetBundleName, assetname));
                         return(null);
                     }
                 }
                 else
                 {
                     var assetpathes = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(AssetBundleName, assetname);
                     if (assetpathes.Length == 1)
                     {
                         T asset = AssetDatabase.LoadAssetAtPath <T>(assetpathes[0]);
                         if (asset != null)
                         {
                             mLoadedAssetMap.Add(assetname, asset);
                             return(asset);
                         }
                         else
                         {
                             ResourceLogger.logErr(string.Format("找不到符合类型 : {0},资源名: {1},Asset : {2}资源!", typeof(T).GetType(), AssetBundleName, assetname));
                             return(null);
                         }
                     }
                     else if (assetpathes.Length > 1)
                     {
                         ResourceLogger.logWar(string.Format("资源名 : {0}里存在同名Asset : {1}资源!建议纠正Asset取名!", AssetBundleName, assetname));
                         // 有同名的情况下遍历选取第一个符合类型条件的资源
                         foreach (var assetpath in assetpathes)
                         {
                             T asset = AssetDatabase.LoadAssetAtPath <T>(assetpath);
                             if (asset != null)
                             {
                                 mLoadedAssetMap.Add(assetname, asset);
                                 return(asset);
                             }
                         }
                         ResourceLogger.logErr(string.Format("找不到符合类型 : {0},资源名: {1},Asset : {2}资源!", typeof(T).GetType(), AssetBundleName, assetname));
                         return(null);
                     }
                     else
                     {
                         ResourceLogger.logErr(string.Format("找不到资源名: {0},Asset : {1}资源!", AssetBundleName, assetname));
                         return(null);
                     }
                 }
             }
         }
     }
     else
     {
         ResourceLogger.logErr(string.Format("异常状态,AB资源:{0}未就绪就请求Asset资源:{1}", AssetBundleName, assetname));
         return(null);
     }
 }