public static IEnumerator LoadAssetCoroutine <T>(AssetReferenceT <T> reference, Action <string, T> onSucceeded = null, Action <string> onFailed = null) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                onFailed?.Invoke(key);
            }
            else
            {
                if (!_assets.ContainsKey(key))
                {
                    var operation = reference.LoadAssetAsync <T>();
                    yield return(operation);

                    OnLoadAssetCompleted(operation, key, true, onSucceeded, onFailed);
                }
                else if (_assets[key] is T asset)
                {
                    onSucceeded?.Invoke(key, asset);
                }
                else
                {
                    if (!SuppressWarningLogs)
                    {
                        Debug.LogWarning(Exceptions.AssetReferenceNotInstanceOf <T>(key));
                    }

                    onFailed?.Invoke(key);
                }
            }
        }
        public static void LoadAsset <T>(AssetReferenceT <T> reference, Action <string, T> onSucceeded,
                                         Action <string> onFailed = null) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                onFailed?.Invoke(key);
                return;
            }

            if (!_assets.ContainsKey(key))
            {
                var operation = reference.LoadAssetAsync <T>();
                operation.Completed += handle => OnLoadAssetCompleted(handle, key, onSucceeded, onFailed);
                return;
            }

            if (_assets[key] is T asset)
            {
                onSucceeded?.Invoke(key, asset);
            }
            else
            {
                Debug.LogWarning($"The asset with key={key} is not an instance of {typeof(T)}.");
                onFailed?.Invoke(key);
            }
        }
        public static IEnumerator LoadAssetCoroutine <T>(AssetReferenceT <T> reference, Action <string, T> onSucceeded = null,
                                                         Action <string> onFailed = null) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                onFailed?.Invoke(key);
            }
            else
            {
                if (!_assets.ContainsKey(key))
                {
                    var operation = reference.LoadAssetAsync <T>();
                    yield return(operation);

                    OnLoadAssetCompleted(operation, key, onSucceeded, onFailed);
                }
                else if (_assets[key] is T asset)
                {
                    onSucceeded?.Invoke(key, asset);
                }
                else
                {
                    Debug.LogWarning($"The asset with key={key} is not an instance of {typeof(T)}.");
                    onFailed?.Invoke(key);
                }
            }
        }
        public static void LoadAsset <T>(AssetReferenceT <T> reference) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                if (ExceptionHandle == ExceptionHandleType.Throw)
                {
                    throw Exceptions.InvalidReference;
                }

                if (ExceptionHandle == ExceptionHandleType.Log)
                {
                    Debug.LogException(new InvalidKeyException(key));
                }

                return;
            }

            if (_assets.ContainsKey(key))
            {
                if (!(_assets[key] is T))
                {
                    if (!SuppressWarningLogs)
                    {
                        Debug.LogWarning(Exceptions.AssetReferenceNotInstanceOf <T>(key));
                    }
                }

                return;
            }

            try
            {
                var operation = reference.LoadAssetAsync <T>();
                operation.Completed += handle => OnLoadAssetCompleted(handle, key, true);
            }
            catch (Exception ex)
            {
                if (ExceptionHandle == ExceptionHandleType.Throw)
                {
                    throw ex;
                }

                if (ExceptionHandle == ExceptionHandleType.Log)
                {
                    Debug.LogException(ex);
                }
            }
        }
Example #5
0
        public static void LoadAsset <T>(AssetReferenceT <T> reference, Action <string, T> onSucceeded,
                                         Action <string> onFailed = null) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                onFailed?.Invoke(key);
                return;
            }

            if (_assets.ContainsKey(key))
            {
                if (_assets[key] is T asset)
                {
                    onSucceeded?.Invoke(key, asset);
                    return;
                }


                if (!SuppressWarningLogs)
                {
                    Debug.LogWarning(Exceptions.AssetReferenceNotInstanceOf <T>(key));
                }

                onFailed?.Invoke(key);
                return;
            }

            try
            {
                var operation = reference.LoadAssetAsync <T>();
                operation.Completed += handle => OnLoadAssetCompleted(handle, key, true, onSucceeded, onFailed);
            }
            catch (Exception ex)
            {
                if (ExceptionHandle == ExceptionHandleType.Throw)
                {
                    throw ex;
                }

                if (ExceptionHandle == ExceptionHandleType.Log)
                {
                    Debug.LogException(ex);
                }
            }
        }
        public static void LoadAsset <T>(AssetReferenceT <T> reference) where T : Object
        {
            if (!GuardKey(reference, out var key))
            {
                return;
            }

            if (!_assets.ContainsKey(key))
            {
                var operation = reference.LoadAssetAsync <T>();
                operation.Completed += handle => OnLoadAssetCompleted(handle, key);
                return;
            }

            if (!(_assets[key] is T))
            {
                Debug.LogWarning($"The asset with key={key} is not an instance of {typeof(T)}.");
            }
        }