mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
Update UnityExtension.cs
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine;
|
||||
using UnityEngineInternal;
|
||||
|
||||
/// <summary>
|
||||
/// 对 Unity 的扩展方法。
|
||||
@@ -18,11 +20,13 @@ public static class UnityExtension
|
||||
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
|
||||
{
|
||||
T component = gameObject.GetComponent<T>();
|
||||
|
||||
if (component == null)
|
||||
{
|
||||
component = gameObject.AddComponent<T>();
|
||||
}
|
||||
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
@@ -35,6 +39,7 @@ public static class UnityExtension
|
||||
public static Component GetOrAddComponent(this GameObject gameObject, Type type)
|
||||
{
|
||||
Component component = gameObject.GetComponent(type);
|
||||
|
||||
if (component == null)
|
||||
{
|
||||
component = gameObject.AddComponent(type);
|
||||
@@ -42,6 +47,40 @@ public static class UnityExtension
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除组件。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">目标对象。</param>
|
||||
/// <param name="type">要获取或增加的组件类型。</param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
[TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
|
||||
public static void RemoveMonoBehaviour(this GameObject gameObject,[NotNull] Type type)
|
||||
{
|
||||
if (type == null) throw new ArgumentNullException(nameof(type));
|
||||
|
||||
Component component = gameObject.GetComponent(type);
|
||||
|
||||
if (component != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(component);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除组件。
|
||||
/// </summary>
|
||||
/// <param name="gameObject">目标对象。</param>
|
||||
/// <typeparam name="T">要获取或增加的组件类型。</typeparam>
|
||||
public static void RemoveMonoBehaviour<T>(this GameObject gameObject) where T : Component
|
||||
{
|
||||
T component = gameObject.GetComponent<T>();
|
||||
|
||||
if (component != null)
|
||||
{
|
||||
UnityEngine.Object.Destroy(component);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 GameObject 是否在场景中。
|
||||
|
Reference in New Issue
Block a user