EditorLabel.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections;
  5. public class EditorLabel : MonoBehaviour {
  6. public string text;
  7. private static GUIStyle style;
  8. private static GUIStyle Style{
  9. get{
  10. if(style == null){
  11. style = new GUIStyle( EditorStyles.largeLabel );
  12. style.alignment = TextAnchor.MiddleCenter;
  13. style.normal.textColor = new Color(0.9f,0.9f,0.9f);
  14. style.fontSize = 32;
  15. }
  16. return style;
  17. }
  18. }
  19. void OnDrawGizmos(){
  20. RaycastHit hit;
  21. Ray r = new Ray(transform.position + Camera.current.transform.up * 8f, -Camera.current.transform.up );
  22. if( GetComponent<Collider>().Raycast( r, out hit, Mathf.Infinity) ){
  23. float dist = (Camera.current.transform.position - hit.point).magnitude;
  24. float fontSize = Mathf.Lerp(64, 12, dist/10f);
  25. Style.fontSize = (int)fontSize;
  26. Vector3 wPos = hit.point + Camera.current.transform.up*dist*0.07f;
  27. Vector3 scPos = Camera.current.WorldToScreenPoint(wPos);
  28. if(scPos.z <= 0){
  29. return;
  30. }
  31. float alpha = Mathf.Clamp(-Camera.current.transform.forward.y, 0f, 1f);
  32. alpha = 1f-((1f-alpha)*(1f-alpha));
  33. alpha = Mathf.Lerp(-0.2f,1f,alpha);
  34. Handles.BeginGUI();
  35. scPos.y = Screen.height - scPos.y; // Flip Y
  36. Vector2 strSize = Style.CalcSize(new GUIContent(text));
  37. Rect rect = new Rect(0f, 0f, strSize.x + 6,strSize.y + 4);
  38. rect.center = scPos - Vector3.up*rect.height*0.5f;
  39. GUI.color = new Color(0f,0f,0f,0.8f * alpha);
  40. GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
  41. GUI.color = Color.white;
  42. GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
  43. GUI.Label(rect, text, Style);
  44. GUI.color = Color.white;
  45. Handles.EndGUI();
  46. }
  47. }
  48. }
  49. #endif