GameManager
A basic GameManager example.
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
public class GameManager : MonoSingleton<GameManager> {
protected override bool NeverReplace => true; // If a new GameManager is created, do not replace the current
#region config
[HideInInspector] public GameConfigEditor MainConfig; // ScriptableObject containing main game configs (dev and release)
public GameConfig GameConfig {
get {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
return MainConfig.DevGameConfig;
#else
return MainConfig.ReleaseGameConfig;
#endif
}
}
#endregion
async void Start() {
// global
Cursor.visible = false;
// init game state
GameState = new GameState(); // game data
// Players manager
var pgo = new GameObject("PlayersManager");
var playerManager = pgo.AddComponent<PlayersManager>();
playerManager.PlayerPrefab = MainConfig.PlayerPrefab;
// Audio manager
var ago = new GameObject("AudioManager");
ago.AddComponent<AudioManager>();
// FSM manager
var fsmgo = new GameObject("FSMManager");
var fsmManager = fsmgo.AddComponent<FSMManager>();
fsmManager.Init(GameConfig.DefaultFSM, GameConfig.DefaultConfig, GameConfig.ListFSM);
// Init scene
SceneInit(UnityEngine.SceneManagement.SceneManager.GetActiveScene(), UnityEngine.SceneManagement.LoadSceneMode.Single);
UnityEngine.SceneManagement.SceneManager.sceneLoaded += SceneInit;
}
#if UNITY_EDITOR
int _frameCount = 0;
float _dt = 0f;
float _fps = 0f;
float _updateRate = 4f;
void Update() {
_frameCount++;
_dt += Time.deltaTime;
if (_dt > 1f / _updateRate) {
_fps = _frameCount / _dt;
_frameCount = 0;
_dt -= 1f / _updateRate;
}
Logpad.Log("Application.FPS", _fps);
}
#endif
void SceneInit(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) {
// FSM Management
FSMManager.LoadFSM(scene);
}
#region scene switch
async Task SceneSwitchCommon() {
UnityAsyncAwaitUtil.AsyncCoroutineRunner.Instance.StopAllCoroutines(); // stop all async awaits
AudioManager.SetEnvironment(null);
await new WaitForSeconds(0.1f);
}
public async Task SceneSwitch(SceneField scene) {
await SceneSwitchCommon();
scene.Load().WrapErrors();
}
// switch to included scene
public async Task SceneSwitch(int id) {
await SceneSwitchCommon();
await UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(id, UnityEngine.SceneManagement.LoadSceneMode.Single);
}
#endregion
#region scene add
public async Task SceneAdd(SceneField scene) {
await scene.Load(true);
}
public async Task SceneAdd(int id) {
await UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(id, UnityEngine.SceneManagement.LoadSceneMode.Additive);
}
#endregion
}
}