怎么通过脚本修改ngui的UILabel字体
2015-12-04 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517199
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
首先我们创建一个Label一个Button,然后创建两个C#类,一个FloatingText绑定到Label上,一个FloatingTextDriver绑定到Button上,这样我们Inspector面板中把相应的参数赋值即可,当我们点击按钮的时候,Label文本出现,并且位置一直跟随Target运动。
Label中的Target为空,我们在FloatingTextDriver的OnClick事件中动态添加了Target对象。
下面是FloatingTextDriver与FloatingText两个类的具体实现:
Source code
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);
}
}
Source code
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;
}
}
Label中的Target为空,我们在FloatingTextDriver的OnClick事件中动态添加了Target对象。
下面是FloatingTextDriver与FloatingText两个类的具体实现:
Source code
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);
}
}
Source code
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;
}
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询