測試.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;
///
/// 實現(xiàn)下劃線的方式類型
///
public int Type = 0;
void Awake()
{
SetURLUnderLine(Type);
}
///
/// 設(shè)定隱私協(xié)議文本的下劃線
/// type值為0表示使用“拼接Text:_”方式實現(xiàn),有缺點
/// type值為1表示使用“拉伸Image”方式實現(xiàn)煌茬,比較完美
///
private void SetURLUnderLine(int type)
{
Debug.Log("設(shè)定隱私協(xié)議文本的下劃線,方式:" + 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;
}
}
}