-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SerializableState implementation from #33
it's a bit opinonated, so maybe it should go into a separate package and/or namespace, not to conflict with custom implementations people have (e.g. Lythom has its own with different serialization methods)
- Loading branch information
Showing
8 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
src/TinkState-Unity/Editor/Nadako.TinkState.Unity.Editor.asmdef
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "Nadako.TinkState.Unity.Editor", | ||
"references": [ | ||
"Nadako.TinkState.Unity" | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
src/TinkState-Unity/Editor/Nadako.TinkState.Unity.Editor.asmdef.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace TinkState | ||
{ | ||
[CustomPropertyDrawer(typeof(SerializableState<>))] | ||
class SerializableStateDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
EditorGUI.BeginProperty(position, label, property); | ||
EditorGUI.PropertyField(position, property.FindPropertyRelative("value"), label); | ||
EditorGUI.EndProperty(); | ||
} | ||
|
||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | ||
{ | ||
return EditorGUI.GetPropertyHeight(property.FindPropertyRelative("value"), label); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/TinkState-Unity/Editor/SerializableStateDrawer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using TinkState; | ||
using UnityEngine; | ||
|
||
namespace TinkState | ||
{ | ||
[Serializable] | ||
public class SerializableState<T> : State<T>, ISerializationCallbackReceiver | ||
{ | ||
State<T> state; | ||
[SerializeField] T value; | ||
|
||
public SerializableState(T initialValue) | ||
{ | ||
state = Observable.State(initialValue); | ||
value = initialValue; | ||
} | ||
|
||
public T Value | ||
{ | ||
get => state.Value; | ||
set | ||
{ | ||
state.Value = value; | ||
this.value = value; | ||
} | ||
} | ||
|
||
public override string ToString() => state.ToString(); | ||
|
||
public IDisposable Bind(Action<T> callback, IEqualityComparer<T> comparer = null, Scheduler scheduler = null) => | ||
state.Bind(callback, comparer, scheduler); | ||
|
||
public Observable<TOut> Map<TOut>(Func<T, TOut> transform, IEqualityComparer<TOut> comparer = null) => state.Map(transform, comparer); | ||
|
||
void ISerializationCallbackReceiver.OnBeforeSerialize() {} | ||
|
||
void ISerializationCallbackReceiver.OnAfterDeserialize() | ||
{ | ||
state ??= Observable.State(value); | ||
Value = value; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.