Skip to content

Commit 40f241e

Browse files
First Commit
1 parent de52c9b commit 40f241e

File tree

399 files changed

+46130
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

399 files changed

+46130
-2
lines changed

.DS_Store

8 KB
Binary file not shown.

BadgeNotificationSystem.meta

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

BadgeNotificationSystem/.DS_Store

6 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &8115805376265406789
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
serializedVersion: 6
10+
m_Component:
11+
- component: {fileID: 6125993688496956866}
12+
- component: {fileID: 8823135042377463800}
13+
m_Layer: 0
14+
m_Name: BadgeManager
15+
m_TagString: Untagged
16+
m_Icon: {fileID: 0}
17+
m_NavMeshLayer: 0
18+
m_StaticEditorFlags: 0
19+
m_IsActive: 1
20+
--- !u!4 &6125993688496956866
21+
Transform:
22+
m_ObjectHideFlags: 0
23+
m_CorrespondingSourceObject: {fileID: 0}
24+
m_PrefabInstance: {fileID: 0}
25+
m_PrefabAsset: {fileID: 0}
26+
m_GameObject: {fileID: 8115805376265406789}
27+
serializedVersion: 2
28+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
29+
m_LocalPosition: {x: 0, y: 0, z: 0}
30+
m_LocalScale: {x: 1, y: 1, z: 1}
31+
m_ConstrainProportionsScale: 0
32+
m_Children: []
33+
m_Father: {fileID: 0}
34+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
35+
--- !u!114 &8823135042377463800
36+
MonoBehaviour:
37+
m_ObjectHideFlags: 0
38+
m_CorrespondingSourceObject: {fileID: 0}
39+
m_PrefabInstance: {fileID: 0}
40+
m_PrefabAsset: {fileID: 0}
41+
m_GameObject: {fileID: 8115805376265406789}
42+
m_Enabled: 1
43+
m_EditorHideFlags: 0
44+
m_Script: {fileID: 11500000, guid: cfa30d055f3c4f64bb0e2d2151af7afe, type: 3}
45+
m_Name:
46+
m_EditorClassIdentifier:

BadgeNotificationSystem/BadgeManager.prefab.meta

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

BadgeNotificationSystem/Core.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.Collections;
2+
using NaughtyAttributes;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
namespace SentryToolkit
7+
{
8+
public class BadgeComponent : MonoBehaviour
9+
{
10+
public BadgeID badgeId; // Unique ID for the badge
11+
public Image badgeSymbolImage; // Image component to display the badge symbol
12+
13+
public bool autoGetButton = true;
14+
[DisableIf("autoGetButton")]
15+
public Button badgeClickButton;
16+
BadgeManager badgeManager;
17+
18+
private void Start()
19+
{
20+
InitializeBadgeManager();
21+
if (autoGetButton)
22+
GetComponent<Button>().onClick.AddListener(OnBadgeClicked);
23+
else
24+
badgeClickButton.onClick.AddListener(OnBadgeClicked);
25+
}
26+
27+
void InitializeBadgeManager()
28+
{
29+
if (!badgeManager)
30+
{
31+
badgeManager = BadgeManager.Instance;
32+
}
33+
}
34+
35+
private void OnEnable()
36+
{
37+
InitializeBadgeManager();
38+
StartCoroutine(HandleManagerSubscription());
39+
}
40+
41+
private IEnumerator HandleManagerSubscription()
42+
{
43+
yield return new WaitForEndOfFrame();
44+
45+
if (badgeSymbolImage != null) badgeSymbolImage.gameObject.SetActive(false);
46+
47+
if (badgeManager != null) {
48+
badgeManager.OnBadgeStateChanged += HandleBadgeStateChanged;
49+
}
50+
else {
51+
Debug.LogError("Badge Manager is null");
52+
}
53+
UpdateBadge();
54+
}
55+
56+
private void OnDisable()
57+
{
58+
if (badgeManager != null)
59+
{
60+
badgeManager.OnBadgeStateChanged -= HandleBadgeStateChanged;
61+
}
62+
}
63+
64+
public void OnBadgeClicked()
65+
{
66+
// Debug.LogError(gameObject.name);
67+
if (badgeManager != null)
68+
{
69+
badgeManager.ClearBadge(badgeId);
70+
}
71+
else
72+
{
73+
Debug.LogError("Can't find badge manager");
74+
}
75+
}
76+
77+
private void HandleBadgeStateChanged(BadgeID id)
78+
{
79+
// Debug.LogError("state has changed");
80+
UpdateBadge();
81+
}
82+
83+
public void UpdateBadge()
84+
{
85+
if (badgeManager == null)
86+
{
87+
Debug.LogError("Badge Manager is null");
88+
}
89+
90+
BadgeState state = badgeManager.GetBadge(badgeId);
91+
// Debug.LogError($"{gameObject.name} badge state is {state.Type.ToString()} & the active status is {state.Type != BadgeType.None}");
92+
if (badgeSymbolImage == null) { Debug.LogError($"{gameObject.name} doesn't have a badge symbol image"); return; }
93+
// Activate or deactivate the symbol image based on badge state
94+
badgeSymbolImage.gameObject.SetActive(state.Type != BadgeType.None);
95+
}
96+
}
97+
}

BadgeNotificationSystem/Core/BadgeComponent.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.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace SentryToolkit
4+
{
5+
[System.Serializable]
6+
public class BadgeState
7+
{
8+
public BadgeType Type;
9+
}
10+
11+
[System.Serializable]
12+
public class BadgeChildMapping
13+
{
14+
public BadgeID Parent;
15+
public List<BadgeID> Children;
16+
}
17+
}

