Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆




工具:unity3.5+WampSever
实现的功能:图片验证码,注册登录,邮箱验证激活
测试请使用英文!
首先创建数据库:
unity 3d教程: unity3d利用PHP+Mysql实现注册激活验证登陆
[C#]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CREATE TABLE IF NOT EXISTS `scores` (
  
`id` int (10) NOT NULL AUTO_INCREMENT,
  
`name` varchar(30) CHARACTER SET utf8 NOT NULL,
  
`password` varchar(50) CHARACTER SET utf8 NOT NULL,
  
`email` varchar(500) CHARACTER SET utf8 NOT NULL,
  
`flag` int (1) NOT NULL,
  
`code` varchar(500) CHARACTER SET utf8 NOT NULL,
  
`session` int (5) NOT NULL,
  
PRIMARY KEY (`id`)
  
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
  
INSERT INTO `scores` (`id`, `name`, `password`, `email`, `flag`, `code`, `session`) VALUES
  
(1, 'admin' , '21232f297a57a5a743894a0e4a801fc3' , '541416005@qq.com' , 1, '21232f297a57a5a743894a0e4a801fc3' , 29136);
插入一条记录,admin,密码admin。然后打开unity3D,新建场景,新建一个GUITexture,坐标调整为(0.04,0.81,0),然后将js代码赋给camera。代码如下:
[C#]  纯文本查看  复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#pragma strict
  
private var secretKey= "hashcode" ;
  
private var formNick = "" ; //this is the field where the player will put the name to login
  
private var formPassword = "" ; //this is his password
  
private var formEmail = "541416005@qq.com" ;
  
private var formyzm = "" ;
  
var formText = "" ; //log
  
var CheckUserURL = "http://localhost/checkuser.php" ; //检测帐户
  
var RegisterURL = "http://localhost/register.php" ;//注册
  
var imageURL = "http://localhost/image.php" ;//生成验证码的php文件
  
var hash = "hashcode" ; //验证码,需要与php文件中一致
  
var reg:boolean = false ; //切换注册页面
  
public var tu:GUITexture; //GUITexture
  
private var textrect = Rect (10, 150, 500, 500); //gui rect
  
function Start()
  
{
  
LoadImage();
  
}
  
function OnGUI() { if (reg== false )
  
{
  
tu.enabled = true ;
  
GUI.Label( Rect (10, 10, 80, 20), "Your name:" ); //text with your nick
  
GUI.Label( Rect (10, 30, 80, 20), "Your pass:" );
  
GUI.Label( Rect (10, 50, 80, 20), "yanzhengma:" );
  
formNick = GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you will insert the new value to variable formNick
  
formPassword = GUI.TextField ( Rect (90, 30, 100, 20), formPassword ); //same as above, but for password
  
formyzm = GUI.TextField(Rect(90,50,100,20),formyzm); if ( GUI.Button ( Rect (10, 120, 100, 20) , "Try login" ) ){ //just a button
  
Login();
  
}
  
if ( GUI.Button ( Rect (220, 120, 100, 20) , "Chanage Pic" ) ){ //just a button
  
LoadImage();
  
}
  
if ( GUI.Button ( Rect (120, 120, 100, 20) , "Register" ) ){ //just a button
  
reg= true ;
  
}
  
}
  
if (reg== true )
  
{
  
tu.enabled = false ;
  
GUI.Label( Rect (10, 10, 80, 20), "Your name:" ); //text with your nick
  
GUI.Label( Rect (10, 30, 80, 20), "Your pass:" );
  
GUI.Label( Rect (10, 50, 80, 20), "Your Email:" );
  
formNick = GUI.TextField ( Rect (90, 10, 100, 20), formNick ); //here you will insert the new value to variable formNick
  
formPassword = GUI.TextField ( Rect (90, 30, 100, 20), formPassword ); //same as above, but for password
  
formEmail = GUI.TextField ( Rect (90, 50, 200, 20), formEmail );
  
if ( GUI.Button ( Rect (10, 120, 100, 20) , "Login" ) ){ //just a buttonreg=false;
  
}
  
if ( GUI.Button ( Rect (120, 120, 100, 20) , "Register" ) ){ //just a button
  
Register();
  
}
  
}GUI.TextArea( textrect, formText );
  
}
  
function LoadImage()
  
{
  
var form = new WWWForm();
  
form.AddField( "act" , "62" );
  
var w = WWW(imageURL, form);
  
yield w; //we wait for the form to check the PHP file, so our game dont just hang
  
if (w.error != null ) {
  
print(w.error); //if there is an error, tell us
  
}
  
else {
  
print( "Test ok" );
  
tu.texture = w.texture;
  
w.Dispose(); //clear our form in game
  
}
  
}
  
function Login() {
  
var form = new WWWForm(); //here you create a new form connection
  
form.AddField( "hash" , hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
  
form.AddField( "name" , formNick );
  
form.AddField( "password" , formPassword );
  
form.AddField( "yzm" ,formyzm);
  
var w = WWW(CheckUserURL, form); //here we create a var called 'w' and we sync with our URL and the form
  
yield w; //we wait for the form to check the PHP file, so our game dont just hang
  
if (w.error != null ) {
  
print(w.error); //if there is an error, tell us
  
}
  
else {
  
print( "Test ok" );
  
formText = w.text; //here we return the data our PHP told us
  
w.Dispose(); //clear our form in game
  
if (formText== "密码正确,欢迎登录" )
  
{
  
Application.LoadLevel(1);
  
}
  
}
  
formNick = "" ; //just clean our variables
  
formPassword = "" ;
  
formyzm = "" ;
  
}
  
function Register()
  
{
  
var form = new WWWForm(); //here you create a new form connection
  
form.AddField( "hash" , hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
  
form.AddField( "name" , formNick );
  
form.AddField( "password" , formPassword );
  
form.AddField( "email" , formEmail);
  
var w = WWW(RegisterURL, form); //here we create a var called 'w' and we sync with our URL and the form
  
yield w; //we wait for the form to check the PHP file, so our game dont just hang
  
if (w.error != null ) {
  
print(w.error); //if there is an error, tell us
  
}
  
else {
  
print( "Register ok" );
  
w.Dispose(); //clear our form in game
  
}
  
formNick = "" ; //just clean our variables
  
formPassword = "" ;
  
formyzm = "" ;
  
}
在服务器端目录下,放置以下php文件:
Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆

然后在Unity中设置php文件的连接地址,并将GUITexture拖给赋值的GUITexture。
Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆

然后运行即可。
效果图:
Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆

Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆

点击下载:PHP文件







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值