using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; namespace MoreMountains.Tools { /// /// Game object extensions /// public static class GameObjectExtensions { static List m_ComponentCache = new List(); /// /// Grabs a component without allocating memory uselessly /// /// /// /// public static Component MMGetComponentNoAlloc(this GameObject @this, System.Type componentType) { @this.GetComponents(componentType, m_ComponentCache); Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null; m_ComponentCache.Clear(); return component; } /// /// Grabs a component without allocating memory uselessly /// /// /// /// public static T MMGetComponentNoAlloc(this GameObject @this) where T : Component { @this.GetComponents(typeof(T), m_ComponentCache); Component component = m_ComponentCache.Count > 0 ? m_ComponentCache[0] : null; m_ComponentCache.Clear(); return component as T; } /// /// Grabs a component on the object, or on its children objects, or on a parent, or adds it to the object if none were found /// /// /// /// public static T MMGetComponentAroundOrAdd(this GameObject @this) where T : Component { T component = @this.GetComponentInChildren(true); if (component == null) { component = @this.GetComponentInParent(); } if (component == null) { component = @this.AddComponent(); } return component; } } }