class MAGES::Utilities::GameObjectExtensions

Overview

Extensions for GameObject class. More…

class GameObjectExtensions
{
public:
    // methods

    static T GetOrAddComponent< T >(this GameObject gameObject);
    static Component GetOrAddComponent(this GameObject gameObject, System.Type type);

    static GameObject EnsureEditTimeComponent< T >(
        this GameObject gameObject,
        System.Action onAdd = null
    );

    static GameObject EnsurePlayTimeComponent< T >(
        this GameObject gameObject,
        System.Action onAdd = null
    );

    static GameObject EnsureAnytimeComponent< T >(
        this GameObject gameObject,
        System.Action onAdd = null
    );

    static GameObject EnsureComponent< T >(
        this GameObject gameObject,
        bool? isPlaying,
        System.Action onAdd = null
    );

    static void SafeDestroy(GameObject gameObject);
    static void SafeDestroyAny(params object[] items);
    static void SafeDestroy(Component component, bool localOnly = true);

    static Vector3 ResolveDirection(
        this GameObject gameObject,
        DirectionType direction
    );

    static Bounds GetBoundsInHierarchy(
        this GameObject gameObject,
        bool includeInactive = false,
        bool local = true
    );

    static Bounds GetCollisionBounds(
        this GameObject gameObject,
        bool includeTriggers = false,
        bool local = true
    );

    static Material[] GetApplicableMaterials(this Renderer renderer);
    static void SetApplicableMaterials(this Renderer renderer, Material[] materials);
    static void SafeStopCoroutine(this MonoBehaviour mono, ref Coroutine coro);
};

Detailed Documentation

Extensions for GameObject class.

Methods

static T GetOrAddComponent< T >(this GameObject gameObject)

Gets a component, or adds it if not found.

Parameters:

T

The component.

gameObject

The gameobject in which to search.

Returns:

The component that was either found or added.

static Component GetOrAddComponent(this GameObject gameObject, System.Type type)

Gets a component, or adds it if not found.

Parameters:

gameObject

The gameobject in which to search.

type

The component type.

Returns:

The component that was either found or added.

static GameObject EnsureEditTimeComponent< T >(
    this GameObject gameObject,
    System.Action onAdd = null
)

Gets a component, or adds it if not found in edit time only.

Parameters:

T

The component type.

gameObject

The gameobject.

onAdd

Callback when added.

Returns:

Self.

static GameObject EnsurePlayTimeComponent< T >(
    this GameObject gameObject,
    System.Action onAdd = null
)

Gets a component, or adds it if not found in play time only.

Parameters:

T

The component type.

gameObject

The gameobject.

onAdd

Callback when added.

Returns:

Self.

static GameObject EnsureAnytimeComponent< T >(
    this GameObject gameObject,
    System.Action onAdd = null
)

Gets a component, or adds it if not found.

Parameters:

T

The component type.

gameObject

The gameobject.

onAdd

Callback when added.

Returns:

Self.

static GameObject EnsureComponent< T >(
    this GameObject gameObject,
    bool? isPlaying,
    System.Action onAdd = null
)

Adds a component if the application is in the specified state.

Parameters:

T

The component type.

gameObject

The gameobject to add it to.

isPlaying

Application isPlaying.

onAdd

Callback when added.

Returns:

The gameobject.

static void SafeDestroy(GameObject gameObject)

Safely destroy a game object. Works in edit mode and play mode.

Parameters:

gameObject

The gameobject.

static void SafeDestroyAny(params object[] items)

Safely destroy any object in the list. Works in edit mode and play mode.

Parameters:

items

The objects to destroy.

static void SafeDestroy(Component component, bool localOnly = true)

Safely destroy a component. Works in edit mode and play mode.

Parameters:

component

The component.

localOnly

Destroy the object only locally.

static Vector3 ResolveDirection(
    this GameObject gameObject,
    DirectionType direction
)

Resolves a vector from a direction.

Parameters:

gameObject

The gameobject.

direction

The direction.

Returns:

The direction vector relative to that gameobject.

static Bounds GetBoundsInHierarchy(
    this GameObject gameObject,
    bool includeInactive = false,
    bool local = true
)

Computes the bounds of all renderers in the gameobject’s hierarchy.

Parameters:

gameObject

The game object.

includeInactive

Set to include/ignore inactive Renderers.

local

Set to compute bounds in local space.

Returns:

The computed bounds.

static Bounds GetCollisionBounds(
    this GameObject gameObject,
    bool includeTriggers = false,
    bool local = true
)

Computes the bounds of all colliders in the gameobject’s hierarchy.

Parameters:

gameObject

The gameobject.

includeTriggers

True to include triggers.

local

Return local space coordinates.

Returns:

The computed bounds of the colliders in the object’s hierarchy.

static Material[] GetApplicableMaterials(this Renderer renderer)

Gets either the shared or instanced materials, depending on whether the application is playing or not.

Parameters:

renderer

The renderer.

Returns:

A copy of the renderer’s materials.

static void SetApplicableMaterials(this Renderer renderer, Material[] materials)

Sets seither the shared or instanced materials, depending on whether the application is playing or not.

Parameters:

renderer

The renderer.

materials

The materials.

static void SafeStopCoroutine(this MonoBehaviour mono, ref Coroutine coro)

Safely stops a coroutine.

Parameters:

mono

The monobehavior.

coro

The coroutine variable reference.