【问题标题】:How to change an input field's content type in c# script with Unity?如何使用 Unity 在 c# 脚本中更改输入字段的内容类型?
【发布时间】:2015-11-12 04:21:27
【问题描述】:

我正在使用 Unity 4.6.9,用 c# 编码,我要做的就是将具有整数内容类型和字符限制为 2 的输入字段的开头更改为具有标准的输入字段内容类型和更大的字符限制。我已经使用 Unity 本身创建了对象本身,所以我不知道如何在脚本中更改它。我要做的是让一个字段负责 2 个整数和一个字符串输入,并且我的整个代码都可以正常工作,除了在程序运行时更改类型和限制。我已经在谷歌上搜索和尝试了将近 2 个小时,所以我希望我没有错过一些东西。我已经尝试过查看 Unity 的文档,但这对我没有任何帮助。

为了回答这个问题,我的输入字段名为 InputField,它所在的类名为 UIInputField,该字段的实际输出名为 tempInput(从该字段发送到程序中的数据)。就像我说的,我只是想更改“内容类型”和“字符限制”,我已经编写了将字符串转换为整数的代码,以及如果输入字段留空时的故障保护,或字符类型错误。

如果我遗漏了回答此问题所必需的任何内容,请告诉我。

编辑:对不起,我忽略了代码,认为它没有必要(认为这将是一个简单的修复,我只是还没有想出)所以这是我到目前为止所获得的代码(我确实知道该程序的某些部分可能会根据用户输入的内容产生问题,并且已经知道如何解决它,但我正在尝试让这个输入问题正常工作,然后再担心这些问题问题):

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

public class UIInputField : MonoBehaviour {
    string[] particNames;
    string[] particInit;
    int subState = 1;
    int state = 1;
    int particVar;
    // Update is called once per frame
    void Update () {

    }

    //Fix substate handling before building.
    public void getInt(string tempInput){
        if (state == 1) {
            if (Int32.TryParse (tempInput, out particVar))
                Debug.Log (particVar);
            particNames = new string[particVar];
            particInit = new string[particVar];
            GameObject.Find ("InputField").characterLimit = 12;
            GameObject.Find ("InputField").ContentType = standard;
            state++;
        } else if (state == 2) {
            particNames [subState] = particVar;
            subState++;
            GameObject.Find ("InputField").characterLimit = 2;
            GameObject.Find ("InputField").ContentType = integerNumber;
            if (subState == particNames.Length){
                state++;
                subState = 1;
            }
        } else if (state == 3) {
            if (Int32.TryParse (tempInput, out particVar))
                Debug.Log (particVar);
            particInit[subState] = particVar;
            subState++;
            if (subState == particNames.Length) /* The code to move on the the next portion of this program will be here once this portion is working properly*/;

        }
    }
}

【问题讨论】:

  • 请用有效代码描述您的问题
  • 对不起,我认为这个问题很简单,不需要它,但我想我应该总是提出来,对吧?我将代码添加到 OP 中。

标签: c# unity3d input-field


【解决方案1】:

在过去几天进行了更多搜索后,我找到了这个问题的答案。它使用:

mainInputField.contentType = InputField.ContentType.Standard;
mainInputField.characterLimit = 12;//Or any integer

此时,mainInputField 只是一个变量,类型为 InputField,您必须进入 UnityEditor 设置该变量以引用实际的输入字段变量。

【讨论】:

猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 2013-03-23
相关资源
最近更新 更多