当前位置: 首页 > 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/news/107484.html

相关文章:

  • 指针仪表读数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电脑怎么永久性彻底删除文件?
  • AI与Prompt:解锁软件开发团队的魔法咒语,在复杂任务上生成正确率更高的代码
  • 如何能在项目具体编码实现之前能尽可能早的发现问题并解决问题
  • jenkins自动化操作步骤(gitblit)
  • SDK 资源
  • 文本处理方法及其在NLP中的应用
  • 获取IEEE会议论文的标题和摘要
  • Maven - 国内 Maven 镜像仓库(加速包,冲冲冲~)
  • 设计模式(15)组合模式
  • Ubuntu安装pyenv,配置虚拟环境
  • 二维码智慧门牌管理系统升级解决方案:采集项目的建立与运用