unity 如何获取Text组件里text内容的长度

 更新时间:2021年04月13日 09:27:28   作者:贪玩的孩纸时代  
这篇文章主要介绍了unity 获取Text组件里text内容的长度操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

/// <summary>
    /// 计算字符串在指定text控件中的长度
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    int CalculateLengthOfText(string message,Text tex)
    {
        int totalLength = 0;
        Font myFont = tex.font;  //chatText is my Text component
        myFont.RequestCharactersInTexture(message, tex.fontSize, tex.fontStyle);
        CharacterInfo characterInfo = new CharacterInfo(); 
        char[] arr = message.ToCharArray(); 
        foreach (char c in arr)
	{
            myFont.GetCharacterInfo(c, out characterInfo, tex.fontSize); 
            totalLength += characterInfo.advance;
        } 
        return totalLength;
    }

补充:用Unity的TextAsset读取TXT文档内容,将物品信息存入字典中

Text Asset 文本资源

Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:

文本资源是导入的文本文件的一个格式。当你拖入一个文本文件到你的项目时,他会被转换为文本资源。支持的文本格式有:

.txt .html .htm .xml .bytes

Properties 属性

Text

The full text of the asset as a single string.

物品属性分析

之后需要新建一个文本txt来存放物品的属性,这里以药品为例

TXT文档内容

每一行文本对应一种物品,用逗号分隔,每个值对应属性表里的每一列属性。

之后新建c#脚本解析文本内容

private TextAsset objectsInfoListText; // 新建TextAsset变量
private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int,ObjectInfo>();
void Awake()
{
    objectsInfoListText = Resources.Load("ObjectsInfoList") as TextAsset; // 从Resouces的path中读取到文件
    ReadInfo();
}
void ReadInfo()
{
     string text = objectsInfoListText.text;  // 将文本内容作为字符串读取
     string[] objInfoArray = text.Split('\n'); // 以\n为分割符将文本分割为一个数组
     foreach (string str in objInfoArray)  // 遍历每一行并将每一个药品的值放入类对象中,并存入字典
     {
         ObjectInfo info = new ObjectInfo();
         string[] propertyArray = str.Split(',');
         int id = int.Parse(propertyArray[0]);
         string name = propertyArray[1];
         string icon_name = propertyArray[2];
         string str_type = propertyArray[3];
         ObjectType type = ObjectType.Drug;
         switch (str_type)
         {  
             case "Equip":
                 type = ObjectType.Equip;
                 break;
             case "Mat":
                 type = ObjectType.Mat;
                 break;
             case "Drug":
                 type = ObjectType.Drug;
                 break;
         }
         info.id = id; info.name = name; 
         info.icon_name = icon_name; info.type = type;
         if (type == ObjectType.Drug)
         {
             int hp = int.Parse(propertyArray[4]);
             int mp = int.Parse(propertyArray[5]);
             int price_sell = int.Parse(propertyArray[6]);
             int price_buy = int.Parse(propertyArray[7]);
             info.hp = hp; info.mp = mp; info.price_sell = price_sell; info.price_buy = price_buy;
         }
         // 将物品信息存储到字典中
         objectInfoDict.Add(id, info);
     }
}
public enum ObjectType
{
    Drug,
    Equip,
    Mat,
};
public class ObjectInfo 
{
    public int id;
    public string name;
    public string icon_name;
    public ObjectType type;
    public int hp;
    public int mp;
    public int price_sell;
    public int price_buy;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • winform中的ListBox和ComboBox绑定数据用法实例

    winform中的ListBox和ComboBox绑定数据用法实例

    这篇文章主要介绍了winform中的ListBox和ComboBox绑定数据用法,实例分析了将集合数据绑定到ListBox和ComboBox控件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • 在C#中global关键字的作用及其用法

    在C#中global关键字的作用及其用法

    global 是 C# 2.0 中新增的关键字,理论上说,如果代码写得好的话,根本不需要用到它,但是不排除一些特别的情况,比如修改别人的代码,本文仅举例说明。
    2016-03-03
  • C#常见算法面试题小结

    C#常见算法面试题小结

    这篇文章主要介绍了C#常见算法面试题,包含了常见的排序、字符串操作、类的操作等技巧,需要的朋友可以参考下
    2014-09-09
  • 基于c# 接口的实例详解

    基于c# 接口的实例详解

    本篇文章是对c#中的接口进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#给图片添加水印完整实例

    C#给图片添加水印完整实例

    这篇文章主要介绍了C#给图片添加水印的方法,以完整实例形式分析了C#实现文字及图像水印、缩略图、图片剪切等相关技巧,需要的朋友可以参考下
    2015-12-12
  • C#实现多选项卡的浏览器控件

    C#实现多选项卡的浏览器控件

    这篇文章主要为大家详细介绍了C#实现多选项卡的浏览器控件的相关资料,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • C#程序执行时间长查询速度慢解决方案

    C#程序执行时间长查询速度慢解决方案

    这篇文章主要介绍了C#程序执行时间长查询速度慢解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • C# winform实现右下角弹出窗口结果的方法

    C# winform实现右下角弹出窗口结果的方法

    这篇文章主要介绍了C# winform实现右下角弹出窗口结果的方法,结合实例形式分析了C#窗口操作的相关技巧,需要的朋友可以参考下
    2017-06-06
  • C#中的延时函数sleep

    C#中的延时函数sleep

    这篇文章主要介绍了C#中的延时函数sleep用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 详解c#与js的rsa加密互通

    详解c#与js的rsa加密互通

    这篇文章主要介绍了详解c#与js的rsa加密互通,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03

最新评论