SRMonoBehaviour.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. namespace SRF
  2. {
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. /// <summary>
  6. /// Base MonoBehaviour which provides useful common functionality
  7. /// </summary>
  8. public abstract class SRMonoBehaviour : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Get the Transform component, using a cached reference if possible.
  12. /// </summary>
  13. public Transform CachedTransform
  14. {
  15. [DebuggerStepThrough]
  16. [DebuggerNonUserCode]
  17. get
  18. {
  19. if (_transform == null)
  20. {
  21. _transform = base.transform;
  22. }
  23. return _transform;
  24. }
  25. }
  26. /// <summary>
  27. /// Get the Collider component, using a cached reference if possible.
  28. /// </summary>
  29. public Collider CachedCollider
  30. {
  31. [DebuggerStepThrough]
  32. [DebuggerNonUserCode]
  33. get
  34. {
  35. if (_collider == null)
  36. {
  37. _collider = GetComponent<Collider>();
  38. }
  39. return _collider;
  40. }
  41. }
  42. /// <summary>
  43. /// Get the Collider component, using a cached reference if possible.
  44. /// </summary>
  45. public Collider2D CachedCollider2D
  46. {
  47. [DebuggerStepThrough]
  48. [DebuggerNonUserCode]
  49. get
  50. {
  51. if (_collider2D == null)
  52. {
  53. _collider2D = GetComponent<Collider2D>();
  54. }
  55. return _collider2D;
  56. }
  57. }
  58. /// <summary>
  59. /// Get the Rigidbody component, using a cached reference if possible.
  60. /// </summary>
  61. public Rigidbody CachedRigidBody
  62. {
  63. [DebuggerStepThrough]
  64. [DebuggerNonUserCode]
  65. get
  66. {
  67. if (_rigidBody == null)
  68. {
  69. _rigidBody = GetComponent<Rigidbody>();
  70. }
  71. return _rigidBody;
  72. }
  73. }
  74. /// <summary>
  75. /// Get the Rigidbody2D component, using a cached reference if possible.
  76. /// </summary>
  77. public Rigidbody2D CachedRigidBody2D
  78. {
  79. [DebuggerStepThrough]
  80. [DebuggerNonUserCode]
  81. get
  82. {
  83. if (_rigidbody2D == null)
  84. {
  85. _rigidbody2D = GetComponent<Rigidbody2D>();
  86. }
  87. return _rigidbody2D;
  88. }
  89. }
  90. /// <summary>
  91. /// Get the GameObject this behaviour is attached to, using a cached reference if possible.
  92. /// </summary>
  93. public GameObject CachedGameObject
  94. {
  95. [DebuggerStepThrough]
  96. [DebuggerNonUserCode]
  97. get
  98. {
  99. if (_gameObject == null)
  100. {
  101. _gameObject = base.gameObject;
  102. }
  103. return _gameObject;
  104. }
  105. }
  106. // Override existing getters for legacy usage
  107. // ReSharper disable InconsistentNaming
  108. public new Transform transform
  109. {
  110. get { return CachedTransform; }
  111. }
  112. #if !UNITY_5 && !UNITY_2017_1_OR_NEWER
  113. public new Collider collider
  114. {
  115. get { return CachedCollider; }
  116. }
  117. public new Collider2D collider2D
  118. {
  119. get { return CachedCollider2D; }
  120. }
  121. public new Rigidbody rigidbody
  122. {
  123. get { return CachedRigidBody; }
  124. }
  125. public new Rigidbody2D rigidbody2D
  126. {
  127. get { return CachedRigidBody2D; }
  128. }
  129. public new GameObject gameObject
  130. {
  131. get { return CachedGameObject; }
  132. }
  133. #endif
  134. // ReSharper restore InconsistentNaming
  135. private Collider _collider;
  136. private Transform _transform;
  137. private Rigidbody _rigidBody;
  138. private GameObject _gameObject;
  139. private Rigidbody2D _rigidbody2D;
  140. private Collider2D _collider2D;
  141. /// <summary>
  142. /// Assert that the value is not null, disable the object and print a debug error message if it is.
  143. /// </summary>
  144. /// <param name="value">Object to check</param>
  145. /// <param name="fieldName">Debug name to pass in</param>
  146. [DebuggerNonUserCode]
  147. [DebuggerStepThrough]
  148. protected void AssertNotNull(object value, string fieldName = null)
  149. {
  150. SRDebugUtil.AssertNotNull(value, fieldName, this);
  151. }
  152. [DebuggerNonUserCode]
  153. [DebuggerStepThrough]
  154. protected void Assert(bool condition, string message = null)
  155. {
  156. SRDebugUtil.Assert(condition, message, this);
  157. }
  158. /// <summary>
  159. /// Assert that the value is not null, disable the object and print a debug error message if it is.
  160. /// </summary>
  161. /// <param name="value">Object to check</param>
  162. /// <param name="fieldName">Debug name to pass in</param>
  163. /// <returns>True if object is not null</returns>
  164. [Conditional("UNITY_EDITOR")]
  165. [DebuggerNonUserCode]
  166. [DebuggerStepThrough]
  167. protected void EditorAssertNotNull(object value, string fieldName = null)
  168. {
  169. AssertNotNull(value, fieldName);
  170. }
  171. [Conditional("UNITY_EDITOR")]
  172. [DebuggerNonUserCode]
  173. [DebuggerStepThrough]
  174. protected void EditorAssert(bool condition, string message = null)
  175. {
  176. Assert(condition, message);
  177. }
  178. }
  179. }