// 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.Common.Layouts; using UnityEngine; using UnityEngine.UI; namespace Doozy.Runtime.Common.Utils { public static class LayoutUtils { /// Returns TRUE if the target RectTransform's parent is a LayoutGroup /// Target RectTransform public static bool IsInLayoutGroup(this RectTransform target) { if (!target.GetLayoutGroupInParent()) return false; LayoutElement layoutElement = target.GetComponent(); if (layoutElement == null) return true; return layoutElement.ignoreLayout == false; } /// Returns TRUE if the target RectTransform's contains a LayoutGroup /// Target RectTransform public static bool ContainsLayoutGroup(this RectTransform target) => target.GetComponentInChildren(); /// Returns the LayoutGroup of the target RectTransform. Returns null if there is no LayoutGroup /// Target RectTransform public static LayoutGroup GetLayoutGroupInParent(this RectTransform target) => target.parent != null ? target.parent.GetComponent() : null; /// /// Returns the UIBehaviourHandler attached to the target RectTransform. /// If one does not exist, it gets automatically added. /// /// Target RectTransform public static UIBehaviourHandler GetUIBehaviourHandler(this RectTransform target) { UIBehaviourHandler handler = target.GetComponent() ?? target.gameObject.AddComponent(); return handler; } /// /// Returns the UIBehaviourHandler attached to the target LayoutGroup. /// If one does not exist, it gets automatically added. /// /// Target LayoutGroup public static UIBehaviourHandler GetUIBehaviourHandler(this LayoutGroup target) { UIBehaviourHandler handler = target.GetComponent() ?? target.gameObject.AddComponent(); return handler; } } }