unity-code-编辑器显示文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections;

public class EditorLabel : MonoBehaviour {



public string text;

private static GUIStyle style;
private static GUIStyle Style{
get{
if(style == null){
style = new GUIStyle( EditorStyles.largeLabel );
style.alignment = TextAnchor.MiddleCenter;
style.normal.textColor = new Color(0.9f,0.9f,0.9f);
style.fontSize = 32;
}
return style;
}

}


void OnDrawGizmos(){


RaycastHit hit;
Ray r = new Ray(transform.position + Camera.current.transform.up * 8f, -Camera.current.transform.up );
if( GetComponent<Collider>().Raycast( r, out hit, Mathf.Infinity) ){

float dist = (Camera.current.transform.position - hit.point).magnitude;

float fontSize = Mathf.Lerp(64, 12, dist/10f);

Style.fontSize = (int)fontSize;

Vector3 wPos = hit.point + Camera.current.transform.up*dist*0.07f;



Vector3 scPos = Camera.current.WorldToScreenPoint(wPos);
if(scPos.z <= 0){
return;
}



float alpha = Mathf.Clamp(-Camera.current.transform.forward.y, 0f, 1f);
alpha = 1f-((1f-alpha)*(1f-alpha));

alpha = Mathf.Lerp(-0.2f,1f,alpha);

Handles.BeginGUI();


scPos.y = Screen.height - scPos.y; // Flip Y


Vector2 strSize = Style.CalcSize(new GUIContent(text));

Rect rect = new Rect(0f, 0f, strSize.x + 6,strSize.y + 4);
rect.center = scPos - Vector3.up*rect.height*0.5f;
GUI.color = new Color(0f,0f,0f,0.8f * alpha);
GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture);
GUI.color = Color.white;
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.Label(rect, text, Style);
GUI.color = Color.white;

Handles.EndGUI();
}

}
}
#endif