91 lines
2.3 KiB
C#
91 lines
2.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class GameSystem : MonoBehaviour
|
|
{
|
|
public static GameSystem Instance { get; private set; }
|
|
public List<IGameComponent> gameComponents { get; private set; } = new List<IGameComponent>();
|
|
|
|
[SerializeField] GameObject[] sigletonComponents;
|
|
[SerializeField] CanvasGroup loadingCanvas;
|
|
|
|
public event Action OnSceneLoaded;
|
|
|
|
|
|
public void LoadScene(string sceneName)
|
|
{
|
|
if(!loadingCanvas.gameObject.activeSelf)
|
|
loadingCanvas.gameObject.SetActive(true);
|
|
|
|
loadingCanvas.DOFade(1, .5f).OnComplete(async() =>
|
|
{
|
|
var qrReader = FindObjectOfType<QRCodeReader>();
|
|
qrReader.camTexture.Stop();
|
|
|
|
var sceneLoadingOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
|
|
while(!sceneLoadingOperation.isDone) await Task.Yield();
|
|
|
|
OnSceneLoaded?.Invoke();
|
|
loadingCanvas.gameObject.SetActive(false);
|
|
});
|
|
}
|
|
|
|
|
|
public void AddGameComponent(IGameComponent component)
|
|
{
|
|
if(!gameComponents.Contains(component))
|
|
gameComponents.Add(component);
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
foreach(var item in sigletonComponents)
|
|
DontDestroyOnLoad(item.gameObject);
|
|
|
|
Instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
GameSystem[] gameSystems = FindObjectsOfType<GameSystem>();
|
|
if(gameSystems.Length > 1)
|
|
{
|
|
Destroy(this.gameObject);
|
|
return;
|
|
}
|
|
|
|
DontDestroyOnLoad(this);
|
|
QRCodeReader.Instance.OnQRCodeRead += LoadScene;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
Parallel.ForEach(gameComponents,
|
|
component => component.OnUpdate());
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
Parallel.ForEach(gameComponents,
|
|
component => component.OnFixedUpdate());
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
Parallel.ForEach(gameComponents,
|
|
component => component.OnLateUpdate());
|
|
}
|
|
|
|
public void RemoveGameComponent(IGameComponent component)
|
|
{
|
|
if(gameComponents.Contains(component))
|
|
gameComponents.Remove(component);
|
|
}
|
|
} |