// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using Doozy.Runtime.Pooler; using UnityEngine; using UnityEngine.UIElements; namespace Doozy.Editor.EditorUI.Components { public abstract class PoolableElement : VisualElement, IPoolable where T : VisualElement, IPoolable, new() { private static readonly PoolService PoolService = new PoolService(); /// Retrieve an item from the Pool. If one does not exist, a new one will be created instead and returned. /// Print relevant debug messages to the console public static T Get(bool debug = false) { if (!debug) return PoolService.Get(); T item = PoolService.Get(); Debug.Log($"{nameof(PoolService)}.{nameof(PoolService.Get)} > [{nameof(Get)}] > {item.GetType().Name}"); return item; } /// Returns TRUE if the item is in the Pool public bool inPool { get; set; } /// /// Reset to the default settings /// Needs to be set up in a custom manner for each component type /// public abstract void Reset(); /// /// Remove from Hierarchy and Reset /// This does not add to the Pool and is intended for the GC to clean it up /// public virtual void Dispose() { RemoveFromHierarchy(); Reset(); } /// /// Remove from Hierarchy and add to the Pool /// This also triggers a Reset, on the item, prior to being added to the Pool /// /// Print relevant debug messages to the console public virtual void Recycle(bool debug = false) { if (debug) Debug.Log($"{nameof(PoolService)}.{nameof(PoolService.AddToPool)} < [{nameof(Recycle)}] < {GetType().Name}"); RemoveFromHierarchy(); PoolService.AddToPool(this as T); } } }