Files
cppk-ar/Assets/Scripts/GameSystem.cs
T

56 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameSystem : MonoBehaviour
{
public static GameSystem Instance { get; private set; }
public List<IGameComponent> gameComponents { get; private set; } = new List<IGameComponent>();
public void AddGameComponent(IGameComponent component)
{
if(!gameComponents.Contains(component))
gameComponents.Add(component);
}
void Awake()
{
Instance = this;
}
void Start()
{
GameSystem[] gameSystems = FindObjectsOfType<GameSystem>();
if(gameSystems.Length > 1)
{
Destroy(this.gameObject);
return;
}
DontDestroyOnLoad(this);
}
void Update()
{
for(int i = 0; i < gameComponents.Count; i++)
gameComponents[i].OnUpdate();
}
void FixedUpdate()
{
for(int i = 0; i < gameComponents.Count; i++)
gameComponents[i].OnFixedUpdate();
}
void LateUpdate()
{
for(int i = 0; i < gameComponents.Count; i++)
gameComponents[i].OnLateUpdate();
}
public void RemoveGameComponent(IGameComponent component)
{
if(gameComponents.Contains(component))
gameComponents.Remove(component);
}
}