Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Last active January 25, 2022 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TarasOsiris/488261cce325a62cdcb0 to your computer and use it in GitHub Desktop.
Save TarasOsiris/488261cce325a62cdcb0 to your computer and use it in GitHub Desktop.
Helper methods used within Unity3d
using UnityEngine;
public static class UnityExtensionMethods
{
#region go_utils
public static void Activate(this GameObject go)
{
go.SetActive(true);
}
public static void Dectivate(this GameObject go)
{
go.SetActive(false);
}
public static void ActivateGO(this MonoBehaviour mb)
{
mb.gameObject.SetActive(true);
}
public static void DectivateGO(this MonoBehaviour mb)
{
mb.gameObject.SetActive(false);
}
#endregion
#region children_manipulation
public static void DestroyAllChildren(this GameObject parent)
{
foreach (Transform childTransform in parent.transform)
{
Object.Destroy(childTransform.gameObject);
}
}
public static void ActivateAllChildren(this GameObject go, bool activate)
{
SetAllChildrenActive(go, activate);
}
public static int GetActiveChildCount(this Transform transform)
{
int activeChildCount = 0;
int childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
if (transform.GetChild(i).gameObject.activeSelf) { activeChildCount++; }
}
return activeChildCount;
}
private static void SetAllChildrenActive(GameObject go, bool active)
{
int childCount = go.transform.childCount;
for (int i = 0; i < childCount; i++)
{
go.transform.GetChild(i).gameObject.SetActive(active);
}
}
#endregion
#region cloning
public static T Clone<T>(this T unityObj) where T : Object
{
return Object.Instantiate(unityObj) as T;
}
public static T Clone<T>(this T unityObj, Vector3 position, Quaternion rotation) where T : Object
{
return Object.Instantiate(unityObj, position, rotation) as T;
}
#endregion
}
@dan-oak
Copy link

dan-oak commented Jan 25, 2022

simple and clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment