using System.Collections.Generic; using System.Linq; using UnityEngine; namespace GameplayIngredients.Managers { [NonExcludeableManager] public class SingleUpdateManager : Manager { public delegate void SingleUpdate(); List updateList; private void OnEnable() { updateList = new List(); } public void Register(SingleUpdate update) { if (!updateList.Any(o => o == update)) { updateList.Add(update); } else Debug.LogWarning("SingleUpdateManager: Already found an entry for this SingleUpdate, ignoring."); } public void Remove(SingleUpdate update) { if(updateList.Any(o => o == update)) { updateList.RemoveAll(o => o == update); } else Debug.LogWarning("SingleUpdateManager: Did not found a matching entry for given SingleUpdate, cannot remove."); } public void Update() { // Process all Currently Registered (Copy as Array) foreach(var update in updateList.ToArray()) { update?.Invoke(); } // Remove all nulls (Destroyed Objects) in updateList updateList.RemoveAll(o => o == null); } } }