using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace MeadowGames.UINodeConnect4 { [DefaultExecutionOrder(-20)] [ExecuteInEditMode] public class UICSystemManager : MonoBehaviour { [SerializeField] static UICSystemManager _instance; public static UICSystemManager Instance { get { if (_instance == null) { _instance = FindObjectOfType(); } return _instance; } set => _instance = value; } [SerializeField] List _nodes = new List(); public static List Nodes { get { if (Instance && Instance._nodes == null) Instance._nodes = new List(); return Instance?._nodes; } } [SerializeField] List _connections = new List(); public static List Connections { get => Instance ? Instance._connections : new List(); } [SerializeField] bool _cacheRaycasters = true; public bool CacheRaycasters { get => _cacheRaycasters; set { raycasterList = new List(); if (value == true) { raycasterList.AddRange(FindObjectsOfType()); } _cacheRaycasters = value; } } public static List raycasterList = new List(); public static void AddNodeToList(Node node) { if (Instance && !Nodes.Contains(node)) { Nodes.Add(node); UICEvents.TriggerEvent(UICEventType.NodeAdded, node); } } public static void RemoveNodeFromList(Node node) { if (Instance && Nodes.Contains(node)) { Nodes.Remove(node); UICEvents.TriggerEvent(UICEventType.NodeRemoved, node); } } public static void AddConnectionToList(Connection connection) { if (!Connections.Contains(connection)) { Connections.Add(connection); UICEvents.TriggerEvent(UICEventType.ConnectionAdded, connection); // v4.1 - bugfix: - connection line position not updating when added using UICSystemManager.AddConnectionToList connection.UpdateLine(true); } } public static void RemoveConnectionFromList(Connection connection) { if (Connections.Contains(connection)) { Connections.Remove(connection); UICEvents.TriggerEvent(UICEventType.ConnectionRemoved, connection); } } // list of selected elements, used for single or multi selection public static List selectedElements = new List(); public static IElement clickedElement; public static IElement hoverElement; static EventManager _uicEvents; public static EventManager UICEvents { get { if (_uicEvents == null) _uicEvents = new EventManager(); return _uicEvents; } } public static void UpdateNodeList() { UICSystemManager.Instance._nodes = new List(); Nodes.AddRange(FindObjectsOfType()); } void OnEnable() { CacheRaycasters = _cacheRaycasters; selectedElements = new List(); // ensures single instance if (Instance != this) { #if UNITY_EDITOR if (Application.isPlaying) #endif GameObject.Destroy(gameObject); #if UNITY_EDITOR else GameObject.DestroyImmediate(gameObject); #endif } UpdateNodeList(); } public static List graphManagers = new List(); void Start() { // initialize the editor mode connections for (int i = 0; i < Connections.Count; i++) { Connections[i].UpdateLine(); Connections[i].OnPointerUp(); } } void Update() { e_OnUpdate.Invoke(); #if UNITY_EDITOR if (!Application.isPlaying) { for (int i = 0; i < Connections.Count; i++) { Connections[i].UpdateLine(); } } #endif } static UnityEvent e_OnUpdate = new UnityEvent(); static List actions = new List(); public static void AddToUpdate(UnityAction action) { if (!actions.Contains(action)) { e_OnUpdate.AddListener(action); actions.Add(action); } } public static void RemoveFromUpdate(UnityAction action) { if (actions.Contains(action)) { e_OnUpdate.RemoveListener(action); actions.Remove(action); } } } }