场景中人名显示

//这只是个简单实现,发现shader方面不应该用U中自带的文本shader,因为那个太透了。。需要完善的地方还很多,等需要的时候再好好写一下 ^.^

//作用:在物体头上显示一串文本,跟着主相机旋转。可以用偏移来设置其位置。

View Code
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class NameTip : MonoBehaviour
 5 {
 6     public string Text="";//内容
 7     public Vector3 TextOffset=new Vector3(0,1,0);//偏移
 8     public Color TextColor = Color.green;//字符颜色
 9     public Font TextFont;//字体
10     private GameObject go;
11     void Start () {
12         Font font = Resources.Load("Fonts/SIMHEI") as Font;
13         //设定承载字符的物体
14         go = new GameObject("TipText");
15         go.AddComponent(typeof(TextMesh));
16         go.AddComponent<MeshRenderer>();
17         go.renderer.material = font.material;
18         go.renderer.material.color = TextColor;
19         go.transform.parent = gameObject.transform;
20         go.transform.localPosition = Vector3.zero + TextOffset;
21         //设置字符的样式
22         TextMesh txt = go.GetComponent<TextMesh>();
23         txt.text = Text;
24         txt.characterSize = 0.2f;
25         txt.anchor = TextAnchor.UpperCenter;
26         txt.alignment = TextAlignment.Center;
27         txt.font = font;
28       }
29       // Update is called once per frame
30       void Update () {
31               go.transform.LookAt(Camera.mainCamera.transform.position);
32      go.transform.Rotate(0, 180, 0);
33     }
34 
35 }
posted @ 2012-07-25 14:22  渡蓝  阅读(137)  评论(0编辑  收藏  举报