Last active
April 22, 2023 04:18
-
-
Save JVinceW/e36dcf675e2e3fdd449087fdb4e68b53 to your computer and use it in GitHub Desktop.
Application Main Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.SceneManagement; | |
namespace BrkLib.Scripts.Core { | |
[SuppressMessage("ReSharper", "UseNullPropagation")] | |
public class AppManager : SingletonMonoBehaviour<AppManager> { | |
[SerializeField] | |
private float _defaultTimeScale = 1f; | |
[Range(0, 10)] | |
[SerializeField] | |
private float _timeScale = 1f; | |
[SerializeField] | |
private string _defaultScenePath; | |
private List<IOnAppLaunch> _onAppLaunchLst = new List<IOnAppLaunch>(); | |
private List<IOnAppQuit> _onAppQuitLst = new List<IOnAppQuit>(); | |
private List<IOnAppPaused> _onAppPauseLst = new List<IOnAppPaused>(); | |
private void Start() { | |
// Load the default scene | |
StartCoroutine(ExecuteLauncherCallback()); | |
} | |
protected virtual IEnumerator ExecuteLauncherCallback() { | |
yield return new WaitForEndOfFrame(); | |
foreach (var launcher in _onAppLaunchLst) { | |
if (launcher != null) { | |
launcher.OnAppLaunch(); | |
} | |
} | |
} | |
private void Update() { | |
if (!Mathf.Approximately(Time.timeScale, _timeScale)) { | |
Time.timeScale = _timeScale; | |
} | |
} | |
private void OnApplicationQuit() { | |
foreach (var quit in _onAppQuitLst) { | |
if (quit != null) { | |
quit.OnAppQuit(); | |
} | |
} | |
// Clean up quit list | |
_onAppQuitLst = _onAppQuitLst.Where(x => x != null).ToList(); | |
} | |
private void OnApplicationPause(bool pauseStatus) { | |
foreach (var callback in _onAppPauseLst) { | |
if (callback != null) { | |
callback.OnAppPaused(pauseStatus); | |
} | |
} | |
// Clean up the null object | |
_onAppLaunchLst = _onAppLaunchLst.Where(x => x != null).ToList(); | |
} | |
#region Application public process | |
/// <summary> | |
/// Setting App time scale | |
/// </summary> | |
/// <param name="timeScale">Time scale value</param> | |
public void SetUpAppTimeScale(float timeScale) { | |
_timeScale = timeScale; | |
} | |
/// <summary> | |
/// Reset app timescale | |
/// </summary> | |
public void ResetTimeScale() { | |
Time.timeScale = _defaultTimeScale; | |
} | |
/// <summary> | |
/// Add callback which will called when application finished load first frame | |
/// </summary> | |
public void AddOnAppLaunch(IOnAppLaunch onAppLaunch) { | |
_onAppLaunchLst.Add(onAppLaunch); | |
} | |
/// <summary> | |
/// Add callback which will called when application paused or resumed | |
/// </summary> | |
public void AddOnAppPaused(IOnAppPaused onAppPaused) { | |
_onAppPauseLst.Add(onAppPaused); | |
} | |
/// <summary> | |
/// Add callback which will called when application quited | |
/// </summary> | |
public void AddOnAppQuit(IOnAppQuit onAppQuit) { | |
_onAppQuitLst.Add(onAppQuit); | |
} | |
#endregion | |
} | |
/// <summary> | |
/// Interface that will be call when application launched. | |
/// THIS INTERFACE SHOULD BE USE WITH THE SINGLETON OBJECT ONLY. THIS INTERFACE WILL BE CALL ONLY ONE TIME SINCE APP START | |
/// (Best fit for the init process of singleton object) | |
/// <seealso cref="AppManager.AddOnAppLaunch"/> | |
/// </summary> | |
public interface IOnAppLaunch { | |
void OnAppLaunch(); | |
} | |
/// <summary> | |
/// Interface that will be call when application paused or resumed. | |
/// <seealso cref="AppManager.AddOnAppPaused"/> | |
/// </summary> | |
public interface IOnAppPaused { | |
void OnAppPaused(bool isPaused); | |
} | |
/// <summary> | |
/// Interface that will be call before application completely quit. | |
/// <seealso cref="AppManager.AddOnAppQuit"/> | |
/// </summary> | |
public interface IOnAppQuit { | |
void OnAppQuit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment