VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_SingletonComponent.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Core {
6  /// <summary>
7  /// Combination of AVR_Component with a singleton.
8  /// </summary>
9  /// <typeparam name="T">Type of the Singleton. Should correspond to the class deriving from this. Example: class Example : AVR_SingletonComponent&lt;Example&gt;</typeparam>
10  public class AVR_SingletonComponent<T> : AVR_Component where T : MonoBehaviour
11  {
12  public static T Instance {
13  get{
14  if(_Instance==null) _Instance = FindObjectOfType<T>();
15  return _Instance;
16  }
17  private set{
18  _Instance = value;
19  }
20  }
21 
22  private static T _Instance;
23 
24  protected override void Awake()
25  {
26  base.Awake();
27 
28  SetInstance();
29  }
30 
31  private void SetInstance() {
32  if (_Instance != null)
33  {
34  AVR_DevConsole.cwarn(this.name + " is marked as Singleton but another instance was found in the scene. Continuing to use old one.", this);
35  return;
36  }
37 
38  Instance = gameObject.GetComponent<T>();
39 
40  if (Instance == null)
41  {
42  AVR_DevConsole.cerror(this.name + " is marked as a Singleton of type " + typeof(T).ToString() + " but no component of this type was found!", this);
43  }
44 
45  DontDestroyOnLoad(this);
46  }
47 
48  public virtual void OnApplicationQuit()
49  {
50  Instance = null;
51  }
52  }
53 }
Base class for all arc-vr components. A component is typically a Monobehaviour that represents a virt...