BadgeNotificationSystem/Core/BadgeData.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.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using UnityEngine;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Newtonsoft.Json;
5+
using System;
6+
7+
namespace SentryToolkit
8+
{
9+
public class BadgeManager : MonoBehaviour
10+
{
11+
public static BadgeManager Instance;
12+
13+
private Dictionary<BadgeID, BadgeState> badgeStates; // Stores current badge states
14+
private Dictionary<BadgeID, List<BadgeID>> childMap; // Maps parent badges to their child badges
15+
16+
public event Action<BadgeID> OnBadgeStateChanged;
17+
18+
private void Awake()
19+
{
20+
if (Instance == null)
21+
{
22+
Instance = this;
23+
LoadBadgeStates();
24+
childMap = new Dictionary<BadgeID, List<BadgeID>>();
25+
DontDestroyOnLoad(gameObject);
26+
}
27+
else
28+
{
29+
Destroy(gameObject);
30+
}
31+
}
32+
33+
public void InitializeChildMap(List<BadgeChildMapping> mappings)
34+
{
35+
foreach (var mapping in mappings)
36+
{
37+
if (!childMap.ContainsKey(mapping.Parent))
38+
{
39+
childMap[mapping.Parent] = new List<BadgeID>();
40+
}
41+
childMap[mapping.Parent].AddRange(mapping.Children);
42+
}
43+
}
44+
45+
public void InitializeBadgesWithPropagations()
46+
{
47+
// Create a temporary list of keys to iterate over
48+
List<BadgeID> badgeKeys = new List<BadgeID>(badgeStates.Keys);
49+
50+
// Iterate over the temporary list
51+
foreach (var key in badgeKeys)
52+
{
53+
if (badgeStates.TryGetValue(key, out var state))
54+
{
55+
SetBadge(key, state);
56+
}
57+
}
58+
}
59+
60+
public void ClearChildMap()
61+
{
62+
childMap.Clear();
63+
}
64+
65+
public void SetBadge(BadgeID id, bool saveState = true)
66+
{
67+
SetBadge(id, new BadgeState { Type = BadgeType.Symbol }, saveState);
68+
}
69+
70+
public void SetBadge(BadgeID id, BadgeState state, bool saveState = true)
71+
{
72+
badgeStates[id] = state;
73+
if (saveState) { SaveBadgeStates(); }
74+
OnBadgeStateChanged?.Invoke(id);
75+
PropagateBadgeState(id, saveState);
76+
}
77+
78+
public BadgeState GetBadge(BadgeID id)
79+
{
80+
if (badgeStates.ContainsKey(id))
81+
{
82+
return badgeStates[id];
83+
}
84+
return new BadgeState { Type = BadgeType.None };
85+
}
86+
87+
public void ClearBadge(BadgeID id)
88+
{
89+
if (badgeStates.ContainsKey(id))
90+
{
91+
badgeStates.Remove(id);
92+
SaveBadgeStates();
93+
OnBadgeStateChanged?.Invoke(id);
94+
PropagateBadgeState(id, true);
95+
}
96+
}
97+
98+
private void SaveBadgeStates()
99+
{
100+
string json = JsonConvert.SerializeObject(badgeStates, Formatting.Indented);
101+
File.WriteAllText(Application.persistentDataPath + "/badgeStates.json", json);
102+
}
103+
104+
private void LoadBadgeStates()
105+
{
106+
string path = Application.persistentDataPath + "/badgeStates.json";
107+
if (File.Exists(path))
108+
{
109+
string json = File.ReadAllText(path);
110+
badgeStates = JsonConvert.DeserializeObject<Dictionary<BadgeID, BadgeState>>(json);
111+
}
112+
else
113+
{
114+
badgeStates = new Dictionary<BadgeID, BadgeState>();
115+
}
116+
}
117+
118+
private void PropagateBadgeState(BadgeID id, bool saveState)
119+
{
120+
// Propagate state to children
121+
if (childMap.TryGetValue(id, out List<BadgeID> childIds))
122+
{
123+
foreach (var childId in childIds)
124+
{
125+
PropagateBadgeState(childId, saveState);
126+
}
127+
}
128+
129+
// Check if the current badge should propagate an alert to its parent
130+
foreach (var kvp in childMap)
131+
{
132+
if (kvp.Value.Contains(id))
133+
{
134+
BadgeID parentId = kvp.Key;
135+
bool parentShouldHaveBadge = false;
136+
137+
// Check if any child badge of the parent has an active badge state
138+
foreach (var childId in kvp.Value)
139+
{
140+
if (badgeStates.ContainsKey(childId) && badgeStates[childId].Type != BadgeType.None)
141+
{
142+
parentShouldHaveBadge = true;
143+
break;
144+
}
145+
}
146+
147+
BadgeState parentState = GetBadge(parentId);
148+
if (parentShouldHaveBadge && parentState.Type == BadgeType.None)
149+
{
150+
parentState.Type = BadgeType.Symbol;
151+
SetBadge(parentId, parentState, saveState);
152+
}
153+
else if (!parentShouldHaveBadge && parentState.Type != BadgeType.None)
154+
{
155+
ClearBadge(parentId);
156+
}
157+
}
158+
}
159+
}
160+
}
161+
}

BadgeNotificationSystem/Core/BadgeManager.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.

0 commit comments

Comments
 (0)