Last active
March 10, 2020 02:25
-
-
Save hammanandre/2672bc2029756ec86547c5c4f04a9eb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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