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

13 files changed

+76
-173
lines changed

CHANGELOG.md

+13
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

+3-3
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/com.unity.package-manager-ui@2.0/manual/index.html

Runtime/MutableObjects/Extensions.meta

-3
This file was deleted.

Runtime/MutableObjects/Extensions/MutableObjectExtensions.cs

-24
This file was deleted.

Runtime/MutableObjects/Extensions/MutableObjectExtensions.cs.meta

-3
This file was deleted.

Runtime/MutableObjects/Generic/IMutableObject.cs

+5
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

+6
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()
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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples/MutableObjects/Prefabs/SceneHandler.prefab

-45
This file was deleted.

Samples/MutableObjects/Prefabs/SceneHandler.prefab.meta

-7
This file was deleted.

Samples/MutableObjects/Scenes/MutableObjects.unity

+4-73
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Transform:
151151
- {fileID: 198214860}
152152
- {fileID: 1418809552}
153153
m_Father: {fileID: 0}
154-
m_RootOrder: 2
154+
m_RootOrder: 1
155155
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
156156
--- !u!1 &198214858
157157
GameObject:
@@ -281,7 +281,7 @@ Transform:
281281
- {fileID: 503514128}
282282
- {fileID: 1902164921}
283283
m_Father: {fileID: 0}
284-
m_RootOrder: 4
284+
m_RootOrder: 3
285285
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
286286
--- !u!4 &503514128 stripped
287287
Transform:
@@ -381,7 +381,7 @@ RectTransform:
381381
m_Children:
382382
- {fileID: 204439456}
383383
m_Father: {fileID: 0}
384-
m_RootOrder: 3
384+
m_RootOrder: 2
385385
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
386386
m_AnchorMin: {x: 0, y: 0}
387387
m_AnchorMax: {x: 0, y: 0}
@@ -607,75 +607,6 @@ PrefabInstance:
607607
objectReference: {fileID: 0}
608608
m_RemovedComponents: []
609609
m_SourcePrefab: {fileID: 100100000, guid: 2060a7fd8dc817a4bb0e331dc4382989, type: 3}
610-
--- !u!1001 &1686363377272728098
611-
PrefabInstance:
612-
m_ObjectHideFlags: 0
613-
serializedVersion: 2
614-
m_Modification:
615-
m_TransformParent: {fileID: 0}
616-
m_Modifications:
617-
- target: {fileID: 1686363378472681812, guid: fc0103a6650789b43a7cb51fb3495f11,
618-
type: 3}
619-
propertyPath: m_Name
620-
value: SceneHandler
621-
objectReference: {fileID: 0}
622-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
623-
type: 3}
624-
propertyPath: m_LocalPosition.x
625-
value: 0
626-
objectReference: {fileID: 0}
627-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
628-
type: 3}
629-
propertyPath: m_LocalPosition.y
630-
value: 0
631-
objectReference: {fileID: 0}
632-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
633-
type: 3}
634-
propertyPath: m_LocalPosition.z
635-
value: 0
636-
objectReference: {fileID: 0}
637-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
638-
type: 3}
639-
propertyPath: m_LocalRotation.x
640-
value: 0
641-
objectReference: {fileID: 0}
642-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
643-
type: 3}
644-
propertyPath: m_LocalRotation.y
645-
value: 0
646-
objectReference: {fileID: 0}
647-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
648-
type: 3}
649-
propertyPath: m_LocalRotation.z
650-
value: 0
651-
objectReference: {fileID: 0}
652-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
653-
type: 3}
654-
propertyPath: m_LocalRotation.w
655-
value: 1
656-
objectReference: {fileID: 0}
657-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
658-
type: 3}
659-
propertyPath: m_RootOrder
660-
value: 0
661-
objectReference: {fileID: 0}
662-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
663-
type: 3}
664-
propertyPath: m_LocalEulerAnglesHint.x
665-
value: 0
666-
objectReference: {fileID: 0}
667-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
668-
type: 3}
669-
propertyPath: m_LocalEulerAnglesHint.y
670-
value: 0
671-
objectReference: {fileID: 0}
672-
- target: {fileID: 1686363378472681818, guid: fc0103a6650789b43a7cb51fb3495f11,
673-
type: 3}
674-
propertyPath: m_LocalEulerAnglesHint.z
675-
value: 0
676-
objectReference: {fileID: 0}
677-
m_RemovedComponents: []
678-
m_SourcePrefab: {fileID: 100100000, guid: fc0103a6650789b43a7cb51fb3495f11, type: 3}
679610
--- !u!1001 &2314108798384297920
680611
PrefabInstance:
681612
m_ObjectHideFlags: 0
@@ -859,7 +790,7 @@ PrefabInstance:
859790
- target: {fileID: 4521292253474263862, guid: 236964640b1e69048a86dc1a58f5f200,
860791
type: 3}
861792
propertyPath: m_RootOrder
862-
value: 1
793+
value: 0
863794
objectReference: {fileID: 0}
864795
- target: {fileID: 4521292253474263862, guid: 236964640b1e69048a86dc1a58f5f200,
865796
type: 3}

Samples/MutableObjects/Scripts/SceneHandler.cs

-13
This file was deleted.

0 commit comments

Comments
 (0)