• [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;
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    【51单片机外部中断控制流水灯转向】2023-10-21
    从 ArcMap 迁移到 ArcGIS Pro
    绁炵粡缃戠粶杈撳叆鍥剧墖澶у皬
    Comparable是排序接口 Comparator是比较器接口
    Java手写大数乘法算法和大数乘法算法应用拓展案例
    腾讯云和阿里云4核8G云服务器多少钱一年和1个月费用对比
    10 分钟讲完 QUIC 协议。
    【C语言趣味教程】(5) 常量:字面常量 | 类型常量 | const 关键字 | const 的声明 | 程序中的只读概念 | const 保护机制 | 如何巧妙区分 “指针常量“ 和 “常量指针“
    Servlet
    如何学习 Linux 内核内存管理
  • 原文地址:https://blog.csdn.net/qq_42980269/article/details/134020030