Skip to content

Commit b8f35d6

Browse files
committed
fix(Scene): Switching scene flag
1 parent 7d6b337 commit b8f35d6

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

Assets/JCSUnity/Scripts/Managers/JCS_SceneManager.cs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ public class JCS_SceneManager : JCS_Manager<JCS_SceneManager>
1919
{
2020
/* Variables */
2121

22-
private bool mSwitchSceneEffect = false;
23-
private string mNextSceneName = "";
24-
2522
// Async loading scene operation. (thread)
2623
private AsyncOperation mAsyncOperation = null;
2724

2825
[Separator("Check Variables (JCS_SceneManager)")]
2926

27+
[Tooltip("Next scene name to load.")]
28+
[SerializeField]
29+
[ReadOnly]
30+
private string mNextSceneName = "";
31+
3032
[Tooltip("Black screen object to be assigned by the system.")]
3133
[SerializeField]
3234
[ReadOnly]
@@ -76,7 +78,7 @@ public class JCS_SceneManager : JCS_Manager<JCS_SceneManager>
7678
private float mSceneFadeOutTime = 1.0f;
7779

7880
// fade the sound while switching the scene.
79-
private JCS_FadeSound mJCSFadeSound = null;
81+
private JCS_FadeSound mFadeSound = null;
8082

8183
// Scene in the game so is dynamic instead of Unity's scene system's scene
8284
// Unity 自帶就有Scene這個物件. 在這個Unity Scene裡面自己宣告Scene
@@ -164,15 +166,15 @@ private void Start()
164166
if (soundS.SMOOTH_SWITCH_SOUND_BETWEEN_SCENE)
165167
{
166168
// get the component.
167-
if (mJCSFadeSound == null)
168-
mJCSFadeSound = this.gameObject.AddComponent<JCS_FadeSound>();
169+
if (mFadeSound == null)
170+
mFadeSound = this.gameObject.AddComponent<JCS_FadeSound>();
169171

170172
// set the background audio source.
171-
mJCSFadeSound.SetAudioSource(
173+
mFadeSound.SetAudioSource(
172174
soundM.GetBGMAudioSource());
173175

174176
// active the fade sound in effect.
175-
mJCSFadeSound.FadeIn(
177+
mFadeSound.FadeIn(
176178
soundS.GetBGM_Volume(),
177179
/* Fade in the sound base on the setting. */
178180
soundS.GetSoundFadeInTimeBaseOnSetting());
@@ -302,8 +304,10 @@ public void LoadScene(string sceneName, float fadeInTime, Color screenColor, boo
302304
}
303305
#endif
304306

307+
var scenes = JCS_SceneSettings.instance;
308+
305309
// if is loading already, dont load it agian
306-
if (mSwitchSceneEffect)
310+
if (scenes.SWITCHING_SCENE)
307311
return;
308312

309313
// set the next scene name
@@ -363,18 +367,18 @@ public void LoadScene(string sceneName, float fadeInTime, Color screenColor, boo
363367
if (ss.SMOOTH_SWITCH_SOUND_BETWEEN_SCENE)
364368
{
365369
// get the component.
366-
if (mJCSFadeSound == null)
367-
mJCSFadeSound = this.gameObject.AddComponent<JCS_FadeSound>();
370+
if (mFadeSound == null)
371+
mFadeSound = this.gameObject.AddComponent<JCS_FadeSound>();
368372

369-
mJCSFadeSound.SetAudioSource(JCS_SoundManager.instance.GetBGMAudioSource());
373+
mFadeSound.SetAudioSource(JCS_SoundManager.instance.GetBGMAudioSource());
370374

371375
// fade out sound to zero
372-
mJCSFadeSound.FadeOut(0, fadeInTime);
376+
mFadeSound.FadeOut(0, fadeInTime);
373377
}
374378
}
375379

376380
// start check to switch scene or not
377-
mSwitchSceneEffect = true;
381+
scenes.SWITCHING_SCENE = true;
378382
}
379383

380384
/// <summary>
@@ -476,16 +480,18 @@ public void ReloadScene(float fadeInTime, Color screenColor, bool keepBGM)
476480
/// <returns> return result. </returns>
477481
public bool IsSwitchingScene()
478482
{
479-
return this.mSwitchSceneEffect;
483+
return JCS_SceneSettings.instance.SWITCHING_SCENE;
480484
}
481485

482486
/// <summary>
483487
/// Do the async switch scene.
484488
/// </summary>
485489
private void DoSwitchScene()
486490
{
491+
var scenes = JCS_SceneSettings.instance;
492+
487493
// check if during the switch scene?
488-
if (!mSwitchSceneEffect)
494+
if (!scenes.SWITCHING_SCENE)
489495
return;
490496

491497
switch (mSwitchSceneType)
@@ -494,16 +500,16 @@ private void DoSwitchScene()
494500
{
495501
if (mBlackScreen.IsFadeIn())
496502
{
497-
// No need this anymore, since we have the
498-
// to clean up everything before we load the scene.
499-
// we need this boolean to check weather the event can
500-
// spawn new "GameObject" when "OnDestroy" function was
501-
// called in Unity.
502-
//mSwitchSceneEffect = false;
503-
504503
// load the scene if is ready
505504
mAsyncOperation.allowSceneActivation = true;
506505
}
506+
507+
bool inNewScene = (mNextSceneName == "");
508+
509+
if (inNewScene && mBlackScreen.IsFadeOut())
510+
{
511+
scenes.SWITCHING_SCENE = false;
512+
}
507513
}
508514
break;
509515

Assets/JCSUnity/Scripts/Settings/JCS_SceneSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class JCS_SceneSettings : JCS_Settings<JCS_SceneSettings>
1818
{
1919
/* Variables */
2020

21+
[Separator("Check Variables (JCS_SceneSettings)")]
22+
23+
[Tooltip("True when is switching scene.")]
24+
[ReadOnly]
25+
public bool SWITCHING_SCENE = false;
26+
2127
[Separator("Runtime Variables (JCS_SceneSettings)")]
2228

2329
[Tooltip("General Scene fadout time. (For all scene)")]
@@ -91,6 +97,8 @@ public float SceneFadeInTimeBaseOnSetting()
9197
/// <param name="_new"> new instance </param>
9298
protected override void TransferData(JCS_SceneSettings _old, JCS_SceneSettings _new)
9399
{
100+
_new.SWITCHING_SCENE = _old.SWITCHING_SCENE;
101+
94102
_new.SCENE_FADEIN_TIME = _old.SCENE_FADEIN_TIME;
95103
_new.SCENE_FADEOUT_TIME = _old.SCENE_FADEOUT_TIME;
96104
_new.SCREEN_COLOR = _old.SCREEN_COLOR;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
using UnityEngine;
1010
using UnityEngine.UI;
11+
using MyBox;
1112

1213
namespace JCSUnity
1314
{
@@ -21,7 +22,7 @@ public class JCS_SoundSlider : MonoBehaviour
2122

2223
private Slider mSlider = null;
2324

24-
[Header("** Initialize Variables (JCS_SoundSlider) **")]
25+
[Separator("Initialize Variables (JCS_SoundSlider)")]
2526

2627
[Tooltip("Sound type you would like the slider to control.")]
2728
[SerializeField]
@@ -41,6 +42,11 @@ private void Awake()
4142

4243
private void LateUpdate()
4344
{
45+
var sm = JCS_SceneManager.instance;
46+
47+
if (sm.IsSwitchingScene())
48+
return;
49+
4450
float total = mSlider.maxValue - mSlider.minValue; // Find total.
4551
float val = mSlider.value / total; // Convert to 0 to 1 scale.
4652

0 commit comments

Comments
 (0)