Advertisement
Guest User

Untitled

a guest
Jul 16th, 2016
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // Открывает доступ к объекту на сцене через статическую переменную.
  4. // Ссылка задаётся в методе OnEnable и удаляется в OnDisable.
  5. public class SceneObject<TSelf> : MonoBehaviour where TSelf : MonoBehaviour
  6. {
  7.     public static TSelf Current { get; private set; }
  8.  
  9.     protected virtual void OnEnable()
  10.     {
  11.         if (Current != null)
  12.         {
  13.             Debug.LogErrorFormat("There should never be more than 1 object '{0}'!", typeof(TSelf));
  14.             return;
  15.         }
  16.  
  17.         Current = GetComponent<TSelf>();
  18.     }
  19.     protected virtual void OnDisable()
  20.     {
  21.         Current = null;
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement