AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SingleInstance.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
12  public class SingleInstance<T> : MonoBehaviour where T : SingleInstance<T>
13  {
14  private static T _Instance;
15  public static T Instance
16  {
17  get
18  {
19  if (_Instance == null)
20  {
21  T[] objects = FindObjectsOfType<T>();
22  if (objects.Length == 1)
23  {
24  _Instance = objects[0];
25  }
26  else if (objects.Length > 1)
27  {
28  Debug.LogErrorFormat("Expected exactly 1 {0} but found {1}", typeof(T).ToString(), objects.Length);
29  }
30  }
31  return _Instance;
32  }
33  }
34 
40  protected virtual void OnDestroy()
41  {
42  _Instance = null;
43  }
44  }
45 }
virtual void OnDestroy()
Called by Unity when destroying a MonoBehaviour. Scripts that extend SingleInstance should be sure to...
A simplified version of the Singleton class which doesn&#39;t depend on the Instance being set in Awake ...