untiy3dUGUI实现text文本下划线

untiy3dUGUI实现text文本下划线
测试.png
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 链接下划线的制作
/// </summary>
public class UnderLine : MonoBehaviour
{
    /// 
    /// URL文本
    /// 
    public GameObject URL;
    /// 
    /// URL下划线文本
    /// 
    public GameObject URL_UderLine;
    /// 
    /// URL下划线Image
    /// 
    public GameObject Line_Image;
    /// 
    /// 实现下划线的方式类型
    /// 
    public int Type = 0;


    void Awake()
    {
        SetURLUnderLine(Type);
    }


    /// 
    /// 设定隐私协议文本的下划线
    /// type值为0表示使用“拼接Text:_”方式实现,有缺点
    /// type值为1表示使用“拉伸Image”方式实现,比较完美
    /// 
    private void SetURLUnderLine(int type)
    {
        Debug.Log("设定隐私协议文本的下划线,方式:" + type);

        switch (type)
        {
            case 0:
                //计算URL文本的宽度
                URL.GetComponent<Text>().text = "www.baidu.com";
                float width = URL.GetComponent<Text>().preferredWidth;

                //计算单个下划线宽度  
                Text underLineText = URL_UderLine.GetComponent<Text>();
                underLineText.text = "_";
                float perlineWidth = underLineText.preferredWidth;

                int lineCount = (int)Mathf.Round(width / perlineWidth);
                for (int i = 1; i < lineCount; i++)
                {
                    underLineText.text += "_";
                }
                break;
            case 1:
                //计算URL文本的宽度
                URL.GetComponent<Text>().text = "www.xmutalbaa.com";
                float width2 = URL.GetComponent<Text>().preferredWidth;

                Vector2 curSizeDelta = Line_Image.GetComponent<RectTransform>().sizeDelta;
                //  Line_Image.GetComponent<RectTransform>().pivot = new Vector2(0, 0.5f);
                Line_Image.GetComponent<RectTransform>().sizeDelta = new Vector2(width2, curSizeDelta.y);
                break;
        }
    }
}