using System; using System.IO; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; namespace UnityExtension { public static class GameObjectExt { //------------------------------------------------------------------------------------------------------------ public static T GetOrAddComponent(this GameObject lGo) where T : Component { T lComponent = lGo.GetComponent(); if (lComponent == null) { lComponent = lGo.gameObject.AddComponent(); } return lComponent; } //------------------------------------------------------------------------------------------------------------ public static T GetComponentOfChild(this GameObject lGo, string lName) where T : Component { T lComponent = null; Transform lTransform = lGo.transform.FindChild(lName); if (lTransform != null) { lComponent = lTransform.GetComponent(); } return lComponent; } //------------------------------------------------------------------------------------------------------------ public static T[] GetComponentsOfChild(this GameObject lGo, string lName) where T : Component { T[] lComponent = null; Transform lTransform = lGo.transform.FindChild(lName); if (lTransform != null) { lComponent = lTransform.GetComponents(); } return lComponent; } //------------------------------------------------------------------------------------------------------------ public static GameObject InstantiateAsChild(this GameObject lGo, GameObject lObject) { GameObject lNewGo = lObject != null ? (GameObject)GameObject.Instantiate(lObject) : new GameObject(); lNewGo.transform.parent = lGo.transform; lNewGo.transform.localPosition = Vector3.zero; lNewGo.transform.localRotation = Quaternion.identity; lNewGo.transform.localScale = Vector3.one; return lNewGo; } //------------------------------------------------------------------------------------------------------------ public static bool DestroyChildIfExists(this GameObject lGo, string lName) { Transform lTransform = lGo.transform.FindChild(lName); if (lTransform != null) { GameObject.Destroy(lTransform.gameObject); return true; } else { return false; } } //------------------------------------------------------------------------------------------------------------ public static bool ContainsChildren(this GameObject lGo, params string[] lPaths) { for (int lCount = 0; lCount < lPaths.Length; ++lCount) { if (lGo.transform.FindChild(lPaths[lCount]) == null) { return false; } } return true; } //------------------------------------------------------------------------------------------------------------ public static bool IsChildOf(this GameObject lGo, GameObject lParent) { Transform lTransform = lGo.transform; while (lTransform.parent != null) { if (lTransform == lParent.transform) { return true; } lTransform = lTransform.parent; } return false; } //------------------------------------------------------------------------------------------------------------ public static void SetLayerRecursively(this GameObject lGo, int lLayer) { lGo.layer = lLayer; foreach (Transform lTransform in lGo.transform) { lTransform.gameObject.SetLayerRecursively(lLayer); } } } }