A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3 4namespace UnityEngine.Rendering 5{ 6 /// <summary> 7 /// Represents an observable value of type T. Subscribers can be notified when the value changes. 8 /// </summary> 9 /// <typeparam name="T">The type of the value.</typeparam> 10 public struct Observable<T> 11 { 12 /// <summary> 13 /// Event that is triggered when the value changes. 14 /// </summary> 15 public event Action<T> onValueChanged; 16 17 private T m_Value; 18 19 /// <summary> 20 /// The current value. 21 /// </summary> 22 public T value 23 { 24 get => m_Value; 25 set 26 { 27 // Only invoke the event if the new value is different from the current value 28 if (!EqualityComparer<T>.Default.Equals(value, m_Value)) 29 { 30 m_Value = value; 31 32 // Notify subscribers when the value changes 33 onValueChanged?.Invoke(value); 34 } 35 } 36 } 37 38 /// <summary> 39 /// Constructor with value 40 /// </summary> 41 /// <param name="newValue">The new value to be assigned.</param> 42 public Observable(T newValue) 43 { 44 m_Value = newValue; 45 onValueChanged = null; 46 } 47 } 48}