Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public abstract class MonoBehavior : MonoBehaviour {
- // private cached variables
- private GameObject _gameObject;
- private Transform _transform;
- private Rigidbody _rigidbody;
- private Renderer _renderer;
- private Animation _animation;
- private Animator _animator;
- private AudioSource _audio;
- private Collider _collider;
- private Camera _camera;
- private Light _light;
- private Rigidbody2D _rigidbody2D;
- private Collider2D _collider2D;
- #region public getter properties
- public new GameObject gameObject {
- get {
- if (_gameObject == null) {
- _gameObject = base.gameObject;
- }
- return _gameObject;
- }
- }
- public new Transform transform {
- get {
- if (_transform == null) {
- _transform = base.transform;
- }
- return _transform;
- }
- }
- public new Rigidbody rigidbody {
- get {
- if (_rigidbody == null) {
- // get the attached rigidbody if one exists
- _rigidbody = GetComponent<Rigidbody>();
- if (_rigidbody == null && collider != null) {
- _rigidbody = collider.attachedRigidbody;
- }
- }
- return _rigidbody;
- }
- }
- public new Renderer renderer {
- get {
- if (_renderer == null) {
- _renderer = GetComponent<Renderer>();
- }
- return _renderer;
- }
- }
- public new Animation animation {
- get {
- if (_animation == null) {
- _animation = GetComponent<Animation>();
- }
- return _animation;
- }
- }
- public Animator animator {
- get {
- if (_animator == null) {
- _animator = GetComponent<Animator>();
- }
- return _animator;
- }
- }
- public new AudioSource audio {
- get {
- if (_audio == null) {
- _audio = GetComponent<AudioSource>();
- }
- return _audio;
- }
- }
- public new Collider collider {
- get {
- if (_collider == null) {
- _collider = GetComponent<Collider>();
- }
- return _collider;
- }
- }
- public new Camera camera {
- get {
- if (_camera == null) {
- _camera = GetComponent<Camera>();
- }
- return _camera;
- }
- }
- public new Light light {
- get {
- if (_light == null) {
- _light = GetComponent<Light>();
- }
- return _light;
- }
- }
- public new Rigidbody2D rigidbody2D {
- get {
- if (_rigidbody2D == null) {
- // get the attached rigidbody if one exists
- _rigidbody2D = GetComponent<Rigidbody2D>();
- if (_rigidbody2D == null && collider2D != null) {
- _rigidbody2D = collider2D.attachedRigidbody;
- }
- }
- return _rigidbody2D;
- }
- }
- public new Collider2D collider2D {
- get {
- if (_collider2D == null) {
- _collider2D = GetComponent<Collider2D>();
- }
- return _collider2D;
- }
- }
- #endregion //public getter properties
- #region Typesafe instantiation
- #if UNITY_3 || UNITY_4
- // Unity 5 includes this now.
- public static T Instantiate<T>(T obj) where T : Component {
- return (T)Object.Instantiate(obj);
- }
- #endif
- public static T Instantiate<T>(T obj, Vector3 position, Quaternion rotation) where T : Component {
- return (T)Object.Instantiate(obj, position, rotation);
- }
- public static GameObject Instantiate(GameObject obj) {
- return (GameObject)Object.Instantiate(obj);
- }
- public static GameObject Instantiate(GameObject obj, Vector3 position, Quaternion rotation) {
- return (GameObject)Object.Instantiate(obj, position, rotation);
- }
- #endregion //Typesafe instantiation
- }
Advertisement
Add Comment
Please, Sign In to add comment