/**
* Copyright 2019 Heroic Labs and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using UnityEngine;
namespace DemoGame.Scripts.Utils
{
///
/// Inherit from this base class to create a singleton.
/// e.g. public class MyClassName : Singleton {}
///
public class Singleton : MonoBehaviour where T : MonoBehaviour
{
#region Variables
///
/// Lock used to not allow simultaneous operations on this singleton by multiple sources.
///
private static object _lock = new object();
///
/// Reference to the singleton instance of type .
///
private static T _instance;
#endregion
#region Properties
///
/// Returns the reference to the singleton instance of type .
///
public static T Instance
{
get
{
// Lock preventing from simultaneous access by multiple sources.
lock (_lock)
{
// If it's the first time accessing this singleton Instance, _instance will always be null
// Searching for an active instance of type T in the scene.
if (_instance == null)
{
_instance = FindObjectOfType();
}
return _instance;
}
}
}
#endregion
#region Monobehaviour
///
/// Checking if an instance of already exists in the scene.
/// If it exists, destroy this object.
///
protected virtual void Awake()
{
if (Instance != this)
{
Destroy(gameObject);
}
}
///
/// Removes the reference to this object on destroy.
///
protected virtual void OnDestroy()
{
if (Instance == this)
{
_instance = null;
}
}
#endregion
}
}