Ota*_*win 2 c# unity-game-engine input-field
我正在使用 Unity 4.6.9,用 c# 进行编码,我想做的就是将一开始为整数内容类型且字符限制为 2 的输入字段更改为具有标准内容类型和更大的字符限制。我已经使用 Unity 本身创建了对象本身,所以我不知道如何在脚本中更改它。我想做的是让一个字段负责 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*/;
}
}
}
Run Code Online (Sandbox Code Playgroud)
经过这几天的更多搜索,我找到了这个问题的答案。它用:
mainInputField.contentType = InputField.ContentType.Standard;
mainInputField.characterLimit = 12;//Or any integer
Run Code Online (Sandbox Code Playgroud)
mainInputField 此时只是一个 InputField 类型的变量,您必须进入 UnityEditor 来设置该变量以引用实际的输入字段变量。