示例#1
0
        /// <summary>
        /// 加载文件夹下的所有资源
        /// </summary>
        /// <param name="folderUrl"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List <PackAsset> getAllAssetsByUrlSync <T> (string folderUrl) where T : Object
        {
            List <PackAsset> nativeAssets = this.findNativeAssets(folderUrl);

            if (nativeAssets != null)
            {
                return(nativeAssets);
            }

            T[] targetAssets = Resources.LoadAll <T> (folderUrl);
            nativeAssets = new List <PackAsset> ();
            bool isContainSplash = folderUrl[folderUrl.Length - 1] == '/';

            if (!isContainSplash)
            {
                folderUrl = folderUrl + "/";
            }
            foreach (T targetAsset in targetAssets)
            {
                string    assetUrl  = folderUrl + targetAsset.name;
                PackAsset packAsset = new PackAsset(assetUrl, targetAsset);
                nativeAssets.Add(packAsset);
                assetPool.Add(assetUrl, packAsset);
            }
            floderAssetPool.Add(folderUrl, nativeAssets);
            return(nativeAssets);
        }
示例#2
0
 private T findNativeAsset <T> (string assetUrl) where T : Object
 {
     if (assetPool.ContainsKey(assetUrl))
     {
         PackAsset packAsset = assetPool[assetUrl];
         packAsset.addRef();
         return(packAsset.targetAsset as T);
     }
     return(null);
 }
示例#3
0
        /* 同步加载目标AB包下的对应资源 */
        private T loadTargetBundleAssetSync <T> (AssetBundle targetBundle, string assetName) where T : Object
        {
            T         nativeAsset = targetBundle.LoadAsset <T> (assetName);
            string    assetUrl    = targetBundle.name + ":" + assetName;
            PackAsset packAsset   = new PackAsset(assetUrl, nativeAsset);
            string    nativeUrl   = targetBundle.name + "/" + assetName;

            this.assetPool.Add(nativeUrl, packAsset);
            return(nativeAsset as T);
        }
示例#4
0
 private T findNativeAsset <T> (string assetUrl) where T : Object
 {
     if (assetPool.ContainsKey(assetUrl))
     {
         // FIXME: 拿到资源不一定使用
         PackAsset packAsset = assetPool[assetUrl];
         return(packAsset.targetAsset as T);
     }
     return(null);
 }
示例#5
0
        /*同步加载目标AB包下的所有资源*/
        private List <PackAsset> loadTaregetBundleAllAssetSync <T> (AssetBundle targetBundle) where T : Object
        {
            T[] targetAssetArray             = targetBundle.LoadAllAssets <T> ();
            List <PackAsset> nativeAssetList = new List <PackAsset> ();

            foreach (T targetAsset in targetAssetArray)
            {
                string    assetUrl  = targetBundle.name + ":" + targetAsset.name;
                PackAsset packAsset = new PackAsset(assetUrl, targetAsset);
                nativeAssetList.Add(packAsset);
                string nativeUrl = targetBundle.name + "/" + targetAsset.name;
                assetPool.Add(nativeUrl, packAsset);
            }
            floderAssetPool.Add(targetBundle.name, nativeAssetList);
            return(nativeAssetList);
        }
示例#6
0
        /// <summary>
        /// 获取指定资源
        /// </summary>
        /// <param name="assetUrl"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T getAssetByUrlSync <T> (string assetUrl) where T : Object
        {
            T nativeAsset = this.findNativeAsset <T> (assetUrl);

            if (nativeAsset != null)
            {
                return(nativeAsset);
            }

            T targetAsset = null;

            targetAsset = Resources.Load <T> (assetUrl);
            PackAsset packageAsset = new PackAsset(targetAsset);

            assetPool.Add(assetUrl, packageAsset);
            return(targetAsset);
        }
