17. 内置工具:BindableProperty

在此篇介绍 BindableProperty。

BindableProperty 提供 数据 + 数据变更事件 的一个对象。

基本使用

using UnityEngine;

namespace QFramework.Example
{
    public class BindablePropertyExample : MonoBehaviour
    {
        private BindableProperty<int> mSomeValue = new BindableProperty<int>(0);

        private BindableProperty<string> mName = new BindableProperty<string>("QFramework");

        void Start()
        {
            mSomeValue.Register(newValue =>
            {
                Debug.Log(newValue);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            mName.RegisterWithInitValue(newName =>
            {
                Debug.Log(mName);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        void Update()
        {

            if (Input.GetMouseButtonDown(0))
            {
                mSomeValue.Value++;
            }
        }
    }
}


// 输出结果// QFramework// 按下鼠标左键,输出:// 1// 按下鼠标左键,输出:// 2

非常简单。

关于 BindableProperty,在之前写 CounterApp 的时候有介绍过,所以这篇就介绍到这里。