2020-10-23 19:04:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2018-10-04 05:19:56 +08:00
|
|
|
|
{
|
2018-10-04 05:25:09 +08:00
|
|
|
|
public static class ExtentionMethods
|
2018-10-04 05:19:56 +08:00
|
|
|
|
{
|
2018-10-04 05:25:09 +08:00
|
|
|
|
public static T GetOrAddComponent<T>(this GameObject child) where T : Component
|
2018-10-04 05:19:56 +08:00
|
|
|
|
{
|
2018-10-04 05:25:09 +08:00
|
|
|
|
T result = child.GetComponent<T>();
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
result = child.AddComponent<T>();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2018-10-04 05:19:56 +08:00
|
|
|
|
}
|
2020-10-23 19:04:11 +08:00
|
|
|
|
|
|
|
|
|
public static bool IsPrefab(this GameObject gameObject)
|
|
|
|
|
{
|
|
|
|
|
if (gameObject == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(gameObject));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
!gameObject.scene.IsValid() &&
|
|
|
|
|
!gameObject.scene.isLoaded &&
|
|
|
|
|
gameObject.GetInstanceID() >= 0 &&
|
|
|
|
|
// I noticed that ones with IDs under 0 were objects I didn't recognize
|
|
|
|
|
!gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy);
|
|
|
|
|
// I don't care about GameObjects *inside* prefabs, just the overall prefab.
|
|
|
|
|
}
|
2018-10-04 05:19:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|