Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit 65db3a8

Browse files
authored
Automate mutable values (#12)
* Enable persisting of mutable objects throughout scenes, replace extensions with Unity hooks * Update changelog and docs
1 parent 6bd9836 commit 65db3a8

File tree

13 files changed

+76
-173
lines changed

13 files changed

+76
-173
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.0] - 2020-10-07
8+
9+
### Added
10+
- `MutableObjectHandler` which would automate the process of resetting `MutableObjects`.
11+
- `Persisting` property on `IMutableObject` which determines how resetting should be handled.
12+
13+
### Changed
14+
- Cleaned up `MutableObject` sample.
15+
- Updated documentation.
16+
17+
### Removed
18+
- `MutableObjectExtensions` as its purpose is now automated.
19+
720
## [0.3.0] - 2020-10-05
821

922
### Added

Documentation~/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Available game events:
3131
width="100%" alt="Example usage of Mutable Objects"
3232
/>
3333

34-
Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when a new level loads (e.g. in `Awake` on a `GameManager` script with a properly setup [Script Execution Order](https://docs.unity3d.com/Manual/class-MonoManager.html)). Mutable objects can be reset using `MutableObjectExtensions.ResetMutatedObjects()` method.
34+
Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when a new level loads.
3535

3636
Available mutable objects:
3737
- `MutableBool` - encapsulates a `bool` value.
@@ -41,6 +41,8 @@ Available mutable objects:
4141
- `MutableVector2` - encapsulates a `Vector2` value.
4242
- `MutableVector3` - encapsulates a `Vector3` value.
4343

44+
If a mutable object value should be saved between scene loads, check the `Persisting` flag on the `MutableObject` asset.
45+
4446
### Custom game events
4547
In some situations, built-in game events might not suffice. For example if a custom type needs to be passed as an argument to the event. In this case, custom game event can be created which would carry all the necessary data.
4648

@@ -102,5 +104,3 @@ public class MutableCustom : MutableObject
102104
}
103105
}
104106
```
105-
106-
[Unity Package Manager]: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

Runtime/MutableObjects/Extensions.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Runtime/MutableObjects/Extensions/MutableObjectExtensions.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

Runtime/MutableObjects/Extensions/MutableObjectExtensions.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

Runtime/MutableObjects/Generic/IMutableObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
{
33
public interface IMutableObject
44
{
5+
/// <summary>
6+
/// Is this mutable object persisting throughout the scenes and should not be reset.
7+
/// </summary>
8+
bool Persisting { get; }
9+
510
/// <summary>
611
/// Reset values on this mutable object to their original ones.
712
/// </summary>

Runtime/MutableObjects/Generic/MutableObject.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ namespace MutableObjects.Generic
44
{
55
public abstract class MutableObject : ScriptableObject, IMutableObject
66
{
7+
[SerializeField]
8+
[Tooltip("Should this mutable object be persisted throughout scene loads")]
9+
private bool persisting = false;
10+
11+
public bool Persisting => persisting;
12+
713
public abstract void ResetValues();
814

915
private void OnValidate()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using MutableObjects.Generic;
4+
using UnityEngine;
5+
using UnityEngine.SceneManagement;
6+
7+
namespace MutableObjects
8+
{
9+
public static class MutableObjectHandler
10+
{
11+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
12+
private static void RuntimeInit()
13+
{
14+
SetInitialMutableObjectValues();
15+
SceneManager.sceneUnloaded += scene => ResetMutableObjectValues();
16+
}
17+
18+
private static void SetInitialMutableObjectValues()
19+
{
20+
var mutableObjects = FindMutableObjects();
21+
foreach (var mutableObject in mutableObjects)
22+
{
23+
mutableObject.ResetValues();
24+
}
25+
}
26+
27+
private static void ResetMutableObjectValues()
28+
{
29+
var mutableObjects = FindMutableObjects()
30+
.Where(mutableObject => !mutableObject.Persisting);
31+
32+
foreach (var mutableObject in mutableObjects)
33+
{
34+
mutableObject.ResetValues();
35+
}
36+
}
37+
38+
private static IEnumerable<IMutableObject> FindMutableObjects()
39+
{
40+
return Resources.FindObjectsOfTypeAll<MutableObject>();
41+
}
42+
}
43+
}

Samples/MutableObjects/Scripts/SceneHandler.cs.meta renamed to Runtime/MutableObjects/MutableObjectHandler.cs.meta

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

Samples/MutableObjects/Prefabs/SceneHandler.prefab

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)