Exemple #1
0
        public void TryCallback(AssetLoadResult ret = null, Action <AssetLoadHandler> callback = null)
        {
            //这里得区分是自己的回调,还是由子回调引起的回调由此来确定result的正确性
            result         = ret ?? result;
            m_callbackCall = m_callbackCall ?? callback;

            if (m_children != null && m_children.Count > 0)
            {
                if (m_callbackCount < m_children.Count)   //所有子回调
                {
                    m_callbackCount++;
                }
                else
                {
                    if (result == null)
                    {
                        return;
                    }
                }
            }

            m_callbackCall?.Invoke(this);
            Callback(result);

            if (m_parents != null && m_parents.Count > 0)
            {
                foreach (var parent in m_parents)
                {
                    parent?.TryCallback(ret, callback);
                }
            }
        }
Exemple #2
0
        protected override void OnLoadAssetSync(AssetLoadHandler handler)
        {
            Object obj    = AssetDatabase.LoadAssetAtPath <Object>(handler.path);
            var    result = new AssetLoadResult(obj, true);

            handler.status = AssetLoadStatus.LOAD_FINISHED;
            handler.Callback(result);
        }
Exemple #3
0
 //加载资源回调处理
 private void LoadAssetPrimitiveCallback(AssetLoadHandler handler, AssetLoadResult result)
 {
     //如果是无序回调,父回调可能执行不到,改用TryCallback
     result = result ?? AssetLoadResult.EMPTY_RESULT;
     if (handler.status == AssetLoadStatus.LOAD_LOADING)
     {
         handler.status = AssetLoadStatus.LOAD_FINISHED;
     }
     handler.Callback(result);
 }
Exemple #4
0
        protected override void OnLoadAssetSync(AssetLoadHandler handler)
        {
            string assetPath = handler.path;
            var    request   = Resources.Load(assetPath);

            handler.status = AssetLoadStatus.LOAD_FINISHED;
            var result = new AssetLoadResult(request, true);

            handler.Callback(result);
        }
Exemple #5
0
        protected override IEnumerator OnLoadAssetAsync(AssetLoadHandler handler)
        {
            string assetPath = handler.path;
            var    request   = Resources.LoadAsync(assetPath); //Resources加载路径不能有后缀

            yield return(request);

            handler.status = AssetLoadStatus.LOAD_FINISHED;
            var result = new AssetLoadResult(request.asset, request.isDone);

            handler.Callback(result);
        }
Exemple #6
0
        public void Reset()
        {
            id     = 0;
            status = 0;
            result = null;
            loader = null;
            path   = null;
            mode   = AssetLoadMode.Async;

            onCallback = null;

            m_parents?.Clear();
            m_children?.Clear();
            m_callbackCount = 0;
            m_callbackCall  = null;
        }
Exemple #7
0
        private void LoadAssetPrimitiveSync(AssetLoadHandler handler)
        {
            //同步的方法
            if (string.IsNullOrEmpty(handler.path))
            {
                LoadAssetPrimitiveCallback(handler, AssetLoadResult.EMPTY_RESULT);
                return;
            }

            string assetPath = handler.path;
            string assetName = null;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out assetName);

            Object asset  = null;
            bool   isDone = false;

            //是否已经在加载池中,如果是就直接返回,引用数加1
            var bundleObject = GetBundleObject(assetPath);

            if (bundleObject != null)
            {
                asset  = bundleObject.assetBundle;
                isDone = true;
            }
            else
            {
                //不支持同步的网络下载
                if (AssetPathUtil.IsUrl(assetPath))
                {
                    asset  = null;
                    isDone = false;
                }
                else
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_LOCAL_STAY_TIME;    //本地Handler超时时间
                    var assetObj = AssetBundle.LoadFromFile(assetPath);

                    asset  = assetObj;
                    isDone = true;
                }

                LoadAssetBundleCallback(handler, asset as AssetBundle);
            }

            ////////////////////////////////
            var assetBundle = asset as AssetBundle;

            if (assetBundle != null)
            {
                if (!string.IsNullOrEmpty(assetName))
                {
                    asset  = assetBundle.LoadAsset(assetName);
                    isDone = true;
                }
            }

            var result = new AssetLoadResult(asset, isDone);

            LoadAssetPrimitiveCallback(handler, result);
        }
Exemple #8
0
        //加载元操作
        private IEnumerator LoadAssetPrimitiveAsync(AssetLoadHandler handler)
        {
            if (string.IsNullOrEmpty(handler.path))
            {
                LoadAssetPrimitiveCallback(handler, AssetLoadResult.EMPTY_RESULT);
                yield break;
            }

            string assetPath = handler.path;
            string assetName = null;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out assetName);

            Object asset  = null;
            bool   isDone = false;

            //是否已经在加载池中,如果是就直接返回,引用数加1
            var bundleObject = GetBundleObject(assetPath);

            if (bundleObject != null)
            {
                asset  = bundleObject.assetBundle;
                isDone = true;
            }
            else
            {
                if (AssetPathUtil.IsUrl(assetPath))                                     //是否为网络路径
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_NETWORK_STAY_TIME; //网络Handler超时时间
                    var request = UnityWebRequestAssetBundle.GetAssetBundle(assetPath);
                    request.timeout = (int)handler.timeoutChecker.stayTime;
                    m_handlerWithRequestMap[handler.id] = new RequestObj()
                    {
                        id         = handler.id,
                        webRequest = request,
                    };
                    yield return(request.SendWebRequest());

                    asset  = DownloadHandlerAssetBundle.GetContent(request);
                    isDone = request.isDone;
                }
                else
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_LOCAL_STAY_TIME;    //本地Handler超时时间
                    var request = AssetBundle.LoadFromFileAsync(assetPath);
                    m_handlerWithRequestMap[handler.id] = new RequestObj()
                    {
                        id        = handler.id,
                        abRequest = request,
                    };
                    yield return(request);

                    asset  = request.assetBundle;
                    isDone = request.isDone;
                }

                //先把加载到的AssetBundle加入记录缓存,并且标记引用次数+1
                //不记录Bundle为空的项
                LoadAssetBundleCallback(handler, asset as AssetBundle);
            }

            ////////////////////////////////
            var assetBundle = asset as AssetBundle;

            if (assetBundle != null)
            {
                if (!string.IsNullOrEmpty(assetName))
                {
                    var loadRequest = assetBundle.LoadAssetAsync(assetName);
                    yield return(loadRequest);

                    asset  = loadRequest.asset;
                    isDone = loadRequest.isDone;
                }
            }

            var result = new AssetLoadResult(asset, isDone);

            LoadAssetPrimitiveCallback(handler, result);
        }
Exemple #9
0
 public void Callback(AssetLoadResult ret = null)
 {
     result = ret ?? result;
     onCallback?.Invoke(result);
 }