BITFALL/Assets/BITKit/Unity/Scripts/Extensions/GameObjectExtensions.cs

22 lines
531 B
C#
Raw Normal View History

2023-10-29 15:27:13 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
public static class GameObjectExtensions
{
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
return gameObject.TryGetComponent<T>(out var t) ? t : gameObject.AddComponent<T>();
}
2023-11-15 23:54:54 +08:00
public static T RemoveComponent<T>(this GameObject gameObject) where T : Component
{
var t = gameObject.GetComponent<T>();
if (t is null) return null;
Object.Destroy(t);
return t;
}
2023-10-29 15:27:13 +08:00
}
}