35 lines
940 B
C#
35 lines
940 B
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace XRLib.Util
|
|
{
|
|
public static class ComponentExtension
|
|
{
|
|
|
|
public static Component GetOrAddComponent(this Transform mono, Type type)
|
|
{
|
|
if (mono.TryGetComponent(type, out var result))
|
|
{
|
|
return result;
|
|
}
|
|
return mono.gameObject.AddComponent(type);
|
|
}
|
|
public static T GetOrAddComponent<T>(this Transform mono) where T : Component
|
|
{
|
|
if (mono.TryGetComponent<T>(out T result))
|
|
{
|
|
return result;
|
|
}
|
|
return mono.gameObject.AddComponent<T>();
|
|
}
|
|
public static T GetOrAddComponent<T>(this GameObject mono) where T : Component
|
|
{
|
|
if (mono.TryGetComponent<T>(out T result))
|
|
{
|
|
return result;
|
|
}
|
|
return mono.gameObject.AddComponent<T>();
|
|
}
|
|
|
|
}
|
|
} |