Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. namespace Utility
  5. {
  6. /// <summary>
  7. /// A safe, drop-in replacement for MonoBehaviour as your base class. Each property value is cached
  8. /// and GetComponent will be called if the instance is null or is destroyed.
  9. /// </summary>
  10. public abstract class CacheBehaviour : MonoBehaviour
  11. {
  12. [HideInInspector, NonSerialized]
  13. private Animation _animation;
  14.  
  15. /// <summary>
  16. /// Gets the Animation attached to the object.
  17. /// </summary>
  18. public new Animation animation { get { return _animation ? _animation : (_animation = GetComponent<Animation>()); } }
  19.  
  20. [HideInInspector, NonSerialized]
  21. private AudioSource _audio;
  22.  
  23. /// <summary>
  24. /// Gets the AudioSource attached to the object.
  25. /// </summary>
  26. public new AudioSource audio { get { return _audio ? _audio : (_audio = GetComponent<AudioSource>()); } }
  27.  
  28. [HideInInspector, NonSerialized]
  29. private Camera _camera;
  30.  
  31. /// <summary>
  32. /// Gets the Camera attached to the object.
  33. /// </summary>
  34. public new Camera camera { get { return _camera ? _camera : (_camera = GetComponent<Camera>()); } }
  35.  
  36. [HideInInspector, NonSerialized]
  37. private Collider _collider;
  38.  
  39. /// <summary>
  40. /// Gets the Collider attached to the object.
  41. /// </summary>
  42. public new Collider collider { get { return _collider ? _collider : (_collider = GetComponent<Collider>()); } }
  43.  
  44. [HideInInspector, NonSerialized]
  45. private Collider2D _collider2D;
  46.  
  47. /// <summary>
  48. /// Gets the Collider2D attached to the object.
  49. /// </summary>
  50. public new Collider2D collider2D { get { return _collider2D ? _collider2D : (_collider2D = GetComponent<Collider2D>()); } }
  51.  
  52. [HideInInspector, NonSerialized]
  53. private ConstantForce _constantForce;
  54.  
  55. /// <summary>
  56. /// Gets the ConstantForce attached to the object.
  57. /// </summary>
  58. public new ConstantForce constantForce { get { return _constantForce ? _constantForce : (_constantForce = GetComponent<ConstantForce>()); } }
  59.  
  60. [HideInInspector, NonSerialized]
  61. private GUIText _guiText;
  62.  
  63. /// <summary>
  64. /// Gets the GUIText attached to the object.
  65. /// </summary>
  66. public new GUIText guiText { get { return _guiText ? _guiText : (_guiText = GetComponent<GUIText>()); } }
  67.  
  68. [HideInInspector, NonSerialized]
  69. private GUITexture _guiTexture;
  70.  
  71. /// <summary>
  72. /// Gets the GUITexture attached to the object.
  73. /// </summary>
  74. public new GUITexture guiTexture { get { return _guiTexture ? _guiTexture : (_guiTexture = GetComponent<GUITexture>()); } }
  75.  
  76. [HideInInspector, NonSerialized]
  77. private HingeJoint _hingeJoint;
  78.  
  79. /// <summary>
  80. /// Gets the HingeJoint attached to the object.
  81. /// </summary>
  82. public new HingeJoint hingeJoint { get { return _hingeJoint ? _hingeJoint : (_hingeJoint = GetComponent<HingeJoint>()); } }
  83.  
  84. [HideInInspector, NonSerialized]
  85. private Light _light;
  86.  
  87. /// <summary>
  88. /// Gets the Light attached to the object.
  89. /// </summary>
  90. public new Light light { get { return _light ? _light : (_light = GetComponent<Light>()); } }
  91.  
  92. [HideInInspector, NonSerialized]
  93. private NetworkView _networkView;
  94.  
  95. /// <summary>
  96. /// Gets the NetworkView attached to the object.
  97. /// </summary>
  98. public new NetworkView networkView { get { return _networkView ? _networkView : (_networkView = GetComponent<NetworkView>()); } }
  99.  
  100. [HideInInspector, NonSerialized]
  101. private ParticleSystem _particleSystem;
  102.  
  103. /// <summary>
  104. /// Gets the ParticleSystem attached to the object.
  105. /// </summary>
  106. public new ParticleSystem particleSystem { get { return _particleSystem ? _particleSystem : (_particleSystem = GetComponent<ParticleSystem>()); } }
  107.  
  108. [HideInInspector, NonSerialized]
  109. private Renderer _renderer;
  110.  
  111. /// <summary>
  112. /// Gets the Renderer attached to the object.
  113. /// </summary>
  114. public new Renderer renderer { get { return _renderer ? _renderer : (_renderer = GetComponent<Renderer>()); } }
  115.  
  116. [HideInInspector, NonSerialized]
  117. private Rigidbody _rigidbody;
  118.  
  119. /// <summary>
  120. /// Gets the Rigidbody attached to the object.
  121. /// </summary>
  122. public new Rigidbody rigidbody { get { return _rigidbody ? _rigidbody : (_rigidbody = GetComponent<Rigidbody>()); } }
  123.  
  124. [HideInInspector, NonSerialized]
  125. private Rigidbody2D _rigidbody2D;
  126.  
  127. /// <summary>
  128. /// Gets the Rigidbody2D attached to the object.
  129. /// </summary>
  130. public new Rigidbody2D rigidbody2D { get { return _rigidbody2D ? _rigidbody2D : (_rigidbody2D = GetComponent<Rigidbody2D>()); } }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement