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

[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 attributes
		for (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.kler.cn/a/107484.html

相关文章:

  • 卷积、频域乘积和矩阵向量乘积三种形式之间的等价关系与转换
  • u盘加密软件有哪些?2025年必备的u盘加密神器分享(共6款!提前布局!)
  • Prometheus面试内容整理-Exporters
  • ⾃动化运维利器Ansible-基础
  • 〔 MySQL 〕数据类型
  • 【计算机网络】UDP网络程序
  • 指针仪表读数YOLOV8NANO
  • GPT出大错了!原来GPT不是万能的!这就是人类存在的意义!
  • sqoop和flume简单安装配置使用
  • Systemverilog中使用interface连接testbench和dut的端口
  • 铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)rust解法
  • Kubernetes - Ingress HTTP 负载搭建部署解决方案(新版本v1.21+)
  • [双指针] Leetcode 283.移动零和1089.复习零
  • 设计模式:访问者模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • AT32固件库外设使用,ArduinoAPI接口移植,模块化
  • windows服务器环境下使用php调用com组件
  • 【springBoot】博客系统
  • 10000字!图解机器学习特征工程
  • Vmware下的虚拟机NAT连接后仍然木有网络
  • Redis的过期键删除策略看这一篇就足够了
  • Could not update Flowable database schema: unknown version from database:
  • C++——类和对象(中)(2)尚未完结
  • Angular-02:环境等说明
  • Crawler4j实例爬取爱奇艺热播剧案例
  • 技术栈 业务架构 插件库
  • mac电脑怎么永久性彻底删除文件?