如何在unity3d的UGUI中给Text添加材质和艺术字

如何在unity3d的UGUI中给Text添加材质和艺术字,第1张

添加艺术字体

将下载好的艺术字体放到Unity中,选中Text 在Inspector—>Text(script)—>character—>Font处可以看到加载好的艺术字体

添加材质:Inspector—>Text(script)—>character—>Material可修改材质

首先我们创建一个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()

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

}

}

NGUI中,使用自定义字体可以使用font maker,但是UGUI中并没有提供相应工具。

UGUI的自定义字体为

Unity3d自定义字体

并且官方没有提供相对应的字体制作工具。这里提供一个生成custom Font参数的脚本。

需要配合BMFont使用。

BMFont导出设置为XML文件

导出文字设置

将生产的.fnt文件和贴图文件拖入项目中,在项目中创建metial,贴图为生成的贴图文件。shader为UI使用的SHADER。

创建自定义font,将材质绑定到Metial绑定到font中,其中,line spacing为文字的行高,根据实际设置。

打开custom font maker窗口。

将自定义字体以及.font文件拖到指定位置。然后点击创建字体,即可生成对应的字体文件。

不足之处,没有设置到对应的line spacing。需要创建好后手动设置。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11843455.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存