Skip to content

Instantly share code, notes, and snippets.

@aleverdes
Created October 5, 2023 08:16
Show Gist options
  • Save aleverdes/adfdb2fbd3b8c6db63a849a8b5cf92d2 to your computer and use it in GitHub Desktop.
Save aleverdes/adfdb2fbd3b8c6db63a849a8b5cf92d2 to your computer and use it in GitHub Desktop.
ReactiveProperty<T> for Unity
using System;
using UnityEngine;
namespace SunnyverseGames
{
[Serializable]
public sealed class ReactiveProperty<T>
{
[SerializeField] private T _value;
public T Value
{
get => _value;
set
{
if (Equals(value, _value))
{
return;
}
_value = value;
ValueChanged?.Invoke(value);
}
}
public event Action<T> ValueChanged;
public void SetWithoutNotify(T value)
{
_value = value;
}
public static implicit operator T(ReactiveProperty<T> reactiveProperty) => reactiveProperty.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment