Skip to content

Commit 1c01992

Browse files
committed
Fixed the handling of the power singleton attribute if no type is set
1 parent 280f0c6 commit 1c01992

File tree

7 files changed

+98
-18
lines changed

7 files changed

+98
-18
lines changed

Assets/Kekser/Example/GameManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Kekser.PowerSingleton.Attributes;
1+
using Kekser.PowerSingleton;
2+
using Kekser.PowerSingleton.Attributes;
23
using UnityEngine;
34

45
namespace Kekser.Example
@@ -9,6 +10,7 @@ public class GameManager : MonoBehaviour, IGameManager
910
public void AddScore(int score)
1011
{
1112
Debug.Log($"GameManager: AddScore({score})");
13+
Single<UIManager>.Instance.UpdateScore(score);
1214
}
1315
}
1416
}

Assets/Kekser/Example/ScoreExample.unity

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,49 @@ NavMeshSettings:
123123
debug:
124124
m_Flags: 0
125125
m_NavMeshData: {fileID: 0}
126+
--- !u!1 &246422328
127+
GameObject:
128+
m_ObjectHideFlags: 0
129+
m_CorrespondingSourceObject: {fileID: 0}
130+
m_PrefabInstance: {fileID: 0}
131+
m_PrefabAsset: {fileID: 0}
132+
serializedVersion: 6
133+
m_Component:
134+
- component: {fileID: 246422330}
135+
- component: {fileID: 246422329}
136+
m_Layer: 0
137+
m_Name: UI Manager
138+
m_TagString: Untagged
139+
m_Icon: {fileID: 0}
140+
m_NavMeshLayer: 0
141+
m_StaticEditorFlags: 0
142+
m_IsActive: 1
143+
--- !u!114 &246422329
144+
MonoBehaviour:
145+
m_ObjectHideFlags: 0
146+
m_CorrespondingSourceObject: {fileID: 0}
147+
m_PrefabInstance: {fileID: 0}
148+
m_PrefabAsset: {fileID: 0}
149+
m_GameObject: {fileID: 246422328}
150+
m_Enabled: 1
151+
m_EditorHideFlags: 0
152+
m_Script: {fileID: 11500000, guid: 7df6670f8fb69734c99359ba1d66a55b, type: 3}
153+
m_Name:
154+
m_EditorClassIdentifier:
155+
--- !u!4 &246422330
156+
Transform:
157+
m_ObjectHideFlags: 0
158+
m_CorrespondingSourceObject: {fileID: 0}
159+
m_PrefabInstance: {fileID: 0}
160+
m_PrefabAsset: {fileID: 0}
161+
m_GameObject: {fileID: 246422328}
162+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
163+
m_LocalPosition: {x: 0, y: 0, z: 0}
164+
m_LocalScale: {x: 1, y: 1, z: 1}
165+
m_Children: []
166+
m_Father: {fileID: 0}
167+
m_RootOrder: 2
168+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
126169
--- !u!1 &478411555
127170
GameObject:
128171
m_ObjectHideFlags: 0
@@ -134,7 +177,7 @@ GameObject:
134177
- component: {fileID: 478411557}
135178
- component: {fileID: 478411556}
136179
m_Layer: 0
137-
m_Name: ScoreTicker
180+
m_Name: Score Ticker
138181
m_TagString: Untagged
139182
m_Icon: {fileID: 0}
140183
m_NavMeshLayer: 0
@@ -164,7 +207,7 @@ Transform:
164207
m_LocalScale: {x: 1, y: 1, z: 1}
165208
m_Children: []
166209
m_Father: {fileID: 0}
167-
m_RootOrder: 2
210+
m_RootOrder: 3
168211
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
169212
--- !u!1 &665219491
170213
GameObject:

Assets/Kekser/Example/UIManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Kekser.PowerSingleton.Attributes;
2+
using UnityEngine;
3+
4+
namespace Kekser.Example
5+
{
6+
[PowerSingleton]
7+
public class UIManager : MonoBehaviour
8+
{
9+
public void UpdateScore(int score)
10+
{
11+
Debug.Log($"UIManager: UpdateScore({score})");
12+
}
13+
}
14+
}

Assets/Kekser/Example/UIManager.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/PowerSingleton/PowerSingletonManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ private static void LookUpAttributes()
6666
}
6767

6868
PowerSingletonAttribute attribute = (PowerSingletonAttribute) attributes[0];
69-
_powerSingletons.TryAddToDictionary(attribute.Type, new List<PowerSingletonData>());
70-
_powerSingletons[attribute.Type].Add(new PowerSingletonData
69+
Type attributeType = attribute.Type ?? type;
70+
_powerSingletons.TryAddToDictionary(attributeType, new List<PowerSingletonData>());
71+
_powerSingletons[attributeType].Add(new PowerSingletonData
7172
{
72-
GenericType = attribute.Type,
73+
GenericType = attributeType,
7374
Type = type,
7475
Creation = attribute.Creation,
7576
CreationName = attribute.CreationName,

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.0",
5+
"version": "1.2.1",
66
"unity": "2020.3",
77
"description": "A simple singleton system for Unity",
88
"keywords": [ "singleton" ],

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55
## Contents
66
- [Simple Example](#simple-example)
7-
- [Interface](#interface)
8-
- [If Needed Creation](#if-needed-creation)
9-
- [Auto Creation](#auto-creation)
7+
- [Interface / Abstract Class](#interface--abstract-class)
8+
- [Creation](#creation)
9+
- [If Needed Creation](#if-needed-creation)
10+
- [Auto Creation](#auto-creation)
1011
- [DontDestroyOnLoad](#dontdestroyonload)
11-
- [Install via git URL](#install-via-git-url)
12+
- [Install](#install)
13+
- [Install via Unity Package](#install-via-unity-package)
14+
- [Install via git URL](#install-via-git-url)
1215
- [License](#license)
1316

1417
### Simple Example
1518

1619
You can define a singleton by adding the `PowerSingleton` attribute to a class.
1720
The class must inherit from `MonoBehaviour` and the attribute must be applied to the class.
18-
The type of the singleton must be specified in the attribute.
1921

2022
```csharp
2123
using Kekser.PowerSingleton.Attributes;
2224
using UnityEngine;
2325

24-
[PowerSingleton(typeof(GameManager))]
26+
[PowerSingleton]
2527
public class GameManager : MonoBehaviour
2628
{
2729
public void AddScore(int score)
@@ -39,9 +41,9 @@ using Kekser.PowerSingleton;
3941

4042
Single<GameManager>.Instance.AddScore(10);
4143
```
42-
### Interface
44+
### Interface / Abstract Class
4345

44-
You can also define a singleton by an interface.
46+
You can also define a singleton by an interface or an abstract class.
4547
This widened flexibility allows you to use multiple Singleton classes, given they implement the specified interface. The system intuitively chooses the active class in the current scene.
4648

4749
```csharp
@@ -71,7 +73,9 @@ using Kekser.PowerSingleton;
7173

7274
Single<IGameManager>.Instance.AddScore(10);
7375
```
74-
### If Needed Creation
76+
### Creation
77+
78+
#### If Needed Creation
7579

7680
You can define that the singleton should only be created when it is needed.
7781
This is useful if you want to use the singleton in a scene where it is not yet present.
@@ -92,7 +96,7 @@ public class GameManager : MonoBehaviour
9296
// ...
9397
}
9498
```
95-
### Auto Creation
99+
#### Auto Creation
96100

97101
You can define that the singleton should be created automatically.
98102
This is useful if you want to use the singleton in a scene where it is not yet present.
@@ -132,7 +136,12 @@ public class GameManager : MonoBehaviour
132136
// ...
133137
}
134138
```
135-
### Install via git URL
139+
### Install
140+
141+
#### Install via Unity Package
142+
143+
Download the latest [release](/releases) and import the package into your Unity project.
144+
#### Install via git URL
136145

137146
You can add this package to your project by adding this git URL in the Package Manager:
138147
```

0 commit comments

Comments
 (0)