2
0

ExtentionMethods.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. namespace UnityEngine.UI.Extensions
  3. {
  4. public static class ExtentionMethods
  5. {
  6. public static T GetOrAddComponent<T>(this GameObject child) where T : Component
  7. {
  8. T result = child.GetComponent<T>();
  9. if (result == null)
  10. {
  11. result = child.AddComponent<T>();
  12. }
  13. return result;
  14. }
  15. public static bool IsPrefab(this GameObject gameObject)
  16. {
  17. if (gameObject == null)
  18. {
  19. throw new ArgumentNullException(nameof(gameObject));
  20. }
  21. return
  22. !gameObject.scene.IsValid() &&
  23. !gameObject.scene.isLoaded &&
  24. gameObject.GetInstanceID() >= 0 &&
  25. // I noticed that ones with IDs under 0 were objects I didn't recognize
  26. !gameObject.hideFlags.HasFlag(HideFlags.HideInHierarchy);
  27. // I don't care about GameObjects *inside* prefabs, just the overall prefab.
  28. }
  29. }
  30. }