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

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

csharp 复制代码
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;
		}
	}
}
相关推荐
njsgcs22 分钟前
c# solidworks GetPartBox无法获得正确实体边界框原因
开发语言·c#·solidworks
rockey62737 分钟前
AScript之匿名类型与动态类型
c#·.net·script·eval·expression·动态脚本
99乘法口诀万物皆可变1 小时前
BMS HIL 自动化测试框架方案(基于 CANoe + C# + Excel)
开发语言·c#·excel
祀爱1 小时前
定时任务之BackgroundService的详细教程
后端·c#·asp.net
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_50:(深入理解 DOM 中的 Text 节点)
前端·javascript·microsoft·ui·html·媒体
ZC跨境爬虫2 小时前
跟着 MDN 学 HTML day_51:(深入理解 XPathEvaluator 接口)
前端·javascript·ui·html·音视频
weixin_428005302 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第3天FewShot少样本测试
人工智能·c#
思麟呀2 小时前
在C++基础上理解CSharp-1
开发语言·c++·c#
键盘飞行员2 小时前
Windsurf + Claude 4.7 前端开发:用 ui-ux-pro-max 根治 “AI 味”、实现全站 UI 统一
前端·ui·ai编程
ZC跨境爬虫3 小时前
跟着 MDN 学 HTML day_53:(深入理解 XPathResult 接口)
前端·javascript·ui·html·音视频