Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Added SetRedneringActive game object extension #178

Merged
merged 1 commit into from
May 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace XRTK.Extensions
{
Expand Down Expand Up @@ -133,5 +134,38 @@ public static void ForEachComponent<T>(this GameObject gameObject, Action<T> act
action(i);
}
}

/// <summary>
/// Sets the <see cref="Renderer"/>s and optionally <see cref="Collider"/>s in this <see cref="GameObject"/> to the provided <see cref="bool"/> state.
/// </summary>
/// <remarks>If the GameObject or it's children are inactive, they will be set active, but the GameObjects will not be set inactive.</remarks>
/// <param name="gameObject"></param>
/// <param name="isActive"></param>
/// <param name="includeColliders"></param>
public static void SetRenderingActive(this GameObject gameObject, bool isActive, bool includeColliders = true)
{
if (isActive)
{
gameObject.SetActive(true);
}

foreach (var renderer in gameObject.GetComponentsInChildren<Renderer>())
{
renderer.enabled = isActive;
}

foreach (var graphic in gameObject.GetComponentsInChildren<Graphic>())
{
graphic.enabled = isActive;
}

if (includeColliders)
{
foreach (var collider in gameObject.GetComponentsInChildren<Collider>())
{
collider.enabled = isActive;
}
}
}
}
}