facebook twitter hatena line email

「Unity/R3/値変更検知/ObservableList」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の1版が非表示)
行2: 行2:
 
===まとめて検知===
 
===まとめて検知===
 
値をObservableListで入れて、ObserveChangedでまとめて検知する。
 
値をObservableListで入れて、ObserveChangedでまとめて検知する。
 +
 +
参考:https://github.com/Cysharp/ObservableCollections
 
<pre>
 
<pre>
 
using UnityEngine;
 
using UnityEngine;
行23: 行25:
 
                     break;
 
                     break;
 
                 case NotifyCollectionChangedAction.Move:
 
                 case NotifyCollectionChangedAction.Move:
                     Debug.Log($"[Move]: {change.OldItem} → {change.NewItem}");
+
                     Debug.Log($"[Move]: {change.OldStartingIndex} → {change.NewStartingIndex}");
 
                     break;
 
                     break;
 
                 case NotifyCollectionChangedAction.Remove:
 
                 case NotifyCollectionChangedAction.Remove:
行64: 行66:
 
[Move]: 1 → 2
 
[Move]: 1 → 2
 
</pre>
 
</pre>
 +
 
===個別に検知===
 
===個別に検知===
 
<pre>
 
<pre>

2025年3月21日 (金) 04:11時点における最新版

配列の検知更新

まとめて検知

値をObservableListで入れて、ObserveChangedでまとめて検知する。

参考:https://github.com/Cysharp/ObservableCollections

using UnityEngine;
using R3;
using ObservableCollections;
using System.Collections.Specialized;
public class ReactiveConnectionScene : MonoBehaviour
{
    private ObservableList<string> users = new ObservableList<string>();

    void Start()
    {
        // すべての変更(追加・移動・削除・更新・リセット)を購読
        users.ObserveChanged().Subscribe(change =>
        {
            Debug.Log($"[ObserveChanged]");
            switch (change.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    Debug.Log($"[Add]: {change.NewItem}");
                    break;
                case NotifyCollectionChangedAction.Move:
                    Debug.Log($"[Move]: {change.OldStartingIndex} → {change.NewStartingIndex}");
                    break;
                case NotifyCollectionChangedAction.Remove:
                    Debug.Log($"[Remove]: {change.NewItem}");
                    break;
                case NotifyCollectionChangedAction.Replace:
                    Debug.Log($"[Replace]: {change.OldItem} → {change.NewItem}");
                    break;
                case NotifyCollectionChangedAction.Reset:
                    Debug.Log($"[Reset]");
                    break;
                default:
                    Debug.Log("[Other]");
                    break;
            }
        });

        users.Add("taro");
        users[0] = "taro!";
        users.Add("jiro");
        users.Add("saburo");
        users.Move(0, 1); // 配列0を配列1へ移動(jiro, taro, saburo)
        users.Move(1, 2); // 配列1を配列2へ移動(jiro, saburo, taro)
    }
}

出力

[ObserveChanged]
[Add]: taro
[ObserveChanged]
[Replace]: taro → taro!
[ObserveChanged]
[Add]: jiro
[ObserveChanged]
[Add]: saburo
[ObserveChanged]
[Move]: 0 → 1
[ObserveChanged]
[Move]: 1 → 2

個別に検知

using UnityEngine;
using R3;
using ObservableCollections;
public class ReactiveConnectionScene : MonoBehaviour
{
    private ObservableList<string> users = new ObservableList<string>();
    void Start()
    {
        users
            .ObserveAdd()
            .Subscribe(value =>
            {
                Debug.Log($"[Add]Index={value.Index},Value={value.Value}");
            });
        users
            .ObserveMove()
            .Subscribe(value =>
            {
                Debug.Log($"[Move]Value={value.Value},NewIndex={value.NewIndex},OldIndex={value.OldIndex}");
            });
        users
            .ObserveRemove()
            .Subscribe(value =>
            {
                Debug.Log($"[Remove]Index={value.Index},Value={value.Value}");
            });
        users
            .ObserveReplace()
            .Subscribe(value =>
            {
                Debug.Log($"[Replace]Index={value.Index},NewValue={value.NewValue},OldValue={value.OldValue}");
                
            });
        users
            .ObserveReset()
            .Subscribe(value =>
            {
                Debug.Log($"[Reset]");
            });

        users.Add("taro");
        users[0] = "taro!";
        users.Add("jiro");
        users.Add("saburo");
        users.Move(0, 1); // 配列0を配列1へ移動(jiro, taro, saburo)
        users.Move(1, 2); // 配列1を配列2へ移動(jiro, saburo, taro)
    }
}

出力

[Add]Index=0,Value=taro
[Replace]Index=0,NewValue=taro!,OldValue=taro
[Move]Value=taro!,NewIndex=1,OldIndex=0
[Move]Value=taro!,NewIndex=2,OldIndex=1

参考:https://hal9.hateblo.jp/entry/2021/06/06/071739