Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Last active April 27, 2017 10:32
Show Gist options
  • Select an option

  • Save sandcastle/23b500cad3c35a8c4223a3dbb6d88ea0 to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/23b500cad3c35a8c4223a3dbb6d88ea0 to your computer and use it in GitHub Desktop.
Log messages to the screen in Unity.
using System;
using System.Text;
using UnityEngine;
public class LogToScreen : Singleton<LogToScreen>
{
const int Lines = 18;
static string[] _lastItems = new string[Lines];
static string _message = string.Empty;
public static void Push(string message)
{
var builder = new StringBuilder();
for (var i = 0; i < _lastItems.Length - 1; i++)
{
_lastItems[i] = _lastItems[i + 1];
builder.AppendLine(_lastItems[i]);
}
_lastItems[_lastItems.Length - 1] = message;
builder.AppendLine(message);
_message = builder.ToString();
}
void OnGUI()
{
var style = new GUIStyle();
style.fontSize = 15;
style.wordWrap = true;
style.normal.textColor = new Color(1, 1, 1, 0.9f);
GUI.Label(new Rect(10, 10, Screen.width - 10, Screen.height - 10), _message, style);
}
}
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T instance = null;
protected static object instanceLock = new object();
protected static bool destroyedOnApplicationQuit = false;
Transform _transform = null;
GameObject _gameObject = null;
bool _isInitialized = false;
bool _isForcefullyDestroyed = false;
public Transform CachedTransform
{
get
{
if (_transform == null)
{
_transform = transform;
}
return _transform;
}
}
public GameObject CachedGameObject
{
get
{
if (_gameObject == null)
{
_gameObject = gameObject;
}
return _gameObject;
}
}
public static T Instance
{
get
{
System.Type _singletonType = typeof(T);
// We are requesting an instance after application is quit
if (destroyedOnApplicationQuit)
{
Debug.LogWarning("[SingletonPattern] " + _singletonType + " instance is already destroyed.");
return null;
}
lock (instanceLock)
{
if (instance == null)
{
// Get all the instances that exist in the screen
T[] _monoComponents = FindObjectsOfType(_singletonType) as T[];
if (_monoComponents.Length > 0)
{
instance = _monoComponents[0];
for (int iter = 1; iter < _monoComponents.Length; iter++)
{
Destroy(_monoComponents[iter].gameObject);
}
}
// We need to create new instance
if (instance == null)
{
// First search in resource if prefab exists for this class
string _singletonName = _singletonType.Name;
GameObject _singletonPrefab = Resources.Load("Singleton/" + _singletonName, typeof(GameObject)) as GameObject;
if (_singletonPrefab != null)
{
instance =(Instantiate(_singletonPrefab) as GameObject).GetComponent<T>();
}
else
{
instance = new GameObject().AddComponent<T>();
}
// Update name
instance.name = _singletonName;
}
}
}
// Check if component is initialized or not
Singleton<T> _singletonInstance = (Singleton<T>)(object)instance;
if (!_singletonInstance._isInitialized)
{
_singletonInstance.Init();
}
return instance;
}
private set
{
instance = value;
}
}
private void Awake()
{
if (!_isInitialized)
{
Init();
}
}
protected virtual void Start() {}
protected virtual void Reset()
{
// Reset properties
_gameObject = null;
_transform = null;
_isInitialized = false;
_isForcefullyDestroyed = false;
}
protected virtual void OnEnable() {}
protected virtual void OnDisable() {}
protected virtual void OnDestroy()
{
// Singleton instance means same instance will run throughout the gameplay session
// If its destroyed that means application is quit
if (instance == this && !_isForcefullyDestroyed)
{
destroyedOnApplicationQuit = true;
}
}
protected virtual void Init()
{
// Set as initialized
_isInitialized = true;
// Just in case, handling so that only one instance is alive
if (instance == null)
{
instance = this as T;
}
// Destroying the reduntant copy of this class type
else if (instance != this)
{
Destroy(CachedGameObject);
return;
}
// Set it as persistent object
DontDestroyOnLoad(CachedGameObject);
}
public void ForceDestroy()
{
// Mark that object was forcefully destroyed
_isForcefullyDestroyed = true;
// Destory
Destroy(CachedGameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment