Deozaan

MonoBehavior.cs

Jan 8th, 2013
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public abstract class MonoBehavior : MonoBehaviour {
  4.     // private cached variables
  5.     private GameObject _gameObject;
  6.     private Transform _transform;
  7.     private Rigidbody _rigidbody;
  8.     private Renderer _renderer;
  9.     private Animation _animation;
  10.     private Animator _animator;
  11.     private AudioSource _audio;
  12.     private Collider _collider;
  13.     private Camera _camera;
  14.     private Light _light;
  15.     private Rigidbody2D _rigidbody2D;
  16.     private Collider2D _collider2D;
  17.  
  18.     #region public getter properties
  19.     public new GameObject gameObject {
  20.         get {
  21.             if (_gameObject == null) {
  22.                 _gameObject = base.gameObject;
  23.             }
  24.  
  25.             return _gameObject;
  26.         }
  27.     }
  28.     public new Transform transform {
  29.         get {
  30.             if (_transform == null) {
  31.                 _transform = base.transform;
  32.             }
  33.  
  34.             return _transform;
  35.         }
  36.     }
  37.     public new Rigidbody rigidbody {
  38.         get {
  39.             if (_rigidbody == null) {
  40.                 // get the attached rigidbody if one exists
  41.                 _rigidbody = GetComponent<Rigidbody>();
  42.                 if (_rigidbody == null && collider != null) {
  43.                     _rigidbody = collider.attachedRigidbody;
  44.                 }
  45.             }
  46.  
  47.             return _rigidbody;
  48.         }
  49.     }
  50.     public new Renderer renderer {
  51.         get {
  52.             if (_renderer == null) {
  53.                 _renderer = GetComponent<Renderer>();
  54.             }
  55.  
  56.             return _renderer;
  57.         }
  58.     }
  59.     public new Animation animation {
  60.         get {
  61.             if (_animation == null) {
  62.                 _animation = GetComponent<Animation>();
  63.             }
  64.  
  65.             return _animation;
  66.         }
  67.     }
  68.     public Animator animator {
  69.         get {
  70.             if (_animator == null) {
  71.                 _animator = GetComponent<Animator>();
  72.             }
  73.  
  74.             return _animator;
  75.         }
  76.     }
  77.     public new AudioSource audio {
  78.         get {
  79.             if (_audio == null) {
  80.                 _audio = GetComponent<AudioSource>();
  81.             }
  82.  
  83.             return _audio;
  84.         }
  85.     }
  86.     public new Collider collider {
  87.         get {
  88.             if (_collider == null) {
  89.                 _collider = GetComponent<Collider>();
  90.             }
  91.  
  92.             return _collider;
  93.         }
  94.     }
  95.     public new Camera camera {
  96.         get {
  97.             if (_camera == null) {
  98.                 _camera = GetComponent<Camera>();
  99.             }
  100.  
  101.             return _camera;
  102.         }
  103.     }
  104.     public new Light light {
  105.         get {
  106.             if (_light == null) {
  107.                 _light = GetComponent<Light>();
  108.             }
  109.  
  110.             return _light;
  111.         }
  112.     }
  113.     public new Rigidbody2D rigidbody2D {
  114.         get {
  115.             if (_rigidbody2D == null) {
  116.                 // get the attached rigidbody if one exists
  117.                 _rigidbody2D = GetComponent<Rigidbody2D>();
  118.                 if (_rigidbody2D == null && collider2D != null) {
  119.                     _rigidbody2D = collider2D.attachedRigidbody;
  120.                 }
  121.             }
  122.  
  123.             return _rigidbody2D;
  124.         }
  125.     }
  126.     public new Collider2D collider2D {
  127.         get {
  128.             if (_collider2D == null) {
  129.                 _collider2D = GetComponent<Collider2D>();
  130.             }
  131.  
  132.             return _collider2D;
  133.         }
  134.     }
  135.     #endregion //public getter properties
  136.  
  137.     #region Typesafe instantiation
  138. #if UNITY_3 || UNITY_4
  139.     // Unity 5 includes this now.
  140.     public static T Instantiate<T>(T obj) where T : Component {
  141.         return (T)Object.Instantiate(obj);
  142.     }
  143. #endif
  144.  
  145.     public static T Instantiate<T>(T obj, Vector3 position, Quaternion rotation) where T : Component {
  146.         return (T)Object.Instantiate(obj, position, rotation);
  147.     }
  148.  
  149.     public static GameObject Instantiate(GameObject obj) {
  150.         return (GameObject)Object.Instantiate(obj);
  151.     }
  152.  
  153.     public static GameObject Instantiate(GameObject obj, Vector3 position, Quaternion rotation) {
  154.         return (GameObject)Object.Instantiate(obj, position, rotation);
  155.     }
  156.     #endregion //Typesafe instantiation
  157. }
Advertisement
Add Comment
Please, Sign In to add comment