牛骨文教育服务平台(让学习变的简单)
博文笔记

NGUI 动态改变Label字体

创建时间:2013-10-30 投稿人: 浏览次数:7318


Label中的Target为空,我们在FloatingTextDriver的OnClick事件中动态添加了Target对象。

下面是FloatingTextDriver与FloatingText两个类的具体实现:

using UnityEngine;
using System.Collections;
 
public class FloatingTextDriver : MonoBehaviour {
 
    public GameObject target;
    public FloatingText ft;
    public UIFont font;
    IEnumerator OnClick()
    {
        ft.Target = target;
        ft.Text = "libufan";
        ft.Active = true;
 
        yield return new WaitForSeconds(2.0f);
 
        ft.Font = font;
        ft.FontSize = new Vector3(50, 50, 1);
 
    }
 
}

using UnityEngine;
using System.Collections;
 
public class FloatingText : MonoBehaviour {
 
	private UILabel _lbl;
 
    public GameObject _target;
    public Camera worldCamera;
    public Camera guiCamera;
    private Vector3 pubPos;
 
    private bool _active;
 
	void Awake()
	{
		_lbl = GetComponent<UILabel>();
		if(_lbl == null)
		{
			Debug.LogError("Could not fint the UILabel!");
		}
	}
 
    void Start()
    {
        guiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
    }
 
	public Color TextColor
	{
		get {return _lbl.color;}
		set {_lbl.color = value;}
	}
 
    public string Text
    {
        get { return _lbl.text; }
        set { _lbl.text = value; }
    }
 
    public bool Active
    {
        get { return _active; }
        set { _active = value; }
    }
 
    public GameObject Target
    {
        get { return _target; }
        set {
            _target = value;
            worldCamera = NGUITools.FindCameraForLayer(_target.layer);
        }
    }
 
    public UIFont Font
    {
        get { return _lbl.font; }
        set { _lbl.font = value; }
    }
 
    public Vector3 FontSize
    {
        get { return _lbl.transform.localScale; }
        set { _lbl.transform.localScale = value; }
    }
 
    void LateUpdate()
    {
 
        if (!_active)
        {
            return;
        }
 
        pubPos = worldCamera.WorldToViewportPoint(_target.transform.position);
        pubPos = guiCamera.ViewportToWorldPoint(pubPos);
        pubPos.z = 0;
        transform.position = pubPos;
    }
 
}


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。