33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace RebootKit.Engine.Extensions {
|
|
public static class GameObjectEx {
|
|
public static T GetOrAdd<T>(this GameObject gameObject) where T : Component {
|
|
T component = gameObject.GetComponent<T>();
|
|
if (component == null) {
|
|
component = gameObject.AddComponent<T>();
|
|
}
|
|
|
|
return component;
|
|
}
|
|
|
|
public static void ForEachChild(this GameObject gameObject, Action<GameObject> action) {
|
|
gameObject.transform.ForEachChild(transform => action(transform.gameObject));
|
|
}
|
|
|
|
public static void DestroyChildren(this GameObject gameObject) {
|
|
gameObject.transform.DestroyChildren();
|
|
}
|
|
|
|
public static T OrNull<T>(this T instance) where T : Object {
|
|
return instance ? instance : null;
|
|
}
|
|
|
|
public static void SetLayersRecursively(this GameObject gameObject, int layer) {
|
|
gameObject.layer = layer;
|
|
gameObject.transform.ForEachChild(child => { child.gameObject.SetLayersRecursively(layer); });
|
|
}
|
|
}
|
|
} |