using System; using D2D.Core; using D2D.Gameplay; using D2D.Utilities; using D2D.Utils; using DG.Tweening; using UnityEngine; namespace D2D.Utilities { public static class LazySugar { /// /// Finds the lazy GameObject or creates it. /// public static T FindLazy(this MonoBehaviour target) where T: Component, ILazy { var instances = GameObject.FindObjectsOfType(); // More than 1 ILazyCreating is an error if (instances?.Length > 1) { Debug.LogError("There are more than 1 LazyCreating objects! " + "Please, find the duplicates and remove them."); } // Create automatically if object not exist if (instances is {Length: 0}) { if (CoreSettings.Instance.lazyRuntimeCreationEnabled) { string lazyName = $"{typeof(T).Name} [created at runtime]"; return new GameObject(lazyName, typeof(T)).GetComponent(); } return null; } return instances[0]; } public static T FindLazy() where T: Component, ILazy { var instances = GameObject.FindObjectsOfType(); // More than 1 ILazyCreating is an error if (instances?.Length > 1) { Debug.LogError("There are more than 1 LazyCreating objects! " + "Please, find the duplicates and remove them."); } // Create automatically if object not exist if (instances != null && instances.Length == 0) { if (CoreSettings.Instance.lazyRuntimeCreationEnabled) { string lazyName = $"{typeof(T).Name} [created at runtime]"; return new GameObject(lazyName, typeof(T)).GetComponent(); } return null; } return instances[0]; } } }