ObjectExtensionMethods.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using UnityEngine;
  8. namespace Live2D.Cubism.Framework
  9. {
  10. /// <summary>
  11. /// Extensions for <see cref="Object"/>s.
  12. /// </summary>
  13. internal static class ObjectExtensionMethods
  14. {
  15. /// <summary>
  16. /// Extracts an interface from an <see cref="Object"/>.
  17. /// </summary>
  18. /// <typeparam name="T">Interface type to extract.</typeparam>
  19. /// <param name="self"><see langword="this"/>.</param>
  20. /// <returns>Valid reference on success; <see langword="null"/> otherwise.</returns>
  21. public static T GetInterface<T>(this Object self) where T : class
  22. {
  23. var result = self as T;
  24. if (result != null)
  25. {
  26. return result;
  27. }
  28. // Deal with GameObjects.
  29. var gameObject = self as GameObject;
  30. if (gameObject != null)
  31. {
  32. result = gameObject.GetComponent<T>();
  33. }
  34. // Warn on error.
  35. if (self != null && result == null)
  36. {
  37. Debug.LogWarning(self + " doesn't expose requested interface of type \"" + typeof(T) + "\".");
  38. }
  39. return result;
  40. }
  41. /// <summary>
  42. /// Nulls reference in case an <see cref="Object"/> doesn't expose an interface requested.
  43. /// </summary>
  44. /// <typeparam name="T">Type of interface to check for.</typeparam>
  45. /// <param name="self"><see langword="this"/>.</param>
  46. /// <returns><paramref name="self"/> if object exposes interface; <see langword="null"/> otherwise.</returns>
  47. public static Object ToNullUnlessImplementsInterface<T>(this Object self) where T : class
  48. {
  49. var exposesInterface = self.ImplementsInterface<T>();
  50. // Warn on error.
  51. if (self != null && !exposesInterface)
  52. {
  53. Debug.LogWarning(self + " doesn't expose requested interface of type \"" + typeof(T) + "\".");
  54. }
  55. return (exposesInterface)
  56. ? self
  57. : null;
  58. }
  59. /// <summary>
  60. /// Checks whether a <see cref="Object"/> implements an interface.
  61. /// </summary>
  62. /// <typeparam name="T">Interface type to check against.</typeparam>
  63. /// <param name="self"><see langword="this"/>.</param>
  64. /// <returns><see langword="true"/> if interface is exposed; <see langword="false"/> otherwise.</returns>
  65. public static bool ImplementsInterface<T>(this Object self)
  66. {
  67. // Return early in case argument matches type.
  68. if (self is T)
  69. {
  70. return true;
  71. }
  72. // Search in components in case object is a GameObject.
  73. var gameObject = self as GameObject;
  74. if (gameObject != null)
  75. {
  76. var components = gameObject.GetComponents<T>();
  77. return components.Length > 0;
  78. }
  79. // Return on fail.
  80. return false;
  81. }
  82. }
  83. }