Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -61,9 +61,9 @@ Asset-backed references (`SharedRef<T>`, `SharedEventRef`) for sharing state and

Shared references are created via **Assets** → **Create** → **Shared References** → **\[Type\]**.

#### `SharedRef<T>`
#### `SharedRef<T>` / `ReadOnlySharedRef<T>`

Abstract base for asset-backed values. Same API as [`Ref<T>`](#reft) (except the constructor), but wrapped in a `ScriptableObject` asset.
Abstract base for asset-backed values. Same API as [`Ref<T>`](#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:

Expand All @@ -80,9 +80,9 @@ using UnityEngine;
class MyCustomSharedRef : SharedRef<MySerializableType> {}
```

#### `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

Expand All @@ -92,7 +92,7 @@ using UnityEngine;

public class Player : MonoBehaviour
{
[SerializeField] SharedIntRef playerHP;
[SerializeField] SharedRef<int> playerHP;
[SerializeField] SharedEventRef playerDied;

public void TakeDamage(int damage)
Expand All @@ -109,7 +109,7 @@ using UnityEngine;

public class HealthUI : MonoBehaviour
{
[SerializeField] SharedIntRef playerHP;
[SerializeField] ReadOnlySharedRef<int> playerHP;

void OnEnable()
{
Expand All @@ -135,7 +135,7 @@ using UnityEngine;

public class GameOverUI : MonoBehaviour
{
[SerializeField] SharedEventRef playerDied;
[SerializeField] ReadOnlySharedEventRef playerDied;

void OnEnable()
{
Expand Down
21 changes: 21 additions & 0 deletions Runtime/Shared/ReadOnlySharedEventRef.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 2 additions & 0 deletions Runtime/Shared/ReadOnlySharedEventRef.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Runtime/Shared/ReadOnlySharedRef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#nullable enable

using System;
using DarkSail.Resettables;
using UnityEngine;

namespace DarkSail.Refs
{
[ResetOnExitPlayMode]
public abstract class ReadOnlySharedRef<T> : ScriptableObject, IReadOnlyRef<T>
{
[SerializeField]
protected Ref<T> valueRef = new Ref<T>(default!);

public T Value => valueRef.Value;

public event Action<T>? Changed
{
add => valueRef.Changed += value;
remove => valueRef.Changed -= value;
}
}
}
2 changes: 2 additions & 0 deletions Runtime/Shared/ReadOnlySharedRef.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions Runtime/Shared/SharedEventRef.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
20 changes: 3 additions & 17 deletions Runtime/Shared/SharedRef.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
#nullable enable

using System;
using DarkSail.Resettables;
using UnityEngine;

namespace DarkSail.Refs
{
[ResetOnExitPlayMode]
public abstract class SharedRef<T> : ScriptableObject, IRef<T>
public abstract class SharedRef<T> : ReadOnlySharedRef<T>, IRef<T>
{
[SerializeField]
Ref<T> valueRef = new Ref<T>(default!);

public T Value
public new T Value
{
get => valueRef.Value;
get => base.Value;
set => valueRef.Value = value;
}

public event Action<T>? Changed
{
add => valueRef.Changed += value;
remove => valueRef.Changed -= value;
}
}
}