Skip to content

Instantly share code, notes, and snippets.

@hammanandre
Last active March 10, 2020 02:25
Show Gist options
  • Save hammanandre/2672bc2029756ec86547c5c4f04a9eb9 to your computer and use it in GitHub Desktop.
Save hammanandre/2672bc2029756ec86547c5c4f04a9eb9 to your computer and use it in GitHub Desktop.
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
[Serializable]
public class ConditionalObserver<T>
{
[ShowInInspector]
public T Value
{
get { return _value; }
set {TestCondtions();_value = value; }
}
private void TestCondtions()
{
foreach (var key in condtions.Keys)
{
if (key.Invoke(_value))
{
condtions[key].Invoke(_value);
}
}
}
private T _value;
public Dictionary<Func<T, bool>, Action<T>> condtions;
public ConditionalObserver()
{
condtions = new Dictionary<Func<T, bool>, Action<T>>();
}
public ConditionalObserver(Dictionary<Func<T, bool>, Action<T>> condtions)
{
this.condtions = condtions;
}
public ConditionalObserver(T value, Dictionary<Func<T, bool>, Action<T>> condtions)
{
_value = value;
this.condtions = condtions;
}
}
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using UnityEngine.Events;
[Serializable]
public class Observer<T>
{
[ShowInInspector]
public T Value
{
get { return _value; }
set {Notify.Invoke(value); _value = value; }
}
private T _value;
public UnityEvent<T> Notify;
public Observer(UnityEvent<T> notify)
{
Notify = notify;
}
public Observer(T value, UnityEvent<T> notify)
{
_value = value;
Notify = notify;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment