gpt4 book ai didi

c# - Unity 引用输入字段文本框

转载 作者:行者123 更新时间:2023-12-02 01:53:23 25 4
gpt4 key购买 nike

这里完全是菜鸟:我想做的是一个简单的注册屏幕,其中文本输入字段可以保存到 SQLite 数据库中。正如你所看到的,其中大部分都是预定义的字符串,因为我仍在测试它。我有一个应该插入值的脚本,它附加到一个按钮,并在单击后调用 InsertScore 函数。插入功能有效,只是我现在需要将输入字段中的文本存储到变量中或直接在此处引用它: cmd.Parameters.Add(new SqliteParameter("@EmployeeID",

无论如何,为什么我不能在 C# 中引用 txtEmployeeID(我的输入字段的名称)?或者更好的是,请告诉我如何引用 txtEmployeeID 输入字段。这样我就可以获取其中的字符串或文本并将其保存到数据库中。

public void InsertScore()
{

connectionString = "URI=file:" + Application.dataPath + "/HighScoreDB.sqlite";

using (SqliteConnection con = new SqliteConnection(connectionString))
{



SqliteCommand cmd = new SqliteCommand();
cmd.CommandText = @"INSERT INTO HighScores(EmployeeID, EmployeeFirstname, TestScore, ROLE) VALUES (@EmployeeID,@EmployeeFirstName,@TestScore,@Role)";
cmd.Connection = con;
//cmd.Parameters.Add(new SqliteParameter("@EmployeeID", "222222"));
//cmd.Parameters.Add(new SqliteParameter("@EmployeeID", gameObject.GetComponent<input>
cmd.Parameters.Add(new SqliteParameter("@EmployeeFirstName", "Anand3"));
cmd.Parameters.Add(new SqliteParameter("@TestScore", "65"));
cmd.Parameters.Add(new SqliteParameter("@Role", "Tester"));

con.Open();

cmd.ExecuteNonQuery();

}

}



最佳答案

how to reference the txtEmployeeID input field

使用 GameObject.Find("txtEmployeeID") 查找输入 GameObject,然后对返回的 GameObject 执行 GetComponent 以获取 InputField 组件。

private InputField employerIdinputField;

void Start()
{
employerIdinputField = GameObject.Find("txtEmployeeID").GetComponent<InputField>();

string dataFromInput = employerIdinputField.text;
}

要将其合并到您当前的代码中:

private InputField employerIdinputField;

void Start()
{
employerIdinputField = GameObject.Find("txtEmployeeID").GetComponent<InputField>();
}

public void InsertScore()
{

connectionString = "URI=file:" + Application.dataPath + "/HighScoreDB.sqlite";

using (SqliteConnection con = new SqliteConnection(connectionString))
{

SqliteCommand cmd = new SqliteCommand();
cmd.CommandText = @"INSERT INTO HighScores(EmployeeID, EmployeeFirstname, TestScore, ROLE) VALUES (@EmployeeID,@EmployeeFirstName,@TestScore,@Role)";
cmd.Connection = con;

//FIXED LINE!
cmd.Parameters.Add(new SqliteParameter("@EmployeeID",employerIdinputField.text));
cmd.Parameters.Add(new SqliteParameter("@EmployeeFirstName", "Anand3"));
cmd.Parameters.Add(new SqliteParameter("@TestScore", "65"));
cmd.Parameters.Add(new SqliteParameter("@Role", "Tester"));
con.Open();
cmd.ExecuteNonQuery();
}
}

对于 InputField 事件,请检查 3.InputField 组件 here .

关于c# - Unity 引用输入字段文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42882172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com