UGUI中的文本添加链接下划线并跳转

哈哈,想了很久,试了好几种方法,最后还是自己写一下

先看代码

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


public class BtnTest : MonoBehaviour {
    public Button btnselect;//不用在意这是测试的委托代码
    public Text linkText;
    void Awake()
    {
        linkText = GameObject.Find("Canvas").transform.Find("Text").GetComponent<Text>();


    }
    void Start()
    {
        CreateLink(linkText);
        //给按钮绑定事件
        ButtonEvent.Get(btnselect.transform).clickObj += selectOnClick;
        // ButtonEvent.Get(textlint.transform).clickObj += onclick;
    }  


    void selectOnClick()
    {


        Debug.Log("Test is Sucess");
       
    }
  public  void onclick()
    {
        Application.OpenURL("www.baidu.com");
    }
    //创建下划线
  public void CreateLink(Text text)
  {
      if (text == null)
          return;


      //克隆Text,获得相同的属性  
      Text underline = Instantiate(text) as Text;
      underline.name = "Underline";


      underline.transform.SetParent(text.transform);
      RectTransform rt = underline.rectTransform;


      //设置下划线坐标和位置  
      rt.anchoredPosition3D = Vector3.zero;
      rt.offsetMax = Vector2.zero;
      rt.offsetMin = Vector2.zero;
      rt.anchorMax = Vector2.one;
      rt.anchorMin = Vector2.zero;


      underline.text = "_";
      float perlineWidth = underline.preferredWidth;      //单个下划线宽度  
      Debug.Log(perlineWidth);


      float width = text.preferredWidth;
      
      int lineCount = (int)Mathf.Round(width / perlineWidth);
   
      for (int i = 1; i < lineCount; i++)
      {
          underline.text += "_";
      }
  }  
}


不足之处请大家多多指教

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值