Rasagar/Library/PackageCache/com.unity.render-pipelines.core/Runtime/Common/Observable.cs
2024-08-26 23:07:20 +03:00

49 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering
{
/// <summary>
/// Represents an observable value of type T. Subscribers can be notified when the value changes.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
public struct Observable<T>
{
/// <summary>
/// Event that is triggered when the value changes.
/// </summary>
public event Action<T> onValueChanged;
private T m_Value;
/// <summary>
/// The current value.
/// </summary>
public T value
{
get => m_Value;
set
{
// Only invoke the event if the new value is different from the current value
if (!EqualityComparer<T>.Default.Equals(value, m_Value))
{
m_Value = value;
// Notify subscribers when the value changes
onValueChanged?.Invoke(value);
}
}
}
/// <summary>
/// Constructor with value
/// </summary>
/// <param name="newValue">The new value to be assigned.</param>
public Observable(T newValue)
{
m_Value = newValue;
onValueChanged = null;
}
}
}