屬性綁定數據綁定思路

/// 方案對比:
/// 表現層監聽消息--->數據層改變發消息---->表現層收到消息,表現層改變
/// 表現層監聽消息--->數據層改變會自動發消息(少寫了發消息的代碼),表現層改變
/// 優勢:少寫了發消息的代碼spa

能夠說是另一種思路,可是也能夠其餘辦法解決,自動發消息這個過程,這裏我實現了個code

核心代碼blog

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;

/// <summary>
/// 屬性綁定
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindableProperty<T>
{
    private List<Action> changeList = new List<Action>();
    private T _data;

    public BindableProperty(T data)
    {
        _data = data;
    }

    public T data
    {
        get
        {
            return _data;
        }
        set
        {
            _data = value;
            DispatchAll();
        }
    }

    public void AddListener(Action ac)
    {
        if (!changeList.Contains(ac))
        {
            changeList.Add(ac);
        }
        else
        {
            Debug.LogWarning("重複監聽");
        }
    }

    public void RemoveListener(Action ac)
    {
        if (changeList.Contains(ac))
        {
            changeList.Remove(ac);
        }
        else
        {
            Debug.LogWarning("移除的消息不存在");
        }
    }

    public void ClearListener()
    {
        changeList.Clear();
        Debug.Log("清理成功");
    }

    private void DispatchAll()
    {
        foreach (var item in changeList)
        {
            item();
        }
    }
}

/// <summary>
/// List
/// 使用了ReadOnlyCollection做爲保護,使用IReadOnlyList也是能夠
/// 經過Add Remove  Update  Clear 對數據進行操做
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindableList<T>
{
    private List<Action> changeList = new List<Action>();
    private List<T> _data;

    public BindableList()
    {
        _data = new List<T>();
    }

    public ReadOnlyCollection<T> data
    {
        get {
            return _data.AsReadOnly();
        }
        private set {
            
        }
    }

    public void Add(T t)
    {
        _data.Add(t);
        DispatchAll();
    }

    public void Remove(T t)
    {
        if (_data.Contains(t))
        {
            _data.Remove(t);
            DispatchAll();
        }
    }

    public void Clear()
    {
        _data.Clear();
        DispatchAll();
    }

    public void Update(List<T> _list)
    {
        _data = _list;
        DispatchAll();
    }

    public void AddListener(Action ac)
    {
        if (!changeList.Contains(ac))
        {
            changeList.Add(ac);
        }
        else
        {
            Debug.LogWarning("重複監聽");
        }
    }

    public void RemoveListener(Action ac)
    {
        if (changeList.Contains(ac))
        {
            changeList.Remove(ac);
        }
        else
        {
            Debug.LogWarning("移除的消息不存在");
        }
    }

    public void ClearListener()
    {
        changeList.Clear();
        Debug.Log("清理成功");
    }

    private void DispatchAll()
    {
        foreach (var item in changeList)
        {
            item();
        }
    }
}

/// <summary>
/// Dic
/// 使用IReadOnlyDictionary做爲保護
/// 經過Add RemoveByKey  Update  Clear 對數據進行操做
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindableDic<T,V>
{
    private List<Action> changeList = new List<Action>();
    private Dictionary<T, V> _data;
    public IReadOnlyDictionary<T, V> data
    { get { return _data; } }

    public BindableDic()
    {
        _data = new Dictionary<T, V>();
    }

    public void Add(T t, V v)
    {
        _data.Add(t,v);
        DispatchAll();
    }

    public void RemoveByKey(T _key)
    {
        if (_data.ContainsKey(_key))
        {
            _data.Remove(_key);
            DispatchAll();
        }
    }

    public void Clear()
    {
        _data.Clear();
        DispatchAll();
    }

    public void Update(Dictionary<T,V> _dic)
    {
        _data = _dic;
        DispatchAll();
    }

    public void AddListener(Action ac)
    {
        if (!changeList.Contains(ac))
        {
            changeList.Add(ac);
        }
        else
        {
            Debug.LogWarning("重複監聽");
        }
    }

    public void RemoveListener(Action ac)
    {
        if (changeList.Contains(ac))
        {
            changeList.Remove(ac);
        }
        else
        {
            Debug.LogWarning("移除的消息不存在");
        }
    }

    public void ClearListener()
    {
        changeList.Clear();
        Debug.Log("清理成功");
    }

    private void DispatchAll()
    {
        foreach (var item in changeList)
        {
            item();
        }
    }
}

 

調用,在unity實現get

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 數據自動變化
/// 
/// 方案對比:
/// 表現層監聽消息--->數據層改變發消息---->表現層收到消息,表現層改變
/// 表現層監聽消息--->數據層改變會自動發消息(少寫了發消息的代碼),表現層改變
/// 優勢:少寫了發消息的代碼
/// </summary>
public class TestBind : MonoBehaviour
{
    BindableProperty<int> hpData = new BindableProperty<int>(100);
    BindableList<int> friendList = new BindableList<int>();
    BindableDic<int, string> myDic = new BindableDic<int, string>();

    // Start is called before the first frame update
    void Start()
    {
        hpData.AddListener(delegate() {
            print("hh" + hpData.data);
        });

        friendList.AddListener(delegate () {
            print("ff" + friendList.data.Count);
        });

        myDic.AddListener(delegate ()
        {
            print("dd" + myDic.data.Count);
        });
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            hpData.data = 99;
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            friendList.Add(658);
            friendList.Add(638);
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            print(friendList.data.Count);
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            myDic.Add(1,"sas");
            myDic.Add(2, "sas2212");
            print(myDic.data.Count);
        }
    }
}
相關文章
相關標籤/搜索