动态改变Font(字体)
来源:互联网 发布:任务记录软件 编辑:程序博客网 时间:2024/06/10 15:02
NGUI 动态改变Label字体
这篇文章的含金量可能并不是很高,也许几乎是一点没有,因为很多游戏中不会这么无聊的在游戏过程中还要改变字体,基本都是静态的图片或者文本就可以了。之所以写这篇文章是为了熟悉一下C#中如何定义getter与setter。首先我们创建一个Label一个Button,然后创建两个C#类,一个FloatingText绑定到Label上,一个FloatingTextDriver绑定到Button上,这样我们Inspector面板中把相应的参数赋值即可,当我们点击按钮的时候,Label文本出现,并且位置一直跟随Target运动。
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; } }
祝大家幸运!
0 0
- 动态改变Font(字体)
- 关于字体改变的类Font
- 使用font-face改变自己的字体
- Ubuntu 8.04 text mode change font 改变控制台字体
- 利用private font改变PDF文件的字体
- font字体
- Font字体
- Font字体
- 动态改变字体的颜色
- NGUI 动态改变Label字体
- HTML动态改变字体颜色
- ios10动态改变系统字体
- html dom动态改变字体
- Flex动态载入嵌入字体(Embeded Font)文件
- NGUI动态字体出现character not found in font
- Unity动态字体文字破碎的解决方法(Dynamic Font Broken)
- Unity动态字体文字破碎的解决方法(Dynamic Font Broken)
- Unity动态字体文字破碎的解决方法(Dynamic Font Broken)
- 关于inflate的第3个参数
- 进程上下文与中断上下文的理解
- LeetCode Word Search II DFS+Trie树
- 升级xcode 7遇到的问题
- ExtJS 性能优化注意要点
- 动态改变Font(字体)
- 根据方法名获取方法并动态执行
- ListView,gallery与内部控件事件冲突以及EditText回退键和回车键失效
- ActiveMQ安装与配置
- JPA 应用篇(一) 基于JPA 的BaseDAO
- s5p4418-android5.1.1编译出错问题2
- rectified linear units (ReLUs)
- hdoj2047(EOF牛肉串
- MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。