簡體   English   中英

我如何使用C#代碼將其應用於Unity中的UI?

[英]What can I do with my C# code for me to apply this to my UI in Unity?

我正在嘗試設置我的帳戶注冊系統,以將數據從統一輸入到我的數據庫,到目前為止,我已經能夠通過Inspector進行操作,現在我希望能夠使用我在其中創建的UI進行此操作團結,我該怎么辦? (附言:這是我第一次發帖,我也是初學者,所以請諒解,如果我可能不遵守某些規則,那不行)

這是我用於通過Unity中的檢查器將數據輸入到PhpMyAdmin數據庫中的代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataInserter : MonoBehaviour
{

    public GameObject inputUserName;
    public GameObject inputEmail;

    string CreateUserURL = "http://localhost/balikaral/insertAccount.php";

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
       if (Input.GetKeyDown(KeyCode.Space)) CreateUser(inputUserName, inputEmail);
    }

    public void CreateUser(string username, string email)
    {
        WWWForm form = new WWWForm();
        form.AddField("usernamePost", username);
        form.AddField("emailPost", email);

        WWW www = new WWW(CreateUserURL, form);
    }
}

您只需要從輸入字段InputField.text獲取輸入即可

我建議直接使用InputField字段,這樣就不需要GetComponent調用。

public class DataInserter : MonoBehaviour
{
    public InputField inputUserName;
    public InputField inputEmail;

    string CreateUserURL = "http://localhost/balikaral/insertAccount.php";

    public void CreateUser()
    {
        var userName = inputUserName.text;
        var email = inputEmail.text;

        WWWForm form = new WWWForm();
        form.AddField("usernamePost", username);
        form.AddField("emailPost", email);

        WWW www = new WWW(CreateUserURL, form);
    }
}

並在Button的onClick事件中引用CreateUser方法。


但是請注意, WWW已過時,您應該使用UnityWebRequest.Post

public void CreateUser()
{
    var userName = inputUserName.text;
    var email = inputEmail.text;
    StartCoroutine(CreateUserRequest(userName, email));
}

private IEnumerator CreateUserRequest(string userName, string email)
{
    WWWForm form = new WWWForm();
    form.AddField("usernamePost", username);
    form.AddField("emailPost", email);

    using (UnityWebRequest www = UnityWebRequest.Post(CreateUserURL, form))
    {
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}

您不應該通過PhpMyAdmin進行操作,它是用於管理mysql的mysql工具,我假設您想為游戲創建玩家帳戶。 有兩種方法可以執行您的操作1.創建一個接受Http post(命令)的webApi站點,該webapi將連接到您的mysql數據庫,並執行CRUD(Create,Read,Update,Delete)事情
2.創建直接與mysql數據庫的mysql連接,然后將數據插入mysql表。 (不安全)

用鄰居的方式,您需要了解SQL語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM