Tilt Five™ Unity API  1.4.1
TiltFive.SingletonComponent< T > Class Template Reference
Inheritance diagram for TiltFive.SingletonComponent< T >:

Protected Member Functions

virtual void Awake ()
 

Properties

static T Instance [get]
 

Static Private Attributes

static T s_Instance = null
 

Detailed Description

Type Constraints
T :UnityEngine.MonoBehaviour 

Definition at line 45 of file Singleton.cs.

Member Function Documentation

◆ Awake()

virtual void TiltFive.SingletonComponent< T >.Awake ( )
protectedvirtual

Reimplemented in TiltFive.TiltFiveManager2, TiltFive.TiltFiveManager, and TiltFive.Display.

Definition at line 112 of file Singleton.cs.

113  {
114  if(s_Instance == null)
115  {
117  }
118  }

References TiltFive.SingletonComponent< T >.Instance, and TiltFive.SingletonComponent< T >.s_Instance.

Member Data Documentation

◆ s_Instance

T TiltFive.SingletonComponent< T >.s_Instance = null
staticprivate

Definition at line 47 of file Singleton.cs.

Referenced by TiltFive.SingletonComponent< T >.Awake().

Property Documentation

◆ Instance

T TiltFive.SingletonComponent< T >.Instance
staticget

Definition at line 48 of file Singleton.cs.

49  {
50  get
51  {
52  if( s_Instance != null )
53  {
54  return s_Instance;
55  }
56 
57  T[] instances = Resources.FindObjectsOfTypeAll<T>();
58  if( instances != null )
59  {
60 
61  // find the one that is actually in the scene (and not the editor)
62  for( int i = 0; i < instances.Length; ++i )
63  {
64  T instance = instances[ i ];
65  if( instance == null )
66  {
67  continue;
68  }
69 
70  if( instance.hideFlags != HideFlags.None )
71  {
72  continue;
73  }
74 
75  // avoid selecting component attached to a prefab asset that isn't in scene
76  if( string.IsNullOrEmpty(instance.gameObject.scene.name) )
77  {
78  continue;
79  }
80 
81  s_Instance = instance;
82 
83  //We can't use DontDestroyOnLoad on objects that aren't root objects. (i.e. objects that have a parent)
84  if(s_Instance.gameObject.transform.parent == null && Application.isPlaying)
85  {
86  DontDestroyOnLoad( s_Instance );
87  }
88  break;
89  }
90  }
91 
92 
93  if(s_Instance == null
94  // Don't automatically generate a new instance if the application isn't playing
95  // (i.e. we're in the editor with the game stopped)
96  && Application.isPlaying)
97  {
98  string name = string.Format( "__{0}__", typeof( T ).FullName );
99  GameObject singletonGo = new GameObject( name );
100  s_Instance = singletonGo.AddComponent<T>();
101  if(s_Instance.gameObject.transform.parent == null){
102  DontDestroyOnLoad( s_Instance );
103  }
104  }
105 
106  return s_Instance;
107  }
108  }

Referenced by TiltFive.SingletonComponent< T >.Awake(), and TiltFive.TiltFiveSingletonHelper.TryGetISceneInfo().


The documentation for this class was generated from the following file: