44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.UI;
|
|
|
|
namespace MoreMountains.Tools
|
|
{
|
|
/// <summary>
|
|
/// Game object extensions
|
|
/// </summary>
|
|
public static class GameObjectExtensions
|
|
{
|
|
static List<Component> m_ComponentCache = new List<Component>();
|
|
|
|
/// <summary>
|
|
/// Grabs a component without allocating memory uselessly
|
|
/// </summary>
|
|
/// <param name="this"></param>
|
|
/// <param name="componentType"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Grabs a component without allocating memory uselessly
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="this"></param>
|
|
/// <returns></returns>
|
|
public static T MMGetComponentNoAlloc<T>(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;
|
|
}
|
|
}
|
|
}
|