找回密码
 立即注册
查看: 5612|回复: 80

[网络] U3D连接网络Sqlserver,并实现注册登录,教程,源码

[复制链接]
发表于 2012-6-15 12:23 | 显示全部楼层 |阅读模式
本帖最后由 多米诺 于 2012-11-27 18:37 编辑

注册界面

代码如下
注册部分:
  1. using UnityEngine;
  2. using System.Collections;
  3. using Bitverse.Unity.Gui;
  4. using System;
  5. using System.Data.SqlClient;

  6. public class regsct : MonoBehaviour {
  7.     public GUISkin txtskin;
  8.         BitEditorStage form;
  9.         BitWindow regwindow;
  10.          BitButton btregist;
  11.         BitButton btexit;
  12.         BitTextField txtname;
  13.         BitTextField txtpwd;
  14.         BitTextField txtpwd1;
  15.         BitTextField txthobby;
  16.     BitToggle tgmale;
  17.     BitToggle tgFemale;
  18.     string gender;
  19.     string tempip;
  20.         // Use this for initialization
  21.         void Start () {
  22.                 form = gameObject.GetComponent<BitEditorStage>();
  23.                 regwindow = form.FindControl<BitWindow>("reg");
  24.         txtname = regwindow.FindControl<BitTextField>("txtname");
  25.         txtpwd = regwindow.FindControl<BitTextField>("txtpwd");
  26.         txtpwd1 = regwindow.FindControl<BitTextField>("txtpwd1");
  27.         txtpwd1.TextChanged += txtpwd1_TextChanged;
  28.         txthobby = regwindow.FindControl<BitTextField>("txthobby");
  29.         tgmale = regwindow.FindControl<BitToggle>("tgmale");
  30.         tgmale.ValueChanged += tgmale_ValueChanged;
  31.         tgFemale = regwindow.FindControl<BitToggle>("tgfemale");
  32.         tgFemale.ValueChanged += tgFemale_ValueChanged;
  33.                  btregist = regwindow.FindControl<BitButton>("btreg");
  34.         btregist.MouseClick += bt_reg;
  35.                 btexit = regwindow.FindControl<BitButton>("btexit");
  36.         btexit.MouseClick += bt_exit;
  37.         }
  38.     void OnGUI()
  39.     {
  40.         GUILayout.Label(tempip);
  41.     }
  42.     private void tgmale_ValueChanged(object sender, ValueChangedEventArgs e)
  43.     {
  44.         if (tgmale.Value == true)
  45.         {
  46.             tgFemale.Value = false;
  47.             gender = "m";
  48.         }
  49.         if (tgmale.Value == false && tgFemale.Value == false)
  50.         {
  51.             tgFemale.Value = true;
  52.         }
  53.         print(gender);
  54.     }
  55.     private void tgFemale_ValueChanged(object sender, ValueChangedEventArgs e)
  56.     {
  57.         if (tgFemale.Value == true)
  58.         {
  59.             tgmale.Value = false;
  60.             gender = "f";
  61.         }
  62.         if (tgmale.Value == false && tgFemale.Value == false)
  63.         {
  64.             tgmale.Value = true;
  65.         }
  66.         print(gender);
  67.     }
  68.     private void txtpwd1_TextChanged(object sender, ValueChangedEventArgs e)
  69.     {
  70.         if (txtpwd1.Text.Trim() != txtpwd.Text.Trim())
  71.         {
  72.             txtpwd1.Skin = txtskin;
  73.         }
  74.         else
  75.         {
  76.             txtpwd1.Skin = null;
  77.         }
  78.     }
  79.         private void bt_reg(object sender, MouseEventArgs e)
  80.     {
  81.         if (txtname.Text != "" && txtpwd.Text != "" && txtpwd1.Text != "" && txthobby.Text != "" && txtpwd.Text == txtpwd1.Text)
  82.         {
  83.             print(txtname.Text);
  84.             SqlConnection sqlcon = new SqlConnection("Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest;");
  85.             SqlCommand cmd = new SqlCommand();
  86.             cmd.Connection = sqlcon;
  87.             cmd.CommandType = System.Data.CommandType.Text;
  88.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  89.             da.SelectCommand.Connection.Open();
  90.             cmd.CommandText = "insert into user_ values ('" + txtname.Text + "','" + txtpwd.Text + "','" + gender.ToString() + "','" + txthobby.Text + "')";
  91.             int i = cmd.ExecuteNonQuery();
  92.             if (i > 0)
  93.             {
  94.                 txtname.Text = "";
  95.                 txtpwd.Text = "";
  96.                 txtpwd1.Text = "";
  97.                 txthobby.Text = "";
  98.                 regwindow.Visible = false;
  99.             }
  100.             da.SelectCommand.Connection.Close();
  101.         }
  102.         else
  103.             txtname.Focus = true;
  104.     }
  105.         private void bt_exit(object sender, MouseEventArgs e)
  106.     {
  107.         txtname.Text = "";
  108.         txtpwd.Text = "";
  109.         txtpwd1.Text = "";
  110.         txthobby.Text = "";
  111.         regwindow.Visible = false;
  112.     }
  113.         // Update is called once per frame
  114.         
  115. }
复制代码
登录部分:
  1. using UnityEngine;
  2. using System.Collections;
  3. using Bitverse.Unity.Gui;
  4. using System;
  5. using System.Data.SqlClient;

  6. public class logsct : MonoBehaviour {
  7.     int temp;
  8.     public GUISkin txtskin;
  9.     BitEditorStage form;
  10.     BitWindow logwindow;
  11.     BitTextField txtname;
  12.     BitTextField txtpwd;
  13.     BitButton btlog;
  14.     BitButton btexit;
  15.         // Use this for initialization
  16.         void Start () {

  17.         form = gameObject.GetComponent<BitEditorStage>();
  18.         logwindow = form.FindControl<BitWindow>("log");
  19.         txtname = logwindow.FindControl<BitTextField>("txtname");
  20.         txtpwd = logwindow.FindControl<BitTextField>("txtpwd");
  21.         btlog = logwindow.FindControl<BitButton>("btlog");
  22.         btlog.MouseClick += bt_log;
  23.         btexit = logwindow.FindControl<BitButton>("btexit");
  24.         btexit.MouseClick += bt_exit;
  25.         }
  26.     private void bt_log(object sender, MouseEventArgs e)
  27.     {
  28.         if (txtname.Text != "" && txtpwd.Text != "")
  29.         {
  30.             SqlConnection sqlcon = new SqlConnection("Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98CEEF_test;User Id=DB_98CEEF_test_admin;Password=testtest;");
  31.             SqlCommand cmd = new SqlCommand();
  32.             cmd.Connection = sqlcon;
  33.             cmd.CommandType = System.Data.CommandType.Text;
  34.             cmd.CommandText = "select * from user_ where user_name='"+txtname.Text+"' and user_pwd ='"+txtpwd.Text+"'";
  35.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  36.             da.SelectCommand.Connection.Open();
  37.             temp = int.Parse(da.SelectCommand.ExecuteScalar().ToString());
  38.             da.SelectCommand.Connection.Close();
  39.             if (temp != 0)
  40.                 print("login successed!");
  41.             txtname.Text = "";
  42.             txtpwd.Text = "";
  43.         }
  44.     }
  45.     private void bt_exit(object sender, MouseEventArgs e)
  46.     {
  47.         txtname.Text = "";
  48.         txtpwd.Text = "";
  49.         logwindow.Visible = false;
  50.     }
  51.         // Update is called once per frame
  52.         void Update () {
  53.         
  54.         }
  55. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2012-11-19 13:45 | 显示全部楼层

回帖奖励 +1 U币

学习,网络不太懂啊!
发表于 2012-11-26 15:27 | 显示全部楼层
实现起来应该是非常有效的,谢谢
发表于 2012-11-26 15:52 | 显示全部楼层
看起来不错
发表于 2012-11-26 15:52 | 显示全部楼层
居然没有中奖
发表于 2012-11-29 21:13 | 显示全部楼层
请问发布之后能不能直接连上数据库呢?
 楼主| 发表于 2012-11-30 15:56 | 显示全部楼层
Joy 发表于 2012-11-29 21:13
请问发布之后能不能直接连上数据库呢?

发布有局限性。。
 楼主| 发表于 2012-11-30 15:58 | 显示全部楼层
Joy 发表于 2012-11-29 21:13
请问发布之后能不能直接连上数据库呢?

http://www.u3dchina.com/forum.ph ... 6orderby%3Ddateline  这个教程是可以的
发表于 2012-12-4 17:54 | 显示全部楼层
very good sample
发表于 2012-12-15 15:06 | 显示全部楼层
有图有真相
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-1 05:09 , Processed in 0.110974 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表