TextSpacingPro

using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;

[DisallowMultipleComponent]
[AddComponentMenu("UI/ToolKit/TextColumnSpacing")]
public class TextColumnSpacing : BaseMeshEffect
{
    public float columnSpacing = 0f;
    
    private float _alignmentFactor = 0f;
    
    public override void ModifyMesh(VertexHelper toFill)
    {
        if (columnSpacing == 0)
            return;
        
        if (!IsActive() || toFill.currentVertCount == 0)
            return;
        
        Text textCmp = GetComponent<Text>();
        if (textCmp == null)
        {
            Debug.LogError("Missing Text component");
            return;
        }
        
        _alignmentFactor = GetAlignmentFactor(textCmp.alignment);
        IList<UILineInfo> lineInfos = textCmp.cachedTextGenerator.lines;
        
        for (int i = 0; i < lineInfos.Count; i++)
        {
            UILineInfo line = lineInfos[i];
            if (i + 1 < lineInfos.Count)
            {
                int current = 0;
                int lineLength = lineInfos[i + 1].startCharIdx;
                for (int j = line.startCharIdx; j < lineLength; j++)
                {
                    ModifyText(toFill, j, current++, lineLength);
                }
            }
            else if (i + 1 == lineInfos.Count)    // The last line.
            {
                int current = 0;
                int lineLength = textCmp.cachedTextGenerator.characterCountVisible;
                for (int j = line.startCharIdx; j < lineLength; j++)
                {
                    ModifyText(toFill, j, current++, lineLength);
                }
            }
        }
    }
    
    void ModifyText(VertexHelper helper, int i, int charYPos, int lineLength)
    {
        float percent = (charYPos * 1.0f) / (lineLength * 1.0f);
        int factor = 0;
        if (percent < _alignmentFactor)
        {
            factor = -1;
        }
        else if(percent > _alignmentFactor)
        {
            factor = 1;
        }
        
        float lineOffset = charYPos * columnSpacing * factor;
        Vector3 offset = Vector3.right * ((columnSpacing * charYPos) + lineOffset);

        for (int j = 0; j < 4; j++)
        {
            UIVertex vertex = new UIVertex();
            helper.PopulateUIVertex(ref vertex, i * 4 + j);
            vertex.position += offset;
            helper.SetUIVertex(vertex, i * 4 + j);
        }
    }
    
    float GetAlignmentFactor(TextAnchor alignment)
    {
        switch (alignment)
        {
            default:
                Debug.LogError("Invalid Convert Here!");
                return 0;
            case TextAnchor.UpperLeft:
            case TextAnchor.MiddleLeft:
            case TextAnchor.LowerLeft:
                return 0;
            case TextAnchor.UpperCenter:
            case TextAnchor.MiddleCenter:
            case TextAnchor.LowerCenter:
                return .5f;
            case TextAnchor.UpperRight:
            case TextAnchor.MiddleRight:
            case TextAnchor.LowerRight:
                return 1f;
        }
    }
}
修正

using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;

[DisallowMultipleComponent]
[AddComponentMenu("UI/ToolKit/TextSpacingPro")]
public class TextSpacingPro : BaseMeshEffect
{
	public float _textSpacing = 1f;
	
	//Rich Text Regex Patterns
	private const string m_RichTextRegexPatterns = @"<b>|</b>|<i>|</i>|<size=.*?>|</size>|<Size=.*?>|</Size>|<color=.*?>|</color>|<Color=.*?>|</Color>|<material=.*?>|</material>";
 
    public override void ModifyMesh(VertexHelper toFill)
    {
	    if (_textSpacing == 0)
		    return;
	    
	    if (!IsActive() || toFill.currentVertCount == 0)
		    return;
	    
	    Text textCmp = GetComponent<Text>();
	    if (textCmp == null)
	    {
		    Debug.LogError("Missing Text component");
		    return;
	    }
	    
	    List<UIVertex> vertexes = new List<UIVertex>();
	    toFill.GetUIVertexStream(vertexes);
	    
	    List<List<int>> linesVertexStartIndexes = GetLinesVertexStartIndexes(textCmp);
	    float alignmentFactor = GetAlignmentFactor(textCmp.alignment);
	    
	    for (int i = 0; i < linesVertexStartIndexes.Count; i++)
	    {
		    //行偏移(左中右) 
		    float lineOffset = (linesVertexStartIndexes[i].Count - 1) * _textSpacing * alignmentFactor;
		    for (int j = 0; j < linesVertexStartIndexes[i].Count; j++)
		    {
			    int vertexStartIndex = linesVertexStartIndexes[i][j];
			    Vector3 offset = Vector3.right * ((_textSpacing * j) - lineOffset);
			    //对每个有效字体的六个顶点偏移
			    AddVertexOffset(vertexes, vertexStartIndex + 0, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 1, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 2, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 3, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 4, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 5, offset);
		    }
	    }
        
	    toFill.Clear();
	    toFill.AddUIVertexTriangleStream(vertexes);
    }
    
	//获取有效的初始顶点
    List<List<int>> GetLinesVertexStartIndexes(Text textCmp)
    {
        List<List<int>> linesVertexIndexes = new List<List<int>>();
        IList<UILineInfo> lineInfos = textCmp.cachedTextGenerator.lines;

        int startIndex = -1;
        for(int i=0;i<lineInfos.Count;i++)
        {
            List<int> lineVertexStartIndex = new List<int>();
            int lineStart = lineInfos[i].startCharIdx;
            int lineLength = (i < lineInfos.Count - 1) ? lineInfos[i + 1].startCharIdx - lineInfos[i].startCharIdx:textCmp.text.Length - lineInfos[i].startCharIdx;
            
			//Rich Text根据正则表达式获取需要忽略的初始顶点
            List<int> ignoreIndexes = new List<int>();
            if (textCmp.supportRichText)
            {
                string line = textCmp.text.Substring(lineStart, lineLength);
                var matchCollection = Regex.Matches(line, m_RichTextRegexPatterns);
                foreach (Match matchTag in matchCollection)
                {
                    for(int j=0; j<matchTag.Length; j++)
                        ignoreIndexes.Add(matchTag.Index + j);
                }
            }
            
            for (int j = 0; j < lineLength; j++)
	            if (ignoreIndexes.Count == 0 || !ignoreIndexes.Contains(j))
		            lineVertexStartIndex.Add((++startIndex)*6);
            
            // for (int j = 0; j < lineLength; j++)
	           //  if (ignoreIndexes.Count == 0 || !ignoreIndexes.Contains(j))
		          //   lineVertexStartIndex.Add((lineStart+j)*6);

            linesVertexIndexes.Add(lineVertexStartIndex);
        }
        return linesVertexIndexes;
    }
    
    //对顶点进行偏移
    void AddVertexOffset(List<UIVertex> vertexes,int index,Vector3 offset)
    {
	    if (index >= vertexes.Count)
	    {
		    Debug.Log(index);    
	    }
	    
	    UIVertex vertex = vertexes[index];
	    vertex.position += offset;
	    vertexes[index] = vertex;
    }
	
	//获取行偏移比例参数
    float GetAlignmentFactor(TextAnchor alignment)
    {
        switch (alignment)
        {
            default:
                Debug.LogError("Invalid Convertions Here!");
                return 0;
            case TextAnchor.UpperLeft:
            case TextAnchor.MiddleLeft:
            case TextAnchor.LowerLeft:
                return 0;
            case TextAnchor.UpperCenter:
            case TextAnchor.MiddleCenter:
            case TextAnchor.LowerCenter:
                return .5f;
            case TextAnchor.UpperRight:
            case TextAnchor.MiddleRight:
            case TextAnchor.LowerRight:
                return 1f;
        }
    }
}

TextSpacingPro
using UnityEngine.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;

[AddComponentMenu("UI/Effects/TextSpacingPro")]
public class TextSpacingPro : BaseMeshEffect
{
	public float _textSpacing = 1f;
	
