已解决
[Unity]给场景中的3D字体TextMesh增加描边方案一
来自网友在路上 159859提问 提问时间:2023-10-25 09:10:36阅读次数: 59
最佳答案 问答题库598位专家为你答疑解惑
取你的文本对象,简单地添加以下脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;public class TextOutline : MonoBehaviour {public float pixelSize = 1;public Color outlineColor = Color.black;public bool resolutionDependant = false;public int doubleResolution = 1024;RectTransform rectTransform;private Text textMesh;private Color originalColor;void Start() {textMesh = GetComponent<Text>(); rectTransform = this.GetComponent<RectTransform>();originalColor = textMesh.color;for (int i = 0; i < 8; i++) {GameObject outline = new GameObject("outline", typeof(Text));outline.transform.parent = transform;outline.transform.localScale = new Vector3(1, 1, 1);RectTransform rectTransformChild = outline.GetComponent<RectTransform>();rectTransformChild.anchoredPosition = rectTransform.anchoredPosition;rectTransformChild.anchoredPosition3D = rectTransform.anchoredPosition3D;rectTransformChild.anchorMax = rectTransform.anchorMax;rectTransformChild.anchorMin = rectTransform.anchorMin;rectTransformChild.offsetMax = rectTransform.offsetMax;rectTransformChild.offsetMin = rectTransform.offsetMin;rectTransformChild.pivot = rectTransform.pivot;rectTransformChild.sizeDelta = rectTransform.sizeDelta;}Reposition();}void Reposition() {Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position);outlineColor.a = textMesh.color.a * textMesh.color.a;textMesh.color = outlineColor;// copy attributesfor (int i = 0; i < transform.childCount; i++) {Text other = transform.GetChild(i).GetComponent<Text>();other.color = outlineColor;other.text = textMesh.text;other.alignment = textMesh.alignment;other.font = textMesh.font;other.fontSize = textMesh.fontSize;other.fontStyle = textMesh.fontStyle;other.lineSpacing = textMesh.lineSpacing;bool doublePixel = resolutionDependant && (Screen.width > doubleResolution || Screen.height > doubleResolution);Vector3 pixelOffset = GetOffset(i) * (doublePixel ? 2.0f * pixelSize : pixelSize);Vector3 worldPoint = Camera.main.ScreenToWorldPoint(screenPoint + pixelOffset);other.transform.position = worldPoint;if(i == transform.childCount-1){other.color = originalColor;}}}Vector3 GetOffset(int i) {switch (i % 8) {case 0: return new Vector3(0, 1, 0);case 1: return new Vector3(1, 1, 0);case 2: return new Vector3(1, 0, 0);case 3: return new Vector3(1, -1, 0);case 4: return new Vector3(0, -1, 0);case 5: return new Vector3(-1, -1, 0);case 6: return new Vector3(-1, 0, 0);case 7: return new Vector3(-1, 1, 0);default: return Vector3.zero;}}
}
查看全文
99%的人还看了
相似问题
- shell 脚本的函数和数组
- shell脚本三
- 二百零六、Flume——Flume1.9.0单机版部署脚本(附截图)
- Linux 启动停止重启jar包shell脚本
- shell脚本用法参数
- ES索引数据清理脚本示例
- hadoop 编写开启关闭集群脚本, hadoop hdfs,yarn开启关闭脚本。傻瓜式hadoop脚本 hadoop(九)
- 暖阳脚本_ 将Agent技术的灵活性引入RPA,清华等发布自动化智能体ProAgent
- Apache Airflow (九) :Airflow Operators及案例之BashOperator及调度Shell命令及脚本
- Redis学习笔记14:基于spring data redis及lua脚本ZSET有序集合实现环形结构案例及lua脚本如何发送到redis服务器
猜你感兴趣
版权申明
本文"[Unity]给场景中的3D字体TextMesh增加描边方案一":http://eshow365.cn/6-24079-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!