memomem

備忘録・メモ置き場

C# ObservableCollection

ObservableCollection 知らなかった。

要素の変更時にイベント発生。

using UnityEngine;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

public class ObservableCollectionTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        ObservableCollection<int> col = new ObservableCollection<int>();
        // コレクション変更のイベントを受け取るハンドラを設定
        col.CollectionChanged += PrintCollectionChanged;

        // 要素の追加
        col.Add(0);

        // 要素の削除
        col.RemoveAt(0);
    }

    void PrintCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Debug.Log(">>> act: " + e.Action);
    }
}

参考

qiita.com

smdn.jp