78 lines
1.9 KiB
C#
78 lines
1.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
|
|
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;
|
|
public event Action OnSceneLoaded;
|
|
|
|
|
|
public async void LoadScene(string sceneName)
|
|
{
|
|
var sceneLoadingOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
|
|
while(!sceneLoadingOperation.isDone) await Task.Yield();
|
|
|
|
OnSceneLoaded?.Invoke();
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
} |