Skip to content

Commit 1580259

Browse files
committed
Added manual binding
1 parent ca9ef24 commit 1580259

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

Assets/Kekser/PowerSingleton/Single.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ public static T Instance {
1111
get
1212
{
1313
if (_rawInstance != null) return _instance;
14-
_rawInstance = PowerSingletonManager.Get(typeof(T));
15-
_instance = _rawInstance as T;
14+
Bind(PowerSingletonManager.Get(typeof(T)));
1615
return _instance;
1716
}
1817
}
18+
19+
public static void Bind(Object instance)
20+
{
21+
_rawInstance = instance;
22+
_instance = instance as T;
23+
}
1924
}
2025
}

Assets/Kekser/PowerSingleton/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.kekser.powersingleton",
33
"displayName": "PowerSingleton",
44
"author": { "name": "Kekser", "url": "https://github.com/DerKekser" },
5-
"version": "1.2.3",
5+
"version": "1.3.0",
66
"unity": "2020.3",
77
"description": "A simple singleton system for Unity",
88
"keywords": [ "singleton" ],
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using UnityEngine;
2+
3+
namespace Kekser.Tests
4+
{
5+
public class BindSingleton : MonoBehaviour
6+
{
7+
public static bool Created = false;
8+
9+
private void Awake()
10+
{
11+
Created = true;
12+
}
13+
14+
private void OnDestroy()
15+
{
16+
Created = false;
17+
}
18+
}
19+
}

Assets/Kekser/Tests/BindSingleton.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.

Assets/Kekser/Tests/PowerSingletonTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public IEnumerator TestDeleteSingleton()
9999
Assert.IsNull(Single<DefaultSingleton>.Instance);
100100
LogAssert.Expect(LogType.Error, "PowerSingletonManager: No PowerSingletonAttribute for type Kekser.Tests.DefaultSingleton, and no instance in scene");
101101
}
102+
103+
[UnityTest]
104+
[LoadScene("Assets/Kekser/Tests/Scenes/PowerSingletonTestScene.unity")]
105+
public IEnumerator TestBindSingleton()
106+
{
107+
yield return null;
108+
Assert.IsFalse(BindSingleton.Created);
109+
Single<BindSingleton>.Bind(new GameObject("BindSingleton").AddComponent<BindSingleton>());
110+
yield return null;
111+
Assert.IsTrue(BindSingleton.Created);
112+
Assert.IsNotNull(Single<BindSingleton>.Instance);
113+
}
102114
}
103115

104116
public class LoadSceneAttribute : NUnitAttribute, IOuterUnityTestAction

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [If Needed Creation](#if-needed-creation)
1010
- [Auto Creation](#auto-creation)
1111
- [DontDestroyOnLoad](#dontdestroyonload)
12+
- [Manual Binding](#manual-binding)
1213
- [Install](#install)
1314
- [Install via Unity Package](#install-via-unity-package)
1415
- [Install via git URL](#install-via-git-url)
@@ -136,6 +137,17 @@ public class GameManager : MonoBehaviour
136137
// ...
137138
}
138139
```
140+
### Manual Binding
141+
142+
You can manually bind a MonoBehaviour to a singleton.
143+
This is useful if you cannot use the `PowerSingleton` attribute or if you want to change the singleton at runtime.
144+
145+
```csharp
146+
using Kekser.PowerSingleton;
147+
148+
GameManager gameManager = new GameObject("GameManager").AddComponent<GameManager>();
149+
Single<GameManager>.Bind(gameManager);
150+
```
139151
### Install
140152

141153
#### Install via Unity Package

0 commit comments

Comments
 (0)