NGUI文本列表TextList

本文介绍了一个用于显示聊天内容和访问记录的文本列表组件,详细讲解了其制作过程、常用属性及如何通过Add方法添加文本内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  • 文本列表简介
  • 文本列表的制作
  • 文本列表的属性
  • 文本列表的方法

文本列表的简介

文本列表经常用于聊天内容的显示,访问记录的显示等,并按行的形式进行显示,如下图。

文本列表制作

由于文本主要是用来显示文本内容,所有需要借助一个Label组件,添加TextList组件。

1) 添加Label组件

2)添加BoxCollider

2) 添加TextList组件,并把之前创建好的Label赋值给TextLabel属性

文本列表常用属性


* TextLabel 用来显示文本内容的Label组件
* ScrollBar 指定滚动条
* Style 显示风格 (分为Text文本和Chat聊天模式,默认为Text文本模式)
* Paragraph History:最多可以显示的行数(默认50行)
* scrollValue 文本滚动的值(0-1之间) 0 表示最久,1 接近最新
通过鼠标左键点击,将文本滚动到可滚动内容的一半位置
参考效果:

void Update () {
      //鼠标点击时,将文本滚动到可滚动范围的一半位置
      if (Input.GetMouseButtonDown(0))
      {
          list.scrollValue = 0.5f;
      }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • Add() 向TextList中添加文本内容
    下面通过Add方法,向TextList进行内容添加
    参考效果:

using UnityEngine;
using System.Collections;

public class TextListDemo : MonoBehaviour {

    private UITextList list;
    // Use this for initialization
    void Start () {
          list = this.GetComponent<UITextList>();
          //动态为文本列表添加内容
          for (int i = 0; i < 20; i++)
          {
              list.Add("[ff0000]"+i+"[-]  line  "+i);
          }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

参考案例:
创建TextList并在开始时,将Chat字符添加到TextList中。

using UnityEngine;				
using System.Collections;				

public class GetLabelValue: MonoBehaviour {				
    public UITextList TextList; //持有一个TextList的引用
    public string  Chat;//需要添加到TextList的字符内容
    void Update(){
       TextList.Add(Chat);//添加内容   	
    }			
}
### 实现 NGUI文本文字描边效果 为了在 NGUI 中实现富文本文字的描边效果,可以采用自定义的方法来改进默认的描边机制。传统的 NGUI 描边通过创建四个额外的文字副本放置于原文字四周形成视觉上的描边[^2]。 然而这种方法存在局限性,在描边宽度较大时会出现明显的瑕疵。一种更优的方式是基于单个字符逐像素生成描边,从而确保即使较宽的描边也能保持平滑过渡而不露出底层文字。 下面是一个具体的 C# 脚本示例用于扩展 `UILabel` 类并应用上述原理: ```csharp using UnityEngine; public class CustomLabelStroke : MonoBehaviour { private UILabel label; void Awake(){ label = GetComponent<UILabel>(); } /// <summary> /// 应用高级描边逻辑至指定标签 /// </summary> public void ApplyAdvancedStroke(float thickness){ // 清除原有子物体防止重复绘制 foreach(Transform child in transform){ GameObject.Destroy(child.gameObject); } string originalText = label.text; Color strokeColor = new Color(0, 0, 0); // 黑色描边 // 计算每个方向上需要增加多少层以满足所需厚度 int layersNeeded = Mathf.CeilToInt(thickness / (label.bitmapFont.lineHeight * 0.1f)); for(int i=1;i<=layersNeeded;i++){ CreateOutlinedCopy(originalText,strokeColor,new Vector2(i,i)); // 右下方 CreateOutlinedCopy(originalText,strokeColor,new Vector2(-i,-i));// 左上方 CreateOutlinedCopy(originalText,strokeColor,new Vector2(i,-i));// 右上方 CreateOutlinedCopy(originalText,strokeColor,new Vector2(-i,i)); // 左下方 // 如果当前层数小于总需层数,则还需补充水平垂直方向的一层 if(i < layersNeeded){ CreateOutlinedCopy(originalText,strokeColor,new Vector2(i,0)); // 向右 CreateOutlinedCopy(originalText,strokeColor,new Vector2(-i,0));// 向左 CreateOutlinedCopy(originalText,strokeColor,new Vector2(0,i)); // 向下 CreateOutlinedCopy(originalText,strokeColor,new Vector2(0,-i));// 向上 } } // 恢复原始文本属性 label.color = new Color(1,1,1,1); // 白色前景 label.depth -= 1; // 确保位于所有描边上一层 } /// <summary> /// 创建带有偏移量的颜色拷贝 /// </summary> private void CreateOutlinedCopy(string text, Color color, Vector2 offset){ GameObject go = new GameObject(); go.transform.SetParent(transform,false); go.transform.localPosition += (Vector3)offset; UILabel outlineLabel = go.AddComponent<UILabel>(); outlineLabel.CopyFrom(label); outlineLabel.text = text; outlineLabel.color = color; } } ``` 此脚本实现了更加精细和平滑的描边处理方案,适用于希望获得高质量视觉呈现的应用场景。需要注意的是,尽管该方法理论上只占用一次 Draw Call,但在实际开发过程中仍应关注其对性能的影响,并考虑使用对象池技术进一步优化资源利用效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值