	//正则表达式参数
	private const string m_RichTextRegexPatterns = @"<b>|</b>|<i>|</i>|<size=.*?>|</size>|<Size=.*?>|</Size>|<color=.*?>|</color>|<Color=.*?>|</Color>|<material=.*?>|</material>";

    public override void ModifyMesh(VertexHelper toFill)
    {
	    if (_textSpacing == 0)
		    return;
	    
	    if (!IsActive() || toFill.currentVertCount == 0)
		    return;
	    
	    Text textCmp = GetComponent<Text>();
	    if (textCmp == null)
	    {
		    Debug.LogError("Missing Text component");
		    return;
	    }
	    
	    List<UIVertex> vertexes = new List<UIVertex>();
	    toFill.GetUIVertexStream(vertexes);
	    
	    List<List<int>> linesVertexStartIndexes = GetLinesVertexStartIndexes(textCmp);
	    float alignmentFactor = GetAlignmentFactor(textCmp.alignment);
	    
	    for (int i = 0; i < linesVertexStartIndexes.Count; i++)
	    {
		    //行偏移(左中右) 
		    float lineOffset = (linesVertexStartIndexes[i].Count - 1) * _textSpacing * alignmentFactor;
		    for (int j = 0; j < linesVertexStartIndexes[i].Count; j++)
		    {
			    int vertexStartIndex = linesVertexStartIndexes[i][j];
			    Vector3 offset = Vector3.right * ((_textSpacing * j) - lineOffset);
			    //对每个有效字体的六个顶点偏移
			    AddVertexOffset(vertexes, vertexStartIndex + 0, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 1, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 2, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 3, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 4, offset);
			    AddVertexOffset(vertexes, vertexStartIndex + 5, offset);
		    }
	    }
        
	    toFill.Clear();
	    toFill.AddUIVertexTriangleStream(vertexes);
    }
    
    //对顶点进行偏移
    void AddVertexOffset(List<UIVertex> vertexes,int index,Vector3 offset)
    {
        UIVertex vertex = vertexes[index];
        vertex.position += offset;
        vertexes[index] = vertex;
    }
	//获取有效的初始顶点
    List<List<int>> GetLinesVertexStartIndexes(Text textCmp)
    {
        List<List<int>> linesVertexIndexes = new List<List<int>>();
        IList<UILineInfo> lineInfos = textCmp.cachedTextGenerator.lines;
        for(int i=0;i<lineInfos.Count;i++)
        {
            List<int> lineVertexStartIndex = new List<int>();
            int lineStart = lineInfos[i].startCharIdx;
            int lineLength = (i < lineInfos.Count - 1) ? lineInfos[i + 1].startCharIdx - lineInfos[i].startCharIdx:textCmp.text.Length - lineInfos[i].startCharIdx;
            
			//Rich Text根据正则表达式获取需要忽略的初始顶点
            List<int> ignoreIndexes = new List<int>();
            if (textCmp.supportRichText)
            {
                string line = textCmp.text.Substring(lineStart, lineLength);
                foreach (Match matchTag in Regex.Matches(line, m_RichTextRegexPatterns))
                {
                    for(int j=0;j<matchTag.Length;j++)
                        ignoreIndexes.Add(matchTag.Index + j);
                }
            }
 
            for (int j = 0; j < lineLength; j++)
                if (!ignoreIndexes.Contains(j))
                    lineVertexStartIndex.Add((lineStart+j)*6);
 
            linesVertexIndexes.Add(lineVertexStartIndex);
        }
        return linesVertexIndexes;
    }
	
	//获取行偏移比例参数
    float GetAlignmentFactor(TextAnchor alignment)
    {
        switch (alignment)
        {
            default:
                Debug.LogError("Invalid Convertions Here!");
                return 0;
            case TextAnchor.UpperLeft:
            case TextAnchor.MiddleLeft:
            case TextAnchor.LowerLeft:
                return 0;
            case TextAnchor.UpperCenter:
            case TextAnchor.MiddleCenter:
            case TextAnchor.LowerCenter:
                return .5f;
            case TextAnchor.UpperRight:
            case TextAnchor.MiddleRight:
            case TextAnchor.LowerRight:
                return 1f;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值