Exemplo n.º 1
0
        public void Init()
        {
#if UNITY_EDITOR && !AB_TEST
            EditorAssetLoadMgr.Init();
#else
            AssetBundleMgr.Init();

            AssetBundleMgr.LoadMainfest();
#endif
            ResourcesLoadMgr.Init();
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="assetName"></param>
        /// <returns></returns>
        public bool IsAssetExist(string assetName)
        {
#if UNITY_EDITOR && !AB_TEST
            return(EditorAssetLoadMgr.IsFileExist(assetName));
#else
            if (ResourcesLoadMgr.IsFileExist(assetName))
            {
                return(true);
            }
            return(AssetBundleMgr.IsFileExist(assetName));
#endif
        }
Exemplo n.º 3
0
        /// <summary>
        /// 异步加载
        /// </summary>
        /// <param name="assetName"></param>
        public void LoadAsync(string assetName, AssetsLoadCallback callFun)
        {
            assetName = assetName.ToLower();

            AssetObject assetObj = GetAssetObj(assetName);

            var status = GetAssetObjStatus(assetName);

            if (assetObj == null || status == AssetObjStatus.None)
            {
                assetObj = new AssetObject()
                {
                    AssetName = assetName,
                };

                PutAssetObInDic(AssetObjStatus.Loading, assetObj);

#if UNITY_EDITOR && !AB_TEST
                assetObj.Asset = EditorAssetLoadMgr.LoadSync(assetName);
#else
                if (AssetBundleMgr.IsFileExist(assetName))
                {
                    assetObj.IsAbLoad = true;

                    AssetBundleMgr.LoadAsync(assetName, (ab) =>
                    {
                        if (GetAssetObjStatus(assetName) == AssetObjStatus.Loading && assetObj.Request == null && assetObj.Asset == null)
                        {
                            assetObj.Request = ab.LoadAssetAsync(ab.GetAllAssetNames()[0]);
                        }
                    });
                }
                else if (ResourcesLoadMgr.IsFileExist(assetName))
                {
                    assetObj.IsAbLoad = false;

                    assetObj.Request = ResourcesLoadMgr.LoadAsync(assetName);
                }
#endif
            }

            assetObj.CallbackList.Add(callFun);

            if (status == AssetObjStatus.Loaded)
            {
                loadedAsyncList.Add(assetObj);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 执行卸载
        /// </summary>
        /// <param name="assetObject"></param>
        private void DoUnLoad(AssetObject assetObject)
        {
#if UNITY_EDITOR && !AB_TEST
            EditorAssetLoadMgr.Unload(assetObject.Asset);
#else
            if (assetObject.IsAbLoad)
            {
                AssetBundleMgr.UnLoadAssetBundleAsync(assetObject.AssetName);
            }
            else
            {
                ResourcesLoadMgr.Unload(assetObject.Asset);
            }
#endif

            assetObject.Asset = null;

            if (goInstanceIDList.ContainsKey(assetObject.InstanceID))
            {
                goInstanceIDList.Remove(assetObject.InstanceID);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 同步加载
        /// </summary>
        /// <param name="assetName"></param>
        public Object LoadSync(string assetName)
        {
            assetName = assetName.ToLower();

            if (!IsAssetExist(assetName))
            {
                return(null);
            }

            var assetObj = GetAssetObj(assetName);

            var status = GetAssetObjStatus(assetName);

            if (status == AssetObjStatus.Loaded)
            {
                assetObj.RefCount++;

                return(assetObj.Asset);
            }

            if (status == AssetObjStatus.Loading)
            {
                if (assetObj.Request == null)
                {
                    if (assetObj.Request is AssetBundleRequest)
                    {
                        assetObj.Asset = (assetObj.Request as AssetBundleRequest).asset;
                    }
                    else if (assetObj.Request is ResourceRequest)
                    {
                        assetObj.Asset = (assetObj.Request as ResourceRequest).asset;
                    }
                    else if (assetObj.Request is EditorAssetRequest)
                    {
                        assetObj.Asset = (assetObj.Request as EditorAssetRequest).asset;
                    }
                }
                else
                {
#if UNITY_EDITOR && !AB_TEST
                    assetObj.Asset = EditorAssetLoadMgr.LoadSync(assetName);
#else
                    if (assetObj.IsAbLoad)
                    {
                        var ab = AssetBundleMgr.LoadSync(assetName);

                        assetObj.Asset = ab.AssetBundle;

                        // 异步转同步,需要卸载异步的引用计数
                        //AssetBundleMgr.Unload(assetName);
                    }
                    else
                    {
                        assetObj.Asset = ResourcesLoadMgr.LoadSync(assetName);
                    }
#endif
                }
            }

            if (assetObj == null || status == AssetObjStatus.None)
            {
                assetObj = new AssetObject()
                {
                    AssetName = assetName,
                    RefCount  = 1
                };

#if UNITY_EDITOR && !AB_TEST
                assetObj.Asset = EditorAssetLoadMgr.LoadSync(assetName);
#else
                if (AssetBundleMgr.IsFileExist(assetName))
                {
                    assetObj.IsAbLoad = true;
                    var ab = AssetBundleMgr.LoadSync(assetName);
                    assetObj.Asset = ab.AssetBundle.LoadAsset(ab.AssetBundle.GetAllAssetNames()[0]);
                    // 异步转同步,需要卸载异步的引用计数
                    //AssetBundleMgr.Unload(assetName);
                }
                else
                {
                    assetObj.IsAbLoad = false;
                    assetObj.Asset    = ResourcesLoadMgr.LoadSync(assetName);
                }
#endif
            }

            if (assetObj.Asset == null)
            {
                PutAssetObInDic(AssetObjStatus.None, assetObj);

                Debug.LogError("asset is null -> " + assetObj.AssetName);

                return(null);
            }

            assetObj.InstanceID = assetObj.Asset.GetInstanceID();

            goInstanceIDList.Add(assetObj.InstanceID, assetObj);

            PutAssetObInDic(AssetObjStatus.Loaded, assetObj);

            assetObj.RefCount++;

            return(assetObj.Asset);
        }