[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;
		}
	}
}
相关推荐
FairGuard手游加固1 小时前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
wuyk5554 小时前
PyQt6-第05讲-QtDesigner工作流与ui文件
python·ui·pyqt
Meta Luo5 小时前
UI 自动化测试中的等待、超时和异步加载怎么处理
功能测试·测试工具·ui·回归·自动化
Light Gao7 小时前
AI 对话助手如何实现动态交互?一文讲清 AG-UI、SSE、组件与工具调用
人工智能·ui·交互·大语言模型·sse·智能体·ag-ui
下页、再停留8 小时前
【C#桌面客户端系列学习-11】将SQLite数据库打包到exe里
数据库·c#·visual studio
下页、再停留8 小时前
【C#桌面客户端系列学习-12】封装SQLite数据库基础操作工具类(CURD)
数据库·c#·visual studio
MicrosoftReactor10 小时前
技术速递|当聊天框不再是合适的交互界面
ui·ai·copilot·canvas
dwm8888888810 小时前
c# 创建使用本地NuGet包
开发语言·c#
进击的大海贼11 小时前
【无标题】
c#
秋名山小白12 小时前
UI自动化-Airtest框架落地实战
运维·ui·自动化