Unity+PHP+MySQ实现用户登录注册

PHP脚本如下:

0)
    {
        echo "success";
    }
    else
    {
         echo "error";
    }
}
else if($action == "regist")
{
    //设定字符集
    $sql = "set names utf8";
    //运行
    mysql_query($sql);
    
    //查看用户是否存在
    $sql = "select *from t_user where uname='".$uname."'";
    //运行
    $rs=mysql_query($sql);
    if(!$rs)
    {
        mysql_close($conn);
        die("error");
    }
    //获得数据库行数
    $recordCount = mysql_num_rows($rs);
    if($recordCount>0)
    {
        mysql_close($conn);
        die("exist");
    }else
    {
        //如果不存在就写入数据库
        $sql = "insert into t_user(uname,upass) values('".$uname."','".$upass."')";
        //运行
        $rs=mysql_query($sql);
        if(!$rs)
        {
            mysql_close($conn);
            die("error");
        }
        else
        {
            echo "success";
        }
        
    }
    //如果存在就提示

}
else
{
    echo "error!";
}

//关闭数据库
mysql_close($conn);
?>

Unity界面如下:

Unity+PHP+MySQ实现用户登录注册_第1张图片

C#脚本如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

/// 
/// 模拟用户登录
/// unity + PHP + MySQL
/// 
public class Demo : MonoBehaviour 
{
	public InputField Name;
	public InputField Pass;
	public Text ShowText;
	private string Url = "http://127.0.0.1/Unity/loginregist.php";
	bool action;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	/// 
	/// 登录
	/// 
	public void Btn_Login()
	{
		action = true;
		CreatWFrom (Name.text, Pass.text,action);
	}

	/// 
	/// 注册
	/// 
	public void Btn_Regist()
	{
		action = false;
		CreatWFrom (Name.text, Pass.text,action);
	}

	/// 
	/// 创建表单
	/// 
	/// Name.
	/// Pass.
	public void CreatWFrom(string name,string pass,bool action)
	{
		WWWForm form = new WWWForm ();
		form.AddField ("uname",name);
		form.AddField ("upass",pass);
		if (action == true) {
			form.AddField ("action", "login");
		} else {
			form.AddField ("action", "regist");
		}
		StartCoroutine (SendPost(Url,form));
	}

	/// 
	/// 提交表单
	/// 
	/// The post.
	/// URL.
	/// W form.
	IEnumerator SendPost(string url,WWWForm wForm){
		WWW www = new WWW (url,wForm);
		yield return www;
		if (www.error != null) {
			Debug.Log (www.error);
		} else {
			Debug.Log (www.text);

			if (action == false) {
				if (www.text == "success") {
					ShowText.text = "注册成功";
				} else if (www.text == "exist") {
					ShowText.text = "用户名存在";
				}
			} else {
				if (www.text == "success") {
					ShowText.text = "登录成功";
				} else if (www.text == "error") {
					ShowText.text = "登录失败";
				}
			}

			Invoke ("ShowTextNull",1);
		}
	}

	void ShowTextNull(){
		ShowText.text = "";
	}
}

完美!

你可能感兴趣的:(Unity,unity,php,脚本)