-
-
Save kiranmaya/31b45e900e94d192fa1f7d30a60dc793 to your computer and use it in GitHub Desktop.
Helper methods used within Unity3d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment