// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2023, Jiaqi Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Core.Extensions
{
using UnityEngine;
public static class GameObjectExtension
{
///
/// Helper method to get or add a component to the GameObject.
///
/// this
/// Type of the component
/// Component
public static T GetOrAddComponent(this GameObject gameObject) where T : Component
{
var component = gameObject.GetComponent();
// Since ?? operation does not work well with UnityObject
// so I have to use the old fashion here checking if it is null.
if (component == null)
{
component = gameObject.AddComponent();
}
return component;
}
}
}