Skip to content

Instantly share code, notes, and snippets.

@FVSHaLuan
Last active September 24, 2015 12:51
Show Gist options
  • Save FVSHaLuan/b278cd450c77f4cf94aa to your computer and use it in GitHub Desktop.
Save FVSHaLuan/b278cd450c77f4cf94aa to your computer and use it in GitHub Desktop.
**Function: Print some text to screen when run game on real devices. Must be built in Development build to take effect (that's good because you don't have to care about removing it in final build)
using UnityEngine;
using System.Collections;
/* by FVS - Ha Luan */
public class Debugf : MonoBehaviour
{
#region Instance
static Debugf instance = null;
static Debugf Instance
{
get
{
if (instance == null)
{
Debugf debugf = new GameObject() { name = "Device logger" }.AddComponent<Debugf>();
debugf.Init();
DontDestroyOnLoad(debugf);
instance = debugf;
return instance;
}
else
{
return instance;
}
}
}
void Awake()
{
if (instance == null)
{
instance = this;
Init();
DontDestroyOnLoad(instance);
}
else
{
if (instance == this)
{
return;
}
else
{
Destroy(this);
}
}
}
#endregion
string logContent = "";
bool isShown = true;
GUIStyle guiStyle;
bool inited = false;
void Init()
{
if (!inited)
{
guiStyle = new GUIStyle()
{
fontSize = 20 * Mathf.Max(Screen.height, Screen.width) / 326
};
}
///
inited = true;
}
public void OnGUI()
{
if (isShown)
{
DisplayLogContent();
}
}
void DisplayLogContent()
{
GUI.Label(new Rect(0, 0, 1000, 1000), new GUIContent() { text = logContent }, guiStyle);
}
void Logf(string content)
{
logContent = "\n" + content + logContent;
}
void Clearf()
{
logContent = "";
}
#region Static public exposition
#if DEBUG
public static void Log(string content)
{
Instance.Logf(content);
}
public static void Log(object content)
{
Instance.Logf(content.ToString());
}
public static void Clear()
{
Instance.Clearf();
}
#else
public static void Log(string content)
{
}
public static void Log(object content)
{
}
public static void Clear()
{
}
#endif
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment