diff --git a/Assets/Scripts/Connector/Event/SimpleAnimationEvent.cs b/Assets/Scripts/Connector/Event/SimpleAnimationEvent.cs index 3485ea2..f405853 100644 --- a/Assets/Scripts/Connector/Event/SimpleAnimationEvent.cs +++ b/Assets/Scripts/Connector/Event/SimpleAnimationEvent.cs @@ -66,16 +66,27 @@ private AnimationClip AnimationClip private SimpleAnimationCollector SimpleAnimationCollector => simpleAnimationCollector; private AnimationClipCollector AnimationClipCollector => animationClipCollector; - private ISubject<(SimpleAnimationEventType eventType, AnimationClip animationClip)> CurrentStateSubject { get; } = new Subject<(SimpleAnimationEventType, AnimationClip)>(); + private IReactiveProperty<(SimpleAnimationEventType eventType, AnimationClip animationClip)> CurrentStateProperty { get; } = new ReactiveProperty<(SimpleAnimationEventType eventType, AnimationClip animationClip)>(); private IDictionary PlayingStatuses { get; } = new Dictionary(); + private IObservable MessageObservable { get; set; } + public override IObservable OnConnectAsObservable() { - ObserveSimpleAnimation(); - return CurrentStateSubject + CurrentStateProperty.Value = default; + + if (MessageObservable == default) + { + ObserveSimpleAnimation(); + } + + MessageObservable = CurrentStateProperty .Where(x => (AnimationClip == default || x.animationClip == AnimationClip) && x.eventType == SimpleAnimationEventType) - .Select(x => this.CreateMessage(x, MessageParameterKey)); + .Select(x => this.CreateMessage(x, MessageParameterKey)) + .FirstOrDefault(); + + return MessageObservable; } private void ObserveSimpleAnimation() @@ -84,7 +95,7 @@ private void ObserveSimpleAnimation() { PlayingStatuses[state] = false; state - .ObserveEveryValueChanged(x => (SimpleAnimation.IsPlaying(x.name), x.normalizedTime)) + .ObserveEveryValueChanged(x => (SimpleAnimation.IsPlaying(x.name) && !Mathf.Approximately(x.normalizedTime, 1.0f), x.normalizedTime)) .Pairwise() .SubscribeWithState(state, OnChangeAnimatorStateInfo) .AddTo(this); @@ -96,17 +107,17 @@ private void OnChangeAnimatorStateInfo(Pair<(bool isPlaying, float normalizedTim if (pair.Current.isPlaying && !PlayingStatuses[state]) { PlayingStatuses[state] = true; - CurrentStateSubject.OnNext((SimpleAnimationEventType.Play, state.clip)); + CurrentStateProperty.Value = (SimpleAnimationEventType.Play, state.clip); } else if (pair.Previous.isPlaying && !pair.Current.isPlaying && PlayingStatuses[state]) { PlayingStatuses[state] = false; - CurrentStateSubject.OnNext((SimpleAnimationEventType.Stop, state.clip)); + CurrentStateProperty.Value = (SimpleAnimationEventType.Stop, state.clip); } else if (pair.Previous.normalizedTime < 1.0f && Mathf.Approximately(pair.Current.normalizedTime, 1.0f) && PlayingStatuses[state]) { PlayingStatuses[state] = false; - CurrentStateSubject.OnNext((SimpleAnimationEventType.Stop, state.clip)); + CurrentStateProperty.Value = (SimpleAnimationEventType.Stop, state.clip); } }