Unity3dUGUIテキストに下線を追加



Unity3d Ugui Text Adds Underline



1.使いやすく、テキストスクリプトでGameObjectオブジェクトに追加するだけです
2.1つまたは複数の行をサポートします
3.テキストテキストコンテンツの変更に応じて自動的に更新できます
4.効果は次のとおりです。
画像 画像

using System.Collections using System.Collections.Generic using UnityEngine using UnityEngine.EventSystems using UnityEngine.UI class UnderlineProperty { public Color _color public Vector3 _position public float _width public float _height public Vector2 _privot } public class MultipleLinkButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { private Text _text private int _curCharacterCount = 0 private List _lines = new List() Private System.Action _clickEvent = null //underline click event private bool _isInitUnderline = false public System.Action ClickEvent { get { return _clickEvent } set { _clickEvent = value } } // Use this for initialization void Start () { _text = transform.GetComponent() _text.gameObject.AddComponent().onClick.AddListener(()=> { if (ClickEvent != null) ClickEvent() }) } void Update() { / / Do the initialization if (_text.cachedTextGenerator.lineCount > 0 && !_isInitUnderline) { _isInitUnderline = true _curCharacterCount = _text.cachedTextGenerator.characterCount List list = GetUnderlinePropertys() CreateUnderLines(list) } //Refresh if (_isInitUnderline && _curCharacterCount != _text.cachedTextGenerator.characterCount) { _curCharacterCount = _text.cachedTextGenerator.characterCount List list = GetUnderlinePropertys() CreateUnderLines(list) } } List GetUnderlinePropertys() { List list = new List() for (int i = 0 i <_text.cachedTextGenerator.lineCount i++) { var curPos = _text.cachedTextGenerator.characters[_text.cachedTextGenerator.lines[i].startCharIdx].cursorPos UnderlineProperty property = new UnderlineProperty { _color = _text.color, _height = _text.fontSize / 10 == 0 ? 1 : _text.fontSize / 10, _width = GetWidth(_text.cachedTextGenerator.lines[i].startCharIdx, _text.cachedTextGenerator.characters), _position = new Vector3(curPos.x, curPos.y - _text.cachedTextGenerator.lines[i].height, 0), _privot = GetTextAnchorPivot(_text.alignment) } list.Add(property) } return list } float GetWidth(int idx, IList info) { float width = 0 float start = info[idx].cursorPos.x for (int i = idx i info[i + 1].cursorPos.x) { width = info[i].cursorPos.x - start break } if (info.Count - 1 == i + 1) width = info[i + 1].cursorPos.x - start } return width } Vector2 GetTextAnchorPivot(TextAnchor anchor) { switch (anchor) { case TextAnchor.LowerLeft: return new Vector2(0, 0) case TextAnchor.LowerCenter: return new Vector2(0.5f, 0) case TextAnchor.LowerRight: return new Vector2(1, 0) case TextAnchor.MiddleLeft: return new Vector2(0, 0.5f) case TextAnchor.MiddleCenter: return new Vector2(0.5f, 0.5f) case TextAnchor.MiddleRight: return new Vector2(1, 0.5f) case TextAnchor.UpperLeft: return new Vector2(0, 1) case TextAnchor.UpperCenter: return new Vector2(0.5f, 1) case TextAnchor.UpperRight: return new Vector2(1, 1) default: return Vector2.zero } } void CreateUnderLines(List list) { for (int i = 0 i < transform.childCount i++) Destroy(transform.GetChild(i).gameObject) _lines.Clear() for (int i = 0 i < list.Count i++) } /* achieve underlined click effect*/ public void OnPointerDown(PointerEventData eventData) { for (int i = 0 i < _lines.Count i++) { Color[] colors = _lines[i].sprite.texture.GetPixels() for (int j = 0 j < colors.Length j++) colors[j] = new Color(colors[j].r, colors[j].g, colors[j].b, colors[j].a * 0.70f) _lines[i].sprite.texture.SetPixels(colors) _lines[i].sprite.texture.Apply() } } public void OnPointerUp(PointerEventData eventData) { for (int i = 0 i < _lines.Count i++) { Color[] colors = _lines[i].sprite.texture.GetPixels() for (int j = 0 j < colors.Length j++) colors[j] = new Color(colors[j].r, colors[j].g, colors[j].b, colors[j].a / 0.70f) _lines[i].sprite.texture.SetPixels(colors) _lines[i].sprite.texture.Apply() } } }

追加情報:



  1. 他の人のオンラインを参照して同様のテキストをインスタンス化し、「_」を使用して下線を付けます。非等幅文字では下線が表示され、表示テキストの長さが一貫しないため、画像を使用して下線を付けました
  2. リンクを実装するために他の誰かを参照してください: https://blog.csdn.net/bszk81340089/article/details/50017317
  3. デモダウンロードアドレス: https://github.com/a834286983/project/raw/master/Underline.zip
  4. 不十分な点はアドバイスを歓迎します:root @ xxxxx