protected override void UpdateOptions(Loadzup.Options options)
        {
            var cancellationToken = new CancellationDisposable();

            _gameObject
            .OnDestroyAsObservable()
            .Take(1)
            .Subscribe(_ => cancellationToken.Dispose());

            options.CancellationToken = cancellationToken;
        }
Пример #2
0
        public static ICompletable BindAsCompletable(this IBinder This, string source, Image target, bool isOptional = false,
                                                     Loadzup.Options options = null,
                                                     bool keepVisible        = false, float?fadeDuration = null, bool isPriority = false)
        {
            if (source == null || target == null)
            {
                return(Completable.Empty());
            }

            return(This.BindAsCompletable(new Uri(source), target, isOptional, options, keepVisible, fadeDuration, isPriority));
        }
Пример #3
0
        public static ICompletable BindAsCompletable(this IBinder This, RawImage image, Uri uri, bool isOptional = false,
                                                     Loadzup.Options options = null, bool keepVisible = false, float?fadeDuration = null, bool isPriority = false)
        {
            if (image == null)
            {
                return(Completable.Empty());
            }

            if (uri == null)
            {
                if (isOptional)
                {
                    return(Completable.Empty());
                }

                return(Completable.Throw(
                           new BindException(
                               $"Cannot bind required image {image.gameObject.name} in view {This.View.GameObject.name} to null Uri.")));
            }

            if (fadeDuration != null)
            {
                image.color = Color.clear;
            }
            else
            {
                image.enabled = keepVisible;
            }

            return(This.Loader
                   .With(This.DefaultImageHttpCachePolicy)
                   .WithCancellationOnDestroy(This.View)
                   .WithQueuing(isPriority)
                   .Load <Texture2D>(uri, options)
                   .Catch <Texture2D, Exception>(ex => Observable
                                                 .Throw <Texture2D>(new BindException(
                                                                        $"Failed to load image {image.gameObject.name} in view {This.View.GetType().Name} from {uri}", ex)))
                   .Do(x =>
            {
                if (image.IsDestroyed())
                {
                    if (uri.Scheme == Scheme.Http || uri.Scheme == Scheme.Https ||
                        uri.Scheme == Scheme.StreamingAsset || uri.Scheme == Scheme.StreamingFile)
                    {
                        Object.Destroy(x);
                    }
                    return;
                }

                image.texture = x;
                image.enabled = true;

                if (fadeDuration != null)
                {
                    Observable.NextFrame()
                    .Then(_ => image.TweenColorTo(Color.white, fadeDuration.Value))
                    .SubscribeAndForget()
                    .AddTo(This);
                }

                if (uri.Scheme == Scheme.Http || uri.Scheme == Scheme.Https ||
                    uri.Scheme == Scheme.StreamingAsset || uri.Scheme == Scheme.StreamingFile)
                {
                    Disposable
                    .Create(() => Object.Destroy(x))
                    .AddTo(This);
                }
            })
                   .AutoDetach()
                   .AsCompletable());
        }
Пример #4
0
        public static ICompletable BindAsCompletable(this IBinder This, Uri source, Image target, bool isOptional = false,
                                                     Loadzup.Options options = null,
                                                     bool keepVisible        = false, float?fadeDuration = null, bool isPriority = false)
        {
            if (target == null)
            {
                return(Completable.Empty());
            }

            if (source == null)
            {
                if (isOptional)
                {
                    return(Completable.Empty());
                }

                return(Completable.Throw(
                           new BindException(
                               $"Cannot bind required image {target.gameObject.name} in view {This.View.GameObject.name} to null Uri.")));
            }

            if (fadeDuration != null)
            {
                target.color = Color.clear;
            }
            else
            {
                target.enabled = keepVisible;
            }

            return(This.Loader
                   .With(This.DefaultImageHttpCachePolicy)
                   .WithCancellationOnDestroy(This.View)
                   .WithQueuing(isPriority)
                   .Load <DisposableSprite>(source, options)
                   .Catch <DisposableSprite, Exception>(ex =>
                                                        Observable.Throw <DisposableSprite>(
                                                            new BindException(
                                                                $"Failed to load image {target.gameObject.name} in view {This.View.GetType().Name} from {source}", ex)))
                   .Do(x =>
            {
                if (target.IsDestroyed())
                {
                    if (source.Scheme == Scheme.Http || source.Scheme == Scheme.Https ||
                        source.Scheme == Scheme.StreamingAsset || source.Scheme == Scheme.StreamingFile)
                    {
                        x.Dispose();
                    }
                    return;
                }

                target.sprite = x.Sprite;
                target.enabled = true;

                if (fadeDuration != null)
                {
                    Observable.NextFrame()
                    .Then(_ => target.TweenColorTo(Color.white, fadeDuration.Value))
                    .SubscribeAndForget()
                    .AddTo(This);
                }

                if (source.Scheme == Scheme.Http || source.Scheme == Scheme.Https || source.Scheme == Scheme.StreamingAsset ||
                    source.Scheme == Scheme.StreamingFile)
                {
                    x.AddTo(This);
                }
            })
                   .AutoDetach()
                   .AsCompletable());
        }