UniRx 泛型 Notifier 数据改变时执行方法

using System;
using UniRx;
using UnityEngine;

namespace Test.testUniRx
{
    public class TestUniRxNotifierT : MonoBehaviour
    {
        //[ShowInInspector]
        public Notifier<Vector3> test = new Notifier<Vector3>();
        private ScheduledNotifier<Vector3> progressNotifier = new ScheduledNotifier<Vector3>();
        private IDisposable _disposable;
        private IDisposable _disposable2;
        
        //[Button]
        public void ChangeValue()
        {
            test.Value += Vector3.one;
            // progressNotifier.Report(testFloat);
        }

        //[Button]
        public void Bind()
        {
            _disposable = test.Subscribe(s =>
            {
                Debug.Log($"mylog:new T Value is : {s.ToString("f3")}");
                progressNotifier.Report(s);
            });
            _disposable2 = progressNotifier.Subscribe(x => { Debug.Log($"mylog: 输出了:{x.ToString("f3")}");}); // write www.progress
        }

        //[Button]
        public void UnBind()
        {
            _disposable.Dispose();
            _disposable2.Dispose();
        }
    }

    /// <summary>
    /// Notify T flag.
    /// </summary>
    public class Notifier<T> : IObservable<T>
    {
        readonly Subject<T> _subject = new Subject<T>();

        T _vector3Value;

        /// <summary>Current flag value</summary>
        //[ShowInInspector]
        public T Value
        {
            get => _vector3Value;
            set
            {
                _vector3Value = value;
                _subject.OnNext(value);
            }
        }

        /// <summary>
        /// Setup initial flag.
        /// </summary>
        public Notifier(T initialValue = default)
        {
            Value = initialValue;
        }

        /// <summary>
        /// Subscribe observer.
        /// </summary>
        public IDisposable Subscribe(IObserver<T> observer)
        {
            return _subject.Subscribe(observer);
        }
        
        /// <summary>
        /// Empty Action
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public IDisposable Subscribe(Action action)
        {
            return _subject.Subscribe((obj =>
            {
                action.Invoke();
            }));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值