【问题标题】:How do i set int value of inputfield with Content Type set to Integer in Unity?如何在 Unity 中将 Content Type 设置为 Integer 的 inputfield 的 int 值设置为?
【发布时间】:2019-07-20 17:43:59
【问题描述】:

我正在使用 Unity 2018。 我有输入框和 2 个按钮。

当我单击按钮时,预期的行为是字段内的计数器会相应更改。例如:

    {
        int current_value = int.Parse(this.craftOrderInput.text);
        if(current_value>0)
            this.craftOrderInput.text = (current_value - 1) + "";
    }

但这显然失败了,因为(current_value - 1) + ""; 是一个字符串,但输入字段需要一个整数。像this.craftOrder.text = (current_value - 1) ; 这样设置整数显然无法编译,因为文本组件需要一个字符串。 我想先试试 Unity 论坛,但我不能在那里提问,因为我的写作风格让他们想起了垃圾邮件。

tldr;如何在 Content Type 设置为 Integer 的情况下设置 inputfield 的 int 值?

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    内容类型只影响允许用户在输入字段中输入的字符。即使将内容类型设置为整数,仍然可以通过将文本更改为字符串来更改输入字段。我做了一个小演示来测试这个,下面的脚本对我有用。

    using UnityEngine;
    using UnityEngine.UI;
    
    public class MakeSmaller : MonoBehaviour {
        InputField inputField;
    
        private void Awake()
        {
            inputField = GetComponent<InputField>();
        }
    
        public void Subtract()
        {
            int orig = int.Parse(inputField.text);
            inputField.text = (orig - 1).ToString();
        }
    }
    

    【讨论】:

    • 我发现了我的错误。我正在设置 inputField 的子对象的 Text 组件,而不是 Inputfield.text,而是更像 inputfield.GetChild(2).GetComponent()。傻我
    猜你喜欢
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2014-08-22
    • 2012-05-22
    • 1970-01-01
    • 2016-10-29
    相关资源
    最近更新 更多