码迷,mamicode.com
首页 > 其他好文 > 详细

角色头上冒数字

时间:2014-07-31 12:21:46      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   art   

/// <summary>
/// Floating text.
/// the GUI Text Floating system
/// </summary>

using UnityEngine;
using System.Collections;

public class FloatingText : MonoBehaviour {

    public GUISkin CustomSkin;// GUISkin
    public string Text = "";// Text
    public float LifeTime = 1;// Life time
    public bool FadeEnd = false;// Fade out at last 1 second before destroyed
    public Color TextColor = Color.white; // Text color
    public bool Position3D = false; // enabled when you need the text along with world 3d position
    public Vector2 Position; // 2D Position
    
    private float alpha = 1;
    private float timeTemp = 0;



    void Start () {
        timeTemp = Time.time;
        GameObject.Destroy(this.gameObject,LifeTime);
        if(Position3D){
            Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
            Position = new Vector2(screenPos.x,Screen.height - screenPos.y);
        }
    }

    void Update () {

        if(FadeEnd){
            if(Time.time >= ((timeTemp + LifeTime) - 1)){
                alpha = 1.0f - (Time.time - ((timeTemp + LifeTime) - 1));
            }
        }else{
            alpha = 1.0f - ((1.0f / LifeTime) * (Time.time - timeTemp));
        }
    
        if(Position3D){
            Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
            Position = new Vector2(screenPos.x,Screen.height - screenPos.y);
        }
    
    }


    void OnGUI(){
        
        GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);
        if(CustomSkin){
            GUI.skin = CustomSkin;
        }
    
        Vector2 textsize = GUI.skin.label.CalcSize(new GUIContent(Text));
        Rect rect = new Rect(Position.x - (textsize.x/2), Position.y,textsize.x,textsize.y);

        GUI.skin.label.normal.textColor = TextColor;
        GUI.Label(rect,Text);

    }
}

 调用方式

public void AddFloatingText(Vector3 pos,string text){
        // Adding Floating Text Effect
        if(FloatingText){
            var floattext = (GameObject)Instantiate(FloatingText,pos,transform.rotation);
            if(floattext.GetComponent<FloatingText>()){
                floattext.GetComponent<FloatingText>().Text = text;
            }
            GameObject.Destroy(floattext,1);    
        }
    }

 

角色头上冒数字,布布扣,bubuko.com

角色头上冒数字

标签:des   style   blog   color   os   io   for   art   

原文地址:http://www.cnblogs.com/softimagewht/p/3880256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!