我想将一个场景的值传递给Unity中的另一个场景

时间:2019-11-23 16:41:33

标签: c# .net visual-studio sqlite unity3d

我正在创建一个用于维护商店库存的应用程序。因此,我创建了一个登录场景和一个注册场景。

因此,当我单击登录时,它会激活另一个场景名称stockMaintainer。

基本上,我想将用户名从前一个场景传递到新场景,任何人都可以解释我该怎么做?

这是我的登录场景代码,我在其中与现有数据库中的现有数据进行匹配并加载新的场景。

我正在使用Sqlite。

private void Start() 
{
    //Data base path set
    string pathToDb = "URI=file:" + Application.dataPath + "/login.sqlite";

    //Makking Connection with the sqlite file
    dbconnector = (IDbConnection) new SqliteConnection(pathToDb);
    //Open connection to the database
    dbconnector.Open();

    //dbcmd is Command promt for executing command of sqllite
    IDbCommand dbcmd = dbconnector.CreateCommand();

    /*dbcmd.CommandText = "CREATE TABLE user" 
        + "("
        + "user_id TEXT PRIMARY KEY NOT NULL,"
        + "password TEXT NOT NULL"
        + ")";
    dbcmd.ExecuteReader();
    dbcmd.Dispose();*/

    /*dbcmd.CommandText = "INSERT INTO user(user_id,password)"
                            + "VALUES('pritam','password')";
    dbcmd.ExecuteReader();
    dbcmd.Dispose();*/

    string getVlaueDB = "SELECT user_id,password FROM user";
    dbcmd.CommandText = getVlaueDB;
    reader = dbcmd.ExecuteReader();
    dbcmd.Dispose();
    //while(reader.Read()) {Debug.Log(reader.GetString(0));}
    //reader.Close();
    //dbconn.Close();
}
public void login() 
{
    userId = userIdInput.text;
    password = passwordInput.text;
    while(reader.Read()) 
    {
        string _userID = reader.GetString(0);
        string _password = reader.GetString(1);
        //cheaking for user id and password
        //reader.getString(Index)
        if (userId == _userID && password == _password) 
        {
            reader.Close();
            dbconnector.Close();
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    } 
}

这是我想要获取用户名的股票代码

void Start()
{
    loginActivity = FindObjectOfType<GameObject>().GetComponent<LoginActivity>();

    string path = "URI=file:" + Application.dataPath + "/stock.sqlite";
    dbConnector = (IDbConnection) new SqliteConnection(path);
    dbConnector.Open();

    IDbCommand dbCommand = dbConnector.CreateCommand();
    //string getData = "SELECT shop_name FROM user WHERE user_id=" + "'" + loginActivity.usrId + "'";
    //dbCommand.CommandText = getData;
    reader = dbCommand.ExecuteReader();
    Debug.Log(reader.GetString(0));
    dbCommand.Dispose();
    dbCommand = null;
    reader.Close();
    reader = null;
    dbConnector.Close();
    dbConnector = null;
  }

我想将userName传递到我的股票脚本中,以显示登录用户的数据。

0 个答案:

没有答案