Skip to content
This repository has been archived by the owner on Jan 8, 2018. It is now read-only.

Commit

Permalink
Cleaned-up Singleton class
Browse files Browse the repository at this point in the history
* Revised the singleton class, removing unnecessary code and changing others.
* Added an extra level of detail to the version numbers used (from x.y to x.y.z)
  • Loading branch information
christianchomiak committed Sep 7, 2015
1 parent 2dbb7af commit 3f45487
Show file tree
Hide file tree
Showing 48 changed files with 96 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public void Awake()
{
v = p2 + p1;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// RandomColor v1.0 by Christian Chomiak, [email protected]
/// RandomColor v1.0.0 by Christian Chomiak, [email protected]
///
/// API to get random colors a given hue, saturation or value as a starting point.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// HSVColorDrawer v1.1 by Christian Chomiak, [email protected]
/// HSVColorDrawer v1.1.0 by Christian Chomiak, [email protected]
///
/// Custom property drawer for the HSVColor struct.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// HSVColor v1.1 by Christian Chomiak, [email protected]
/// HSVColor v1.1.0 by Christian Chomiak, [email protected]
///
/// Struct to represent colors in the HSV (hue, saturation, value) system.
/// Includes:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/// <summary>
/// RegexAttribute v1.0
/// RegexAttribute v1.0.0
///
/// Taken from: http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/
/// </summary>


using UnityEngine;

namespace Unitilities
{
public class ReadOnlyAttribute : PropertyAttribute
{

public ReadOnlyAttribute()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ReadOnlyDrawer v1.0
/// ReadOnlyDrawer v1.0.0
///
/// Taken from: http://answers.unity3d.com/questions/489942/how-to-make-a-readonly-property-in-inspector.html
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// RegexAttribute v1.0
/// RegexAttribute v1.0.0
///
/// Taken from: http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// RegexDrawer v1.0
/// RegexDrawer v1.0.0
///
/// When declaring a variable:
/// * For IPv4: [Regex(@"^(?:\d{1,3}\.){3}\d{1,3}$", "Invalid IPv4 address!\nExample: '127.0.0.1'")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// TransformInspector v1.0 by Christian Chomiak, [email protected]
/// TransformInspector v1.0.0 by Christian Chomiak, [email protected]
///
/// Script to provide shortcuts in the Transform Inspector to reset the Position,
/// Scale and Rotation of a GameObject.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// UnitilitiesMenu v1.0 by Christian Chomiak, [email protected]
/// UnitilitiesMenu v1.0.1 by Christian Chomiak, [email protected]
///
/// Script to show quick Unitilities-related shortcuts in the Unity Editor.
/// </summary>
Expand All @@ -14,14 +14,12 @@ public class UnitilitiesMenu : ScriptableObject
// Add a menu item named "Do Something with a Shortcut Key" to MyMenu in the menu bar
// and give it a shortcut (ctrl-g on Windows, cmd-g on OS X).
[MenuItem("Unitilities/GameObject/Enable-Disable %h")]
static void DoIt()
static void ChangeEnableStatus()
{
foreach (GameObject go in Selection.gameObjects)
{
//EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", "");
bool activeStatus = go.activeSelf;

go.SetActive(!activeStatus);
go.SetActive(!go.activeSelf);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// CollectionExtensions v1.2 by Christian Chomiak, [email protected]
/// CollectionExtensions v1.2.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of Collections (arrays, lists, etc.)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ColorExtensions v1.0 by Christian Chomiak, [email protected]
/// ColorExtensions v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of Unity's Color.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// DateTimeExtensions v1.0 by Christian Chomiak, [email protected]
/// DateTimeExtensions v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions for manipulating Date/Time.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// GameObjectExtensions v1.0 by Christian Chomiak, [email protected]
/// GameObjectExtensions v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of GameObjects
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// IComparableExtensions v1.0 by Christian Chomiak, [email protected]
/// IComparableExtensions v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of IComparable objects.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// PrimitiveExtensions v1.1 by Christian Chomiak, [email protected]
/// PrimitiveExtensions v1.1.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of C# primitives.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// VectorExtensions v1.0 by Christian Chomiak, [email protected]
/// VectorExtensions v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the use of Vectors
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <summary>
/// ApplicationHelper v1.1 by Christian Chomiak, [email protected]
/// ApplicationHelper v1.1.0 by Christian Chomiak, [email protected]
///
/// Shortcuts for common queries to Unity's Application.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ColorHelper v1.1 by Christian Chomiak, [email protected]
/// ColorHelper v1.1.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate the manipulation of Unitiy's Color.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <summary>
/// EnumHelper v1.0 by Christian Chomiak, [email protected]
/// EnumHelper v1.0.0 by Christian Chomiak, [email protected]
///
/// Useful functions related to enums.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ColorHelper v1.0 by Christian Chomiak, [email protected]
/// ColorHelper v1.0.0 by Christian Chomiak, [email protected]
///
/// Set of useful mathematical functions.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <summary>
/// PrimitiveHelper v1.0 by Christian Chomiak, [email protected]
/// PrimitiveHelper v1.0.0 by Christian Chomiak, [email protected]
///
/// Useful functions for C# primitives.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// RandomHelper v1.0 by Christian Chomiak, [email protected]
/// RandomHelper v1.0.0 by Christian Chomiak, [email protected]
///
/// Useful functions for random labors.
/// </summary>
Expand All @@ -8,10 +8,8 @@

namespace Unitilities
{

public static class RandomHelper
{

#region int
/// <summary>
/// Returns 1 or -1 at random.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ColorHelper v1.0 by Christian Chomiak, [email protected]
/// ColorHelper v1.0.0 by Christian Chomiak, [email protected]
///
/// Functions that facilitate working with Unity Textures.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using UnityEngine;


namespace Unitilities.Debugging
{

public enum PlayerPrefsType { Int, Float, String };

public class PlayerPrefsDataType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// PlayerPrefsEditor v1.0 by Christian Chomiak, [email protected]
/// PlayerPrefsEditor v1.0.0 by Christian Chomiak, [email protected]
///
/// PlayerPrefs editor and reader.
///
Expand Down
76 changes: 43 additions & 33 deletions Unity Utilities/Assets/Unitilities/Scripts/Core/Singleton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// Singleton v1.1 by Christian Chomiak, [email protected]
/// Singleton v1.2.0 by Christian Chomiak, [email protected]
///
/// Base class to provide a singleton status to an object.
///
Expand Down Expand Up @@ -33,46 +33,69 @@ public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
[SerializeField]
bool isPersistent = true;


#region Unity

protected virtual void Awake()
{
if (Exists) //(instance != null && !IsCurrentSingleton())
if (Exists)
{
if (!IsCurrentSingleton())
if (!IsCurrentSingleton)
{
Debug.LogWarning("Warning: More than one instance of singleton " + typeof(T) + " existing.");
Debug.LogWarning("Warning: More than one instance of singleton " + typeof(T) + " exists in the scene. The newest one will be destroyed.");
Destroy(gameObject);
}
}
else //if (instance == null)
else
{
instance = gameObject.GetComponent<T>(); // AddComponent(typeof(T)) as T;
// Previously, it was `instance = gameObject.GetComponent<T>();` but this should be more efficient
instance = this as T;

if (isPersistent)
DontDestroyOnLoad(gameObject);
}
}

public virtual void OnDestroy()
//Only the true singleton can reset the global reference
protected virtual void OnDestroy()
{
if (IsCurrentSingleton())
{
if (IsCurrentSingleton)
instance = null;
}

#endregion

#region Private & Protected

/// <summary>
/// Determines whether the current instance is the one true singleton
/// </summary>
/// <returns></returns>
protected bool IsCurrentSingleton
{
get
{
return Exists && instance.gameObject.GetInstanceID() == gameObject.GetInstanceID();
}
}

#endregion


#region Public API

/// <summary>
/// Determines whether the specified Singleton exists or not
/// </summary>
public static bool Exists
{
get { return ((object)instance != null); }
get
{
#if UNITY_EDITOR
return (instance != null);
#else
return ((object) instance != null);
#endif
}
}

/// <summary>
Expand All @@ -84,33 +107,20 @@ public static T Instance
{
if (!Exists)
{
instance = (T)FindObjectOfType(typeof(T));

if (!Exists)
{
Debug.Log("An instance of " + typeof(T) + " is needed in the scene, but there is none. Generated automatically.");

GameObject obj = new GameObject("Singleton_" + typeof(T));
instance = obj.AddComponent(typeof(T)) as T;
}
//We could first check inside the scene for an existing instance
//but because how this script is constructed, asking if it `Exists` should be enough.
//instance = (T) FindObjectOfType(typeof(T));

Debug.Log("An instance of " + typeof(T).ToString() + " is needed in the scene, but there was none found.\nOne was generated automatically for you.");

GameObject obj = new GameObject("Singleton_" + typeof(T));
instance = obj.AddComponent(typeof(T)) as T;
}

return instance;
}
}

/// <summary>
/// Determines whether the current instance is the one true singleton
/// </summary>
/// <returns></returns>
public bool IsCurrentSingleton()
{
if (!Exists) // instance == null
return false;

return instance.gameObject.GetInstanceID() == gameObject.GetInstanceID();
}


#endregion

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// InvisibleObjectDrawerEditor v1.0 by Christian Chomiak, [email protected]
/// InvisibleObjectDrawerEditor v1.0.0 by Christian Chomiak, [email protected]
///
/// Custom editor for InvisibleObjectDrawer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ObjectTextDrawerEditor v1.0 by Christian Chomiak, [email protected]
/// ObjectTextDrawerEditor v1.0.0 by Christian Chomiak, [email protected]
///
/// Custom editor for ObjectTextDrawer.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// InvisibleObjectDrawer v1.0 by Christian Chomiak, [email protected]
/// InvisibleObjectDrawer v1.0.0 by Christian Chomiak, [email protected]
///
/// Script to show a GameObject's position ingame.
/// Meant to be used on objects that are have no meshrender.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// ObjectTextDrawer v1.0 by Christian Chomiak, [email protected]
/// ObjectTextDrawer v1.0.0 by Christian Chomiak, [email protected]
///
/// Script to show a text, above a GameObject, ingame.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// LocalizationConfig v1.1 by Christian Chomiak, [email protected]
/// LocalizationConfig v1.1.0 by Christian Chomiak, [email protected]
///
/// MonoBehaviour that makes use of the LocalizationManager and modify
/// the sibling UI Text as needed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <summary>
/// LocalizationManager v1.1 by Christian Chomiak, [email protected]
/// LocalizationManager v1.1.0 by Christian Chomiak, [email protected]
///
/// Manager in charge of translating the LocalizedTexts.
///
Expand Down
Loading

0 comments on commit 3f45487

Please sign in to comment.