Skip to content

Commit 3585523

Browse files
committed
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)
1 parent 9b2481f commit 3585523

File tree

8 files changed

+113
-4
lines changed

8 files changed

+113
-4
lines changed

playground-unity/Assets/HelloWorld/HelloWorld.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ public class HelloWorld : MonoBehaviour
77
[SerializeField] TMP_InputField nameInput;
88
[SerializeField] TMP_Text greetingLabel;
99

10+
// define piece of mutable observable state
11+
[SerializeField] SerializableState<string> name = new("World");
12+
1013
void Start()
1114
{
12-
// define piece of mutable observable state
13-
var name = Observable.State("World");
14-
1515
// bind the state two-ways to an input field
1616
name.Bind(nameInput.SetTextWithoutNotify);
1717
nameInput.onValueChanged.AddListener(newValue => name.Value = newValue);
@@ -22,4 +22,4 @@ void Start()
2222
// bind the auto-observable to a text field
2323
greeting.Bind(text => greetingLabel.text = text);
2424
}
25-
}
25+
}

src/TinkState-Unity/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "Nadako.TinkState.Unity.Editor",
3+
"references": [
4+
"Nadako.TinkState.Unity"
5+
]
6+
}

src/TinkState-Unity/Editor/Nadako.TinkState.Unity.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace TinkState
5+
{
6+
[CustomPropertyDrawer(typeof(SerializableState<>))]
7+
class SerializableStateDrawer : PropertyDrawer
8+
{
9+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
10+
{
11+
EditorGUI.BeginProperty(position, label, property);
12+
EditorGUI.PropertyField(position, property.FindPropertyRelative("value"), label);
13+
EditorGUI.EndProperty();
14+
}
15+
16+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
17+
{
18+
return EditorGUI.GetPropertyHeight(property.FindPropertyRelative("value"), label);
19+
}
20+
}
21+
}

src/TinkState-Unity/Editor/SerializableStateDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using TinkState;
4+
using UnityEngine;
5+
6+
namespace TinkState
7+
{
8+
[Serializable]
9+
public class SerializableState<T> : State<T>, ISerializationCallbackReceiver
10+
{
11+
State<T> state;
12+
[SerializeField] T value;
13+
14+
public SerializableState(T initialValue)
15+
{
16+
state = Observable.State(initialValue);
17+
value = initialValue;
18+
}
19+
20+
public T Value
21+
{
22+
get => state.Value;
23+
set
24+
{
25+
state.Value = value;
26+
this.value = value;
27+
}
28+
}
29+
30+
public override string ToString() => state.ToString();
31+
32+
public IDisposable Bind(Action<T> callback, IEqualityComparer<T> comparer = null, Scheduler scheduler = null) =>
33+
state.Bind(callback, comparer, scheduler);
34+
35+
public Observable<TOut> Map<TOut>(Func<T, TOut> transform, IEqualityComparer<TOut> comparer = null) => state.Map(transform, comparer);
36+
37+
void ISerializationCallbackReceiver.OnBeforeSerialize() {}
38+
39+
void ISerializationCallbackReceiver.OnAfterDeserialize()
40+
{
41+
state ??= Observable.State(value);
42+
Value = value;
43+
}
44+
}
45+
}

src/TinkState-Unity/Runtime/SerializableState.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)