u3d连接网络sqlserver,并实现注册登录 教程+源码
登录注册功能已经做好了,还好比较顺利值得欣慰注册界面
注册部分代码:using UnityEngine;
using System.Collections;
using Bitverse.Unity.Gui;
using System;
using System.Data.SqlClient;
public class regsct : MonoBehaviour {
public GUISkin txtskin;
BitEditorStage form;
BitWindow regwindow;
BitButton btregist;
BitButton btexit;
BitTextField txtname;
BitTextField txtpwd;
BitTextField txtpwd1;
BitTextField txthobby;
BitToggle tgmale;
BitToggle tgFemale;
string gender;
string tempip;
// Use this for initialization
void Start () {
form = gameObject.GetComponent<BitEditorStage>();
regwindow = form.FindControl<BitWindow>("reg");
txtname = regwindow.FindControl<BitTextField>("txtname");
txtpwd = regwindow.FindControl<BitTextField>("txtpwd");
txtpwd1 = regwindow.FindControl<BitTextField>("txtpwd1");
txtpwd1.TextChanged += txtpwd1_TextChanged;
txthobby = regwindow.FindControl<BitTextField>("txthobby");
tgmale = regwindow.FindControl<BitToggle>("tgmale");
tgmale.ValueChanged += tgmale_ValueChanged;
tgFemale = regwindow.FindControl<BitToggle>("tgfemale");
tgFemale.ValueChanged += tgFemale_ValueChanged;
btregist = regwindow.FindControl<BitButton>("btreg");
btregist.MouseClick += bt_reg;
btexit = regwindow.FindControl<BitButton>("btexit");
btexit.MouseClick += bt_exit;
}
void OnGUI()
{
GUILayout.Label(tempip);
}
private void tgmale_ValueChanged(object sender, ValueChangedEventArgs e)
{
if (tgmale.Value == true)
{
tgFemale.Value = false;
gender = "m";
}
if (tgmale.Value == false && tgFemale.Value == false)
{
tgFemale.Value = true;
}
print(gender);
}
private void tgFemale_ValueChanged(object sender, ValueChangedEventArgs e)
{
if (tgFemale.Value == true)
{
tgmale.Value = false;
gender = "f";
}
if (tgmale.Value == false && tgFemale.Value == false)
{
tgmale.Value = true;
}
print(gender);
}
private void txtpwd1_TextChanged(object sender, ValueChangedEventArgs e)
{
if (txtpwd1.Text.Trim() != txtpwd.Text.Trim())
{
txtpwd1.Skin = txtskin;
}
else
{
txtpwd1.Skin = null;
}
}
private void bt_reg(object sender, MouseEventArgs e)
{
if (txtname.Text != "" && txtpwd.Text != "" && txtpwd1.Text != "" && txthobby.Text != "" && txtpwd.Text == txtpwd1.Text)
{
print(txtname.Text);
SqlConnection sqlcon = new SqlConnection("Data Source=Sql1001.u3dchina.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest;");
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlcon;
cmd.CommandType = System.Data.CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection.Open();
cmd.CommandText = "insert into user_ values ('" + txtname.Text + "','" + txtpwd.Text + "','" + gender.ToString() + "','" + txthobby.Text + "')";
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
txtname.Text = "";
txtpwd.Text = "";
txtpwd1.Text = "";
txthobby.Text = "";
regwindow.Visible = false;
}
da.SelectCommand.Connection.Close();
}
else
txtname.Focus = true;
}
private void bt_exit(object sender, MouseEventArgs e)
{
txtname.Text = "";
txtpwd.Text = "";
txtpwd1.Text = "";
txthobby.Text = "";
regwindow.Visible = false;
}
// Update is called once per frame
}登录部分:using UnityEngine;
using System.Collections;
using Bitverse.Unity.Gui;
using System;
using System.Data.SqlClient;
public class logsct : MonoBehaviour {
int temp;
public GUISkin txtskin;
BitEditorStage form;
BitWindow logwindow;
BitTextField txtname;
BitTextField txtpwd;
BitButton btlog;
BitButton btexit;
// Use this for initialization
void Start () {
form = gameObject.GetComponent<BitEditorStage>();
logwindow = form.FindControl<BitWindow>("log");
txtname = logwindow.FindControl<BitTextField>("txtname");
txtpwd = logwindow.FindControl<BitTextField>("txtpwd");
btlog = logwindow.FindControl<BitButton>("btlog");
btlog.MouseClick += bt_log;
btexit = logwindow.FindControl<BitButton>("btexit");
btexit.MouseClick += bt_exit;
}
private void bt_log(object sender, MouseEventArgs e)
{
if (txtname.Text != "" && txtpwd.Text != "")
{
SqlConnection sqlcon = new SqlConnection("Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest;");
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlcon;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "select * from user_ where user_name='"+txtname.Text+"' and user_pwd ='"+txtpwd.Text+"'";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection.Open();
temp = int.Parse(da.SelectCommand.ExecuteScalar().ToString());
da.SelectCommand.Connection.Close();
if (temp != 0)
print("login successed!");
txtname.Text = "";
txtpwd.Text = "";
}
}
private void bt_exit(object sender, MouseEventArgs e)
{
txtname.Text = "";
txtpwd.Text = "";
logwindow.Visible = false;
}
// Update is called once per frame
void Update () {
}
}
感谢楼主的无私分享!{:soso__11402694654016840197_7:} 啊,看不懂啊,,我刚下载的Unity,但是提示要注册哦~~~ 找一下源码在哪里 恳求楼主加注释好么?我连过Sqlite 没试过SqlServer 感谢楼主的无私分享! 不提供教程源码下载吗? {:soso_e176:} 楼主有没有尝试打包发布啊 我打包发布出问题 學習了{:5_401:} 学习了 谢楼主分享