diff --git a/README.md b/README.md index c43ca25..61fbee8 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ Unity containers for values and events with change/invoke notifications, as plai ## Install -1. In Unity Editor click **Window** → **Package Manager**. +1. In the Unity Editor, click **Window** → **Package Manager**. 2. Click **+** (top left) → **Install package from git URL…**. -3. Enter this repository URL with `.git` suffix and click **Add**: +3. Enter this repository URL with the `.git` suffix: ``` https://github.com/darksailstudio/refs.git ``` @@ -61,9 +61,9 @@ Asset-backed references (`SharedRef`, `SharedEventRef`) for sharing state and Shared references are created via **Assets** → **Create** → **Shared References** → **\[Type\]**. -#### `SharedRef` +#### `SharedRef` / `ReadOnlySharedRef` -Abstract base for asset-backed values. Same API as [`Ref`](#reft) (except the constructor), but wrapped in a `ScriptableObject` asset. +Abstract base for asset-backed values. Same API as [`Ref`](#reft) (except the constructor), but wrapped in a `ScriptableObject` asset. Read only variant provides immutable access and preserves encapuslation. Requires derived types for values due to Unity serialization constraints (assets can't be generic types). Implementations provided for common C# and Unity primitives: @@ -80,9 +80,9 @@ using UnityEngine; class MyCustomSharedRef : SharedRef {} ``` -#### `SharedEventRef` +#### `SharedEventRef` / `ReadOnlySharedEventRef` -Same API as [`EventRef`](#eventref), but wrapped in a `ScriptableObject` asset. +Same API as [`EventRef`](#eventref), but wrapped in a `ScriptableObject` asset. Read-only variant provides immutable access and preserves encapsulation. #### Examples @@ -92,7 +92,7 @@ using UnityEngine; public class Player : MonoBehaviour { - [SerializeField] SharedIntRef playerHP; + [SerializeField] SharedRef playerHP; [SerializeField] SharedEventRef playerDied; public void TakeDamage(int damage) @@ -109,7 +109,7 @@ using UnityEngine; public class HealthUI : MonoBehaviour { - [SerializeField] SharedIntRef playerHP; + [SerializeField] ReadOnlySharedRef playerHP; void OnEnable() { @@ -135,7 +135,7 @@ using UnityEngine; public class GameOverUI : MonoBehaviour { - [SerializeField] SharedEventRef playerDied; + [SerializeField] ReadOnlySharedEventRef playerDied; void OnEnable() { diff --git a/Runtime/Shared/ReadOnlySharedEventRef.cs b/Runtime/Shared/ReadOnlySharedEventRef.cs new file mode 100644 index 0000000..bb1810d --- /dev/null +++ b/Runtime/Shared/ReadOnlySharedEventRef.cs @@ -0,0 +1,21 @@ +#nullable enable + +using System; +using DarkSail.Resettables; +using UnityEngine; + +namespace DarkSail.Refs +{ + [ResetOnExitPlayMode] + public abstract class ReadOnlySharedEventRef : ScriptableObject, IReadOnlyEventRef + { + [SerializeField] + protected EventRef eventRef = new EventRef(); + + public event Action? Invoked + { + add => eventRef.Invoked += value; + remove => eventRef.Invoked -= value; + } + } +} diff --git a/Runtime/Shared/ReadOnlySharedEventRef.cs.meta b/Runtime/Shared/ReadOnlySharedEventRef.cs.meta new file mode 100644 index 0000000..f4b9974 --- /dev/null +++ b/Runtime/Shared/ReadOnlySharedEventRef.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0aaca03b50e59814e9fb7ca87061f8c9 diff --git a/Runtime/Shared/ReadOnlySharedRef.cs b/Runtime/Shared/ReadOnlySharedRef.cs new file mode 100644 index 0000000..d45a835 --- /dev/null +++ b/Runtime/Shared/ReadOnlySharedRef.cs @@ -0,0 +1,23 @@ +#nullable enable + +using System; +using DarkSail.Resettables; +using UnityEngine; + +namespace DarkSail.Refs +{ + [ResetOnExitPlayMode] + public abstract class ReadOnlySharedRef : ScriptableObject, IReadOnlyRef + { + [SerializeField] + protected Ref valueRef = new Ref(default!); + + public T Value => valueRef.Value; + + public event Action? Changed + { + add => valueRef.Changed += value; + remove => valueRef.Changed -= value; + } + } +} diff --git a/Runtime/Shared/ReadOnlySharedRef.cs.meta b/Runtime/Shared/ReadOnlySharedRef.cs.meta new file mode 100644 index 0000000..0081af0 --- /dev/null +++ b/Runtime/Shared/ReadOnlySharedRef.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 4a1ecfc132d40f445b3c72daf1befb3b diff --git a/Runtime/Shared/SharedEventRef.cs b/Runtime/Shared/SharedEventRef.cs index 63d14b7..0cbc8b2 100644 --- a/Runtime/Shared/SharedEventRef.cs +++ b/Runtime/Shared/SharedEventRef.cs @@ -1,24 +1,12 @@ #nullable enable -using System; -using DarkSail.Resettables; using UnityEngine; namespace DarkSail.Refs { - [ResetOnExitPlayMode] [CreateAssetMenu(menuName = "Shared References/Event")] - public class SharedEventRef : ScriptableObject, IEventRef + public class SharedEventRef : ReadOnlySharedEventRef, IEventRef { - [SerializeField] - EventRef eventRef = new EventRef(); - - public event Action? Invoked - { - add => eventRef.Invoked += value; - remove => eventRef.Invoked -= value; - } - public void Invoke() => eventRef.Invoke(); } } diff --git a/Runtime/Shared/SharedRef.cs b/Runtime/Shared/SharedRef.cs index 300c909..c44bec8 100644 --- a/Runtime/Shared/SharedRef.cs +++ b/Runtime/Shared/SharedRef.cs @@ -1,27 +1,13 @@ #nullable enable -using System; -using DarkSail.Resettables; -using UnityEngine; - namespace DarkSail.Refs { - [ResetOnExitPlayMode] - public abstract class SharedRef : ScriptableObject, IRef + public abstract class SharedRef : ReadOnlySharedRef, IRef { - [SerializeField] - Ref valueRef = new Ref(default!); - - public T Value + public new T Value { - get => valueRef.Value; + get => base.Value; set => valueRef.Value = value; } - - public event Action? Changed - { - add => valueRef.Changed += value; - remove => valueRef.Changed -= value; - } } }