// 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 System; using System.Linq; using Doozy.Runtime.Pooler; using UnityEditor.UIElements; using UnityEngine.UIElements; namespace Doozy.Editor.UIElements { public static class VisualElementExtensions { /// Checks all the target's Children if they are IPoolable (and calls Recycle()) /// Target VisualElement /// If TRUE, calls Clear() on the target after checking its Children public static T RecycleIPoolableChildren(this T target, bool clear = false) where T : VisualElement { foreach (VisualElement child in target.Children().ToList()) { switch (child) { case null: continue; case IPoolable poolable: poolable.Recycle(); break; } } if(clear) target.Clear(); return target; } /// Checks all the target's Children if they are IDisposable (and calls Dispose()) /// Target VisualElement /// /// If TRUE, calls Clear() on the target after checking its Children public static T DisposeIDisposableChildren(this T target, bool clear = false) where T : VisualElement { foreach (VisualElement child in target.Children().ToList()) { switch (child) { case null: continue; case IDisposable disposable: disposable.Dispose(); break; } } if(clear) target.Clear(); return target; } /// Checks all the target's Children if they are IPoolable (calls Recycle()) or IDisposable (calls Dispose()) and then calls Clear() on the target /// Target VisualElement public static T RecycleAndClear(this T target) where T : VisualElement { foreach (VisualElement child in target.Children().ToList()) { switch (child) { case null: continue; case IPoolable poolable: poolable.Recycle(); continue; case IDisposable disposable: disposable.Dispose(); break; } } target.Clear(); return target; } #region ObjectField public static T SetObjectType(this T target, Type objectType) where T : ObjectField { target.objectType = objectType; return target; } public static T SetAllowSceneObjects(this T target, bool allowSceneObjects) where T : ObjectField { target.allowSceneObjects = allowSceneObjects; return target; } #endregion } }