mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
32 lines
1.2 KiB
C#
32 lines
1.2 KiB
C#
// ---------------------------------------------------------------------------------------------
|
|
// Copyright (c) 2021-2023, Jiaqi Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
namespace Engine.Extensions
|
|
{
|
|
using UnityEngine;
|
|
|
|
public static class GameObjectExtensions
|
|
{
|
|
/// <summary>
|
|
/// Helper method to get or add a component to the GameObject.
|
|
/// </summary>
|
|
/// <param name="gameObject">this</param>
|
|
/// <typeparam name="T">Type of the component</typeparam>
|
|
/// <returns>Component</returns>
|
|
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
|
|
{
|
|
var component = gameObject.GetComponent<T>();
|
|
|
|
// 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<T>();
|
|
}
|
|
|
|
return component;
|
|
}
|
|
}
|
|
} |