Unity中InputField一些属性的理解

        先看代码:

using UnityEngine;
using UnityEngine.UI;

public class TestInput : MonoBehaviour
{
    [SerializeField]
    InputField inputField;
    void Start()
    {
        Debug.Log(inputField.text);
		Debug.Log(inputField.text.Length);
		Debug.Log(inputField.preferredWidth);
		Debug.Log(inputField.textComponent.text);
		Debug.Log(inputField.textComponent.text.Length);
		Debug.Log(inputField.textComponent.preferredWidth);
	}
}

        再看输出结果:

        这里InputField的输入内容是:"abcdefghijklmnopqrstuvwxyz",这个是实际输入的内容,打印出来的字母数量是26,适配宽度preferredWidth是181。

        但是InputField的属性textComponent的文本内容是"abcdefghijklmno",也就是文本输入框内部实际显示的内容,所以它的字母数量是15,适配宽度preferredWidth是103。

        这里需要说明的是以前不知道InputField有个preferredWidth属性,以为只有Text类的对象才有这个属性,而且以前把inputField.textComponent.text和inputField.text混为一谈,闹了不少笑话,哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值