This repository has been archived on 2025-09-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
XericLibrary-OLD/Runtime/MicroLibrary/MacroObject.cs

43 lines
840 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XericLibrary.Runtime.MacroLibrary
{
public static class MacroObject
{
#region
/// <summary>
/// 无论如何都要获取一个组件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetComponentAnyway<T>(this GameObject obj)
where T : Component
{
var component = obj.GetComponent<T>();
if(component is null)
component = obj.AddComponent<T>();
return component;
}
/// <summary>
/// 无论如何都要获取一个对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T GetObjectAnyway<T>(this T obj)
where T : new()
{
if(obj is null)
obj = new T();
return obj;
}
#endregion
}
}