将Unity3d连接到数据库? (C#)

时间:2018-01-11 05:28:16

标签: c# mysql unity3d

我正在寻找如何连接到mysql数据库的最简单的解决方案,例如,检索和更新数据。我试过做我的研究,但我找不到任何真正的东西。我的意思是,我敢打赌这是一个简单的方法。我之前使用过PHP,我认为使用C#连接到数据库相对容易。

有人可以给我看一下如何连接mysql数据库的示例代码吗?另外,如何检索和更新数据?拜托,谢谢!

1 个答案:

答案 0 :(得分:0)

我不清楚你的意思,但我已经使用了www表格。

从youtube开始关注this教程系列。

总之;您将创建www表单并将所需数据发送到PHP代码。有了PHP代码的答案,你可以完成你的任务。我将尝试提供一个例子。

  

Unity C#登录脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoginScript : MonoBehaviour {

    public GameObject inputUsernameText;
    public GameObject inputPasswordText;

    public string inputUsername;
    public string inputPassword;

    public string userUsername;
    public string userPassword;
    public string[] userDataArray;

    string loginURL ="YOUR LOGIN PHP CODE URL HERE" ;


    public bool isLoginButtonPressed;

    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if (isLoginButtonPressed) 
        {
            StartCoroutine(loginToDB (inputUsername, inputPassword));
            isLoginButtonPressed = false;
        }

    }

     IEnumerator loginToDB(string username, string password)
    {

        WWWForm form = new WWWForm();
        form.AddField("usernamePost", username);
        form.AddField("passwordPost", password);

        WWW www = new WWW (loginURL, form);
        yield return www;
        //Debug.Log (www.text);


        userDataArray = userEkipIDString.Split ('|');

        userUsername = userDataArray [0];
        userPassword = userDataArray [1];
        isLoginButtonPressed = false;

        SceneManager.LoadScene("MainMenuScene", LoadSceneMode.Single);




    }

    public void loginButtonPressedFunc()
    {
        inputUsername = inputUsernameText.GetComponent<Text> ().text.ToString ();
        inputPassword = inputPasswordText.GetComponent<Text> ().text.ToString ();
        isLoginButtonPressed = true;
    }




}

这个脚本的作用是,它从Unity的输入字段中获取用户名和密码并将其发送到php代码。如果用户名和密码正确,则会加载另一个场景。

  

PHP代码

<?php

    $servername = "YOUR HOST NAME ex:localhost";
    $username = "YOUR DB USER NAME";
    $password = "YOUR DB PASSWORD";
    $dbName = "YOUR DB NAME";

    $user_username = $_POST["usernamePost"];
    $user_password = $_POST["passwordPost"];

    //Make Connection
    $conn = mysqli_connect($servername,$username,$password,$dbName);

    //Check Connection
    if(!$conn)
    {
        die("Connection Failed. " . mysqli_connect_error());
    }

    //else echo ("Connection Success.");

    $sql ="Select user, pass  from "YOUR TABLE NAME" where user = '".$user_username."' ";  //You can write your own query here
    $result = mysqli_query($conn,$sql);

    //Get the result and confirm login

    if(mysqli_num_rows($result)>0)
    {
        //show data for each row
        while($row = mysqli_fetch_assoc($result))
        {
            if($row['pass'] == $user_password)
            {
                //echo "Login Success";
                echo "".$row['user']."|".$row['pass']."" ;
            }
            else
            {
                echo "Wrong Password";
            }
        }
    }
    else
    {
        echo "User not found";
    }

?>

您可以调整此逻辑以插入,更新和删除

希望这会有所帮助。干杯!