Beispiel #1
0
        private AsyncOperationHandle <IList <IResourceLocation> > CheckAssetExist(string key, Action <IResourceLocation> callBack = null)
        {
            var assetHandle = Addressables.LoadResourceLocationsAsync(key);

            assetHandle.Completed += (inquiryData) =>
            {
                if (inquiryData.Result.Count == 0)
                {
                    Debug.LogError("key:{0} is not exist", key);
                    return;
                }

                if (inquiryData.Result.Count > 1)
                {
                    Debug.LogError("key:{0} - asset is more then one", key);
                    return;
                }

                if (!checkPool.ContainsKey(key))
                {
                    checkPool.Add(key, assetHandle.Result[0]);
                }
                callBack?.Invoke(inquiryData.Result[0]);
            };
            return(assetHandle);
        }
Beispiel #2
0
        public AsyncResult LoadAssetAsync(IList <string> key, Action <UnityEngine.Object> callBack = null)
        {
            var result = new AsyncResult();

            if (!checkPool.ContainsKey(key[0]))
            {
                CheckAssetExist(key, (ires) =>
                {
                    AsyncOperationHandle <UnityEngine.Object> h = Addressables.LoadAssetAsync <UnityEngine.Object>(ires);
                    h.Completed += (obj) =>
                    {
                        result.IsDone = true;
                        result.Result = obj.Result;
                        assetPool.Add(key[0], h);
                        callBack?.Invoke(obj.Result);
                    };
                });
            }
            else
            {
                Debug.LogError("The asset {0} is exist", key[0]);
                result.IsDone = true;
                result.Result = checkPool[key[0]];
            }
            return(result);
        }
Beispiel #3
0
 public void UnAsset(string key)
 {
     if (assetPool.ContainsKey(key))
     {
         var asset = assetPool[key];
         var check = checkPool[key];
         Addressables.Release(asset);
         assetPool.Remove(key);
     }
     else
     {
         Debug.LogError("asset {0} is not exist", key);
     }
 }
Beispiel #4
0
        public T Instantiate <T>(string key) where T : UnityEngine.Object
        {
            T result = null;

            if (assetPool.ContainsKey(key))
            {
                var asset = assetPool[key].Result as T;
                if (asset != null)
                {
                    result = GameObject.Instantiate <T>(asset);
                }
                else
                {
                    Debug.LogError("Asset {0} can not convert to {1}", key, typeof(T));
                }
            }
            else
            {
                Debug.LogError("Asset: {0} is not Loaded", key);
            }
            return(result);
        }
Beispiel #5
0
        public void CheckAssetExist(IList <string> keys, Action <IResourceLocation> callBack)
        {
            var handler = Addressables.LoadResourceLocationsAsync(keys as IList <object>, Addressables.MergeMode.Intersection);

            handler.Completed += (inquiryData) =>
            {
                if (inquiryData.Result.Count == 0)
                {
                    Debug.LogError("keys is not exist:{0}", ArrayUtil.ToString(keys));
                    callBack?.Invoke(null);
                    return;
                }

                if (inquiryData.Result.Count > 1)
                {
                    Debug.LogError("key asset is more then one:{0}", ArrayUtil.ToString(keys));
                }

                checkPool.Add(keys[0], handler.Result[0]);
                callBack?.Invoke(inquiryData.Result[0]);
            };
        }
Beispiel #6
0
        public AsyncResult LoadAssetAsync(string key, Action <UnityEngine.Object> callBack = null)
        {
            var result = new AsyncResult();

            if (!checkPool.ContainsKey(key))
            {
                CheckAssetExist(key, (ires) =>
                {
                    AsyncOperationHandle <UnityEngine.Object> h = Addressables.LoadAssetAsync <UnityEngine.Object>(ires);
                    h.Completed += (obj) =>
                    {
                        result.IsDone = true;
                        result.Result = obj.Result;
                        callBack?.Invoke(obj.Result);
                        if (obj.Result != null)
                        {
                            if (!assetPool.ContainsKey(key))
                            {
                                assetPool.Add(key, h);
                            }
                        }
                        else
                        {
                            Addressables.Release(h);
                            Debug.Log("The asset {0} is not exist", key);
                        }
                    };
                });
            }
            else
            {
                Debug.Log("The asset {0} is exist", key);
                result.IsDone = true;
                result.Result = checkPool[key];
            }
            return(result);
        }