示例#7
0
        /// <summary>
        /// 尝试释放资源并返回释放结果
        /// </summary>
        /// <param name="assetUrl"></param>
        /// <returns></returns>
        public bool tryReleaseAsset(string assetUrl)
        {
            if (!this.assetPool.ContainsKey(assetUrl))
            {
                Debug.LogWarning("can not release not exist asset");
                return(false);
            }

            PackAsset packAsset     = this.assetPool[assetUrl];
            bool      releaseResult = packAsset.releaseAsset();

            if (releaseResult)
            {
                this.assetPool.Remove(assetUrl);
            }
            return(releaseResult);
        }
示例#8
0
        public void getAssetByUrlAsync <T> (string assetUrl, Action <T> callback) where T : Object
        {
            T nativeAsset = this.findNativeAsset <T> (assetUrl);

            if (nativeAsset != null)
            {
                callback(nativeAsset);
                return;
            }

            ResourceRequest request = Resources.LoadAsync <T> (assetUrl);

            request.completed += operation => {
                PackAsset packageAsset = new PackAsset(request.asset);
                assetPool.Add(assetUrl, packageAsset);
                callback(request.asset as T);
            };
        }
示例#9
0
        public async Task <T> getAssetByUrlAsyncOb <T> (string assetUrl) where T : Object
        {
            T nativeAsset = this.findNativeAsset <T> (assetUrl);

            if (nativeAsset != null)
            {
                return(nativeAsset);
            }

            T targetAsset = null;

            targetAsset = await Task.Run(() => {
                ResourceRequest request = Resources.LoadAsync <T> (assetUrl);
                PackAsset packageAsset  = new PackAsset(request.asset);
                assetPool.Add(assetUrl, packageAsset);
                return(request.asset as T);
            });

            return(targetAsset);
        }
示例#10
0
        /* 异步加载目标AB下的对应资源 */
        private Promise <T> loadTargetBundleAssetAsync <T> (AssetBundle targetBundle, string assetName) where T : Object
        {
            AssetBundleRequest assetBundleRequest = targetBundle.LoadAssetAsync <T> (assetName);

            return(new Promise <T> (
                       (Action <T> resolve, Action <Exception> reject) => {
                assetBundleRequest.completed += bundleOperation => {
                    T nativeAsset = assetBundleRequest.asset as T;
                    string assetUrl = targetBundle.name + ":" + assetName;
                    PackAsset packAsset = new PackAsset(assetUrl, nativeAsset);
                    string nativeUrl = targetBundle.name + "/" + assetName;
                    if (this.assetPool.ContainsKey(nativeUrl))
                    {
                        this.assetPool.Remove(nativeUrl);
                    }
                    this.assetPool.Add(nativeUrl, packAsset);
                    resolve(nativeAsset);
                };
            }
                       ));
        }
示例#11
0
        /*异步加载目标AB下的所有资源*/
        private Promise <List <PackAsset> > loadTargetBundleAllAssetAsync <T> (AssetBundle targetBundle) where T : Object
        {
            AssetBundleRequest assetBundleRequest = targetBundle.LoadAllAssetsAsync <T> ();

            return(new Promise <List <PackAsset> > (
                       (Action <List <PackAsset> > resolve, Action <Exception> reject) => {
                assetBundleRequest.completed += bundleOperation => {
                    Object[] allAssets = assetBundleRequest.allAssets;
                    List <PackAsset> nativeAssetList = new List <PackAsset> ();
                    foreach (Object targetAsset in allAssets)
                    {
                        string assetUrl = targetBundle.name + ":" + targetAsset.name;
                        PackAsset packAsset = new PackAsset(assetUrl, targetAsset);
                        nativeAssetList.Add(packAsset);
                        string nativeUrl = targetBundle.name + "/" + targetAsset.name;
                        assetPool.Add(nativeUrl, packAsset);
                    }
                    floderAssetPool.Add(targetBundle.name, nativeAssetList);
                    resolve(nativeAssetList);
                };
            }
                       ));
        }