20240918. 新增 BindableList
BindableProperty 很好用,但是不支持 List 等集合。
而 Bindable List 功能群友呼吁了很久了。
应群友要求,笔者实现了 Bindable List。
基本使用方式如下:
using System;
using UnityEngine;
using UnityEngine.UI;
namespace QFramework.Example
{
    public class BindableListExample : MonoBehaviour
    {
        private BindableList<string> mNameList = new BindableList<string>();
        public Text NameTextTemplate;
        public Transform ContentRoot;
        private void Start()
        {
            NameTextTemplate.Hide();
            
            mNameList.OnCountChanged.Register(count =>
            {
                print("count:" + count);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mNameList.OnAdd.Register((index, newName) =>
            {
                print("add:" + index + "," + newName);
                NameTextTemplate.InstantiateWithParent(ContentRoot)
                    .SiblingIndex(index)
                    .Show()
                    .text = newName;
                
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mNameList.OnMove.Register((oldIndex, newIndex, nameItem) =>
            {
                print("move:" + oldIndex + "," + newIndex + "," + nameItem);
                ContentRoot.GetChild(oldIndex).SiblingIndex(newIndex);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            mNameList.OnRemove.Register((index, nameItem) =>
            {
                print("remove:" + index + "," + nameItem);
                
                ContentRoot.GetChild(index).DestroyGameObjGracefully();
                
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            mNameList.OnReplace.Register((index,oldName, newName) =>
            {
                print("replace:" + index + "," + oldName + "," + newName);
                ContentRoot.GetChild(index).GetComponent<Text>().text = newName;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            mNameList.OnClear.Register(() =>
            {
                print("clear");
                ContentRoot.DestroyChildren();
                
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
        private string mNameToRemove = null;
        private void OnGUI()
        {
            IMGUIHelper.SetDesignResolution(640,360);
            GUILayout.Label("count:" + mNameList.Count);
            GUILayout.BeginVertical("box");
            
            foreach (var nameItem in mNameList)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.Label(nameItem);
                if (GUILayout.Button("-"))
                {
                    mNameToRemove = nameItem;
                }
                
                GUILayout.EndHorizontal();
            }
            if (mNameToRemove.IsNotNullAndEmpty())
            {
                mNameList.Remove(mNameToRemove);
                mNameToRemove = null;
            }
            GUILayout.EndVertical();
            if (GUILayout.Button("add"))
            {
                mNameList.Add("liangxie" + UnityEngine.Random.Range(0,100));
            }
            if (mNameList.Count > 0)
            {
                if (GUILayout.Button("remove at 0"))
                {
                    mNameList.RemoveAt(0);
                }
                if (GUILayout.Button("replace at 0"))
                {
                    mNameList[0] = "replaced name" + UnityEngine.Random.Range(0, 100);
                }
                if (mNameList.Count > 1)
                {
                    if (GUILayout.Button("move 0 -> 1"))
                    {
                        mNameList.Move(0, 1);
                    }
                }
                if (GUILayout.Button("clear"))
                {
                    mNameList.Clear();
                }
            }
        }
    }
}
运行结果如下:

本文由 《QFramework 教程年会员》赞助,298 元,9 套教程一年内随便看。



















