[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;
		}
	}
}
相关推荐
独行soc2 小时前
#渗透测试#SRC漏洞挖掘#深入挖掘XSS漏洞02之测试流程
web安全·面试·渗透测试·xss·漏洞挖掘·1024程序员节
Envyᥫᩣ2 小时前
C#语言:从入门到精通
开发语言·c#
MediaTea3 小时前
七次课掌握 Photoshop:选区与抠图
ui·photoshop
XuanRanDev6 小时前
【每日一题】LeetCode - 三数之和
数据结构·算法·leetcode·1024程序员节
小春熙子7 小时前
Unity图形学之Shader结构
unity·游戏引擎·技术美术
不秃头的UI设计师7 小时前
UI界面设计入门:打造卓越用户体验
ui·ux·ui设计
未来的嗒嘀嗒8 小时前
Axure是什么软件?全方位解读助力设计入门
ui·photoshop
IT技术分享社区8 小时前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
Sitarrrr9 小时前
【Unity】ScriptableObject的应用和3D物体跟随鼠标移动:鼠标放置物体在场景中
3d·unity
极梦网络无忧9 小时前
Unity中IK动画与布偶死亡动画切换的实现
unity·游戏引擎·lucene