Files
RebootKit/Runtime/Engine/Code/Extensions/GameObjectEx.cs

30 lines
939 B
C#

using UnityEngine;
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 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);
});
}
}
}