Skip to content

Commit fb1f79a

Browse files
committed
refactor(Manager): Improve register event on after init
1 parent 4a95d1c commit fb1f79a

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

Assets/JCSUnity/Scripts/Managers/JCS_AppManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void Awake()
158158

159159
private void Start()
160160
{
161-
JCS_GameManager.instance.onSystemAfterInitialize += RefreshSimulateLanguage;
161+
JCS_GameManager.instance.RegisterOnSystemAfterInit(RefreshSimulateLanguage);
162162
}
163163

164164
private void OnValidate()

Assets/JCSUnity/Scripts/Managers/JCS_GameManager.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public class JCS_GameManager : JCS_Manager<JCS_GameManager>
2020
/* Variables */
2121

2222
// Callback after the game is done initialize. (system used)
23-
public Action onSystemAfterInitialize = null;
23+
private Action mOnSystemAfterInitialize = null;
2424

2525
// Callback after the game is done initialize.
26-
public Action onAfterInitialize = null;
26+
private Action mOnAfterInitialize = null;
2727

2828
[Separator("Check Variable (JCS_GameManager)")]
2929

@@ -69,6 +69,39 @@ private void Awake()
6969
Invoke("OnFirstFrame", 0.0f);
7070
}
7171

72+
/// <summary>
73+
/// Register event run on the first frame of the game.
74+
/// </summary>
75+
public void RegisterOnAfterInit(Action action)
76+
{
77+
// Already initialize, just execute it.
78+
if (mDoneInitialize)
79+
{
80+
action?.Invoke();
81+
return;
82+
}
83+
84+
mOnAfterInitialize += action;
85+
}
86+
87+
/// <summary>
88+
/// Register event run on the first frame of the game.
89+
/// </summary>
90+
public void RegisterOnSystemAfterInit(Action action)
91+
{
92+
// Already initialize, just execute it.
93+
if (mDoneInitialize)
94+
{
95+
action?.Invoke();
96+
return;
97+
}
98+
99+
mOnSystemAfterInitialize += action;
100+
}
101+
102+
/// <summary>
103+
/// Run only once on the first frame.
104+
/// </summary>
72105
private void OnFirstFrame()
73106
{
74107
SetDoneInitializeFlag();
@@ -99,12 +132,9 @@ private void SetDoneInitializeFlag()
99132

100133
this.mDoneInitialize = true;
101134

102-
if (onSystemAfterInitialize != null)
103-
onSystemAfterInitialize.Invoke();
135+
mOnSystemAfterInitialize?.Invoke();
104136

105-
if (onAfterInitialize != null)
106-
onAfterInitialize.Invoke();
137+
mOnAfterInitialize?.Invoke();
107138
}
108139
}
109140
}
110-

Assets/JCSUnity/Scripts/Settings/JCS_AppSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void Start()
7373
{
7474
// If already starts we don't need to enable the flag.
7575
if (!APPLICATION_STARTS)
76-
JCS_GameManager.instance.onAfterInitialize += DoApplicationStart;
76+
JCS_GameManager.instance.RegisterOnAfterInit(DoApplicationStart);
7777
}
7878

7979
/// <summary>

Assets/JCSUnity/Scripts/UI/Slider/JCS_SoundSlider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void Start()
5454
if (mMixer == null)
5555
mMixer = JCS_SoundSettings.instance.MIXER;
5656

57-
AddListener();
57+
JCS_GameManager.instance.RegisterOnAfterInit(AddListener);
5858
}
5959

6060
private void OnValidate()

Assets/JCSUnity/Scripts/UI/Undo Redo/JCS_UndoRedoComponent.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class JCS_UndoRedoComponent : JCS_GUIObject
3838
[SerializeField]
3939
private JCS_KeyWith mRedoKey = new JCS_KeyWith
4040
{
41-
comb = JCS_KeyCombination.ALT,
41+
comb = JCS_KeyCombination.ALT,
4242
key = KeyCode.S,
4343
};
4444
#endif
@@ -170,9 +170,9 @@ private void Start()
170170
// to be as late as possible. Cuz any script change
171171
// the UI value after this will not be record...
172172
{
173-
JCS_GameManager gm = JCS_GameManager.instance;
173+
var gm = JCS_GameManager.instance;
174174

175-
gm.onAfterInitialize += RecordPrevData;
175+
gm.RegisterOnAfterInit(RecordPrevData);
176176
}
177177
}
178178

@@ -897,7 +897,9 @@ private void DoFocusAfterUndoRedoAction()
897897
case JCS_GUIType.TOGGLE:
898898
{
899899
if (mToggle != null)
900+
{
900901
mToggle.Select();
902+
}
901903
else if (mSwitch != null)
902904
{
903905
// empty..

0 commit comments

Comments
 (0)