如何使用 Unity 在 C# 脚本中更改输入字段的内容类型?
How to change an input field's content type in c# script with Unity?
我正在使用 Unity 4.6.9,用 c# 编码,我想做的就是将以整数内容类型和字符数限制为 2 的输入字段开始的内容更改为具有标准内容类型和更大的字符限制。我已经使用 Unity 本身创建了对象本身,所以我不知道如何在脚本中更改它。我想做的是让一个字段负责 2 个整数和一个字符串输入,并且我的整个代码都可以正常工作,除了在程序 运行 时更改类型和限制。我已经用谷歌搜索和尝试了将近 2 个小时,所以我希望我不会错过任何东西。我已经尝试过查看 Unity 的文档,但这对我没有任何帮助。
为了回答这个问题,我的输入字段被命名为 InputField,它所在的 class 被命名为 UIInputField,该字段的实际输出被命名为 tempInput(从该字段发送的数据进入程序)。就像我说的,我只是想改变 "Content Type" 和 "Character Limit",我已经制定了将字符串转换为整数的代码,以及如果输入字段被保留的故障保护空白,或字符类型错误。
如果我遗漏了任何需要回答这个问题的内容,请告诉我。
编辑:抱歉,我遗漏了代码,认为它没有必要(我认为这将是一个我还没有想出的简单修复)所以这是我所拥有的代码到目前为止(我知道这个程序的某些部分可能会根据用户输入的内容产生问题,并且已经知道如何修复它,但我试图在担心之前让这个输入问题正常工作关于这些问题):
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*/;
}
}
}
经过这两天的多方搜索,我找到了这个问题的答案。它使用:
mainInputField.contentType = InputField.ContentType.Standard;
mainInputField.characterLimit = 12;//Or any integer
mainInputField 此时只是一个 InputField 类型的变量,您必须进入 UnityEditor 设置该变量以引用实际的输入字段变量。
我正在使用 Unity 4.6.9,用 c# 编码,我想做的就是将以整数内容类型和字符数限制为 2 的输入字段开始的内容更改为具有标准内容类型和更大的字符限制。我已经使用 Unity 本身创建了对象本身,所以我不知道如何在脚本中更改它。我想做的是让一个字段负责 2 个整数和一个字符串输入,并且我的整个代码都可以正常工作,除了在程序 运行 时更改类型和限制。我已经用谷歌搜索和尝试了将近 2 个小时,所以我希望我不会错过任何东西。我已经尝试过查看 Unity 的文档,但这对我没有任何帮助。
为了回答这个问题,我的输入字段被命名为 InputField,它所在的 class 被命名为 UIInputField,该字段的实际输出被命名为 tempInput(从该字段发送的数据进入程序)。就像我说的,我只是想改变 "Content Type" 和 "Character Limit",我已经制定了将字符串转换为整数的代码,以及如果输入字段被保留的故障保护空白,或字符类型错误。
如果我遗漏了任何需要回答这个问题的内容,请告诉我。
编辑:抱歉,我遗漏了代码,认为它没有必要(我认为这将是一个我还没有想出的简单修复)所以这是我所拥有的代码到目前为止(我知道这个程序的某些部分可能会根据用户输入的内容产生问题,并且已经知道如何修复它,但我试图在担心之前让这个输入问题正常工作关于这些问题):
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*/;
}
}
}
经过这两天的多方搜索,我找到了这个问题的答案。它使用:
mainInputField.contentType = InputField.ContentType.Standard;
mainInputField.characterLimit = 12;//Or any integer
mainInputField 此时只是一个 InputField 类型的变量,您必须进入 UnityEditor 设置该变量以引用实际的输入字段变量。