Created
March 24, 2017 18:22
-
-
Save kalineh/2ae12100fe718156161bbe0d548111de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class DebugTextHelper | |
: SingletonMonoBehaviourOnDemand<DebugTextHelper> | |
{ | |
public abstract class MessageBase | |
{ | |
public TextMesh text; | |
public float expire; | |
public float offset; | |
public abstract Vector3 GetBasePosition(); | |
public abstract bool IsExpired(); | |
} | |
public class MessageObject | |
: MessageBase | |
{ | |
public GameObject owner; | |
public override Vector3 GetBasePosition() | |
{ | |
return owner.transform.position; | |
} | |
public override bool IsExpired() | |
{ | |
if (GameObjectExtension.IsDestroyed(owner)) | |
return true; | |
if (!owner.activeSelf) | |
return true; | |
if (expire >= 0.0f && Time.time > expire) | |
return true; | |
return false; | |
} | |
} | |
public class MessagePosition | |
: MessageBase | |
{ | |
public Vector3 position; | |
public override Vector3 GetBasePosition() | |
{ | |
return position; | |
} | |
public override bool IsExpired() | |
{ | |
if (expire >= 0.0f && Time.time > expire) | |
return true; | |
return false; | |
} | |
} | |
public List<MessageBase> Messages = new List<MessageBase>(); | |
public bool Enabled = false; | |
[ConsoleCommand("debugtext.toggle", "shift+b")] | |
public static void ToggleEnabled() | |
{ | |
DebugTextHelper.Instance.Enabled = !DebugTextHelper.Instance.Enabled; | |
} | |
public void OnScriptReload() | |
{ | |
gameObject.DestroyAllChildren(); | |
Messages.Clear(); | |
} | |
public void RemoveExisting(GameObject obj) | |
{ | |
var index = 0; | |
while (index < Messages.Count) | |
{ | |
var item = Messages[index]; | |
var owned = item as MessageObject; | |
if (owned != null && owned.owner == obj) | |
{ | |
Messages.Remove(item); | |
Destroy(item.text); | |
continue; | |
} | |
index++; | |
} | |
} | |
public MessageBase Print(string str, Vector3 pos, float expire = 5.0f) | |
{ | |
var text = TextHelper.MakeDebugText(str); | |
text.color = new Color(0.2f, 0.2f, 0.2f); | |
text.transform.position = pos; | |
var actual_expire = expire < 0.0f ? expire : Time.time + expire; | |
var message = new MessagePosition() { text = text, position = pos, expire = actual_expire, }; | |
Messages.Add(message); | |
text.transform.SetParent(Instance.transform); | |
if (!Enabled) | |
text.gameObject.SetActive(false); | |
return message; | |
} | |
public MessageBase Print(string str, GameObject obj, float expire = 5.0f) | |
{ | |
if (!Enabled) return null; | |
var text = TextHelper.MakeDebugText(str); | |
text.transform.position = obj.transform.position; | |
text.color = new Color(0.2f, 0.2f, 0.2f); | |
var actual_expire = expire < 0.0f ? expire : Time.time + expire; | |
var offset = obj.EstimateRadius(); | |
var message = new MessageObject() { text = text, owner = obj, expire = actual_expire, offset = offset, }; | |
Messages.Add(message); | |
text.transform.SetParent(Instance.transform); | |
if (!Enabled) | |
text.gameObject.SetActive(false); | |
return message; | |
} | |
public void Update() | |
{ | |
var index = 0; | |
var camera = PlayerCameraSupport.GetCamera(); | |
while (index < Messages.Count) | |
{ | |
var item = Messages[index]; | |
if (item == null) | |
{ | |
Messages.Remove(item); | |
Destroy(item.text); | |
continue; | |
} | |
var expired = item.IsExpired(); | |
if (!Enabled) | |
expired = true; | |
if (expired) | |
{ | |
Messages.Remove(item); | |
Destroy(item.text); | |
continue; | |
} | |
if (item.expire > 0.0f) | |
item.offset += Time.deltaTime * 0.01f; | |
var ofs = Vector3.up * item.offset; | |
var fade = Mathf.Clamp01(item.expire - Time.time); | |
if (item.expire < 0.0f) | |
fade = 1.0f; | |
var pos = item.GetBasePosition(); | |
pos += ofs; | |
item.text.transform.position = pos; | |
item.text.transform.localScale = new Vector3( | |
item.text.transform.localScale.x, | |
item.text.transform.localScale.y * fade, | |
item.text.transform.localScale.z | |
); | |
var look_ofs = item.text.transform.position - camera.transform.position; | |
var look_dir = look_ofs.normalized; | |
item.text.transform.rotation = Quaternion.LookRotation(look_dir, Vector3.up); | |
index++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment