当前位置: 首页 > news >正文

[Unity]给场景中的3D字体TextMesh增加描边方案一

取你的文本对象,简单地添加以下脚本:

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;}}
}
http://www.lryc.cn/news/205042.html

相关文章:

  • TDengine(taos)数据库导出历史数据
  • 算法进修Day-37
  • 服务器之日常整活
  • 交互式 Web 应用 0 基础入门
  • JSONP的安全性较差,那么在跨域情况下,有没有其他更安全的替代方案呢?
  • Slax Linux 获得增强的会话管理和启动参数选项
  • C/C++新冠疫情死亡率 2020年9月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析
  • Adobe Photoshop 基本操作
  • SpringMVC原理及核心组件
  • 【rk3568-linux】 rk3568x_linux-- 编译说明
  • 模拟计算器编程教程,中文编程开发语言工具编程实例
  • Spring Security漏洞防护—HTTP 安全响应头
  • Plooks大型视频在线一起看网站源码
  • 图像处理中底层、高层特征、上下文信息理解
  • 负载均衡的算法(静态算法与动态算法)
  • mac安装jdk
  • WIN11+OPENCV4.8 编译及下载失败处理方法
  • 万宾科技智能井盖传感器怎么使用?
  • Server Name Indication(SNI),HTTP/TLS握手过程解析
  • react项目实现文件预览,比如PDF、txt、word、Excel、ppt等常见文件(腾讯云cos)
  • ES SearchAPI----Query DSL语言
  • 【STM32】HAL库——串口中断只接收到两个字符
  • 页面html结构导出为word或pdf
  • Object.prototype.toString.call() 和 instanceOf 和 Array.isArray() 详解
  • 自学(黑客技术)方法——网络安全
  • CVE-2023-46227 Apache inlong JDBC URL反序列化漏洞
  • MySQL几种方法的数据库备份
  • CI/CD:GitLab-CI 自动化集成/部署 JAVA微服务的应用合集
  • Flask 上传文件,requests通过接口上传文件
  • kvm webvirtcloud 如何添加直通物理机的 USB 启动U盘