您的位置:首页 > 移动开发 > Unity3D

【unity3d脚本】Easy login / register script

2013-08-11 10:20 274 查看
#pragma strict

var s :String;

var username :String;

var pw :String;

var pw2 :String;

var info:String;

var success :boolean;

var skin:GUISkin;

var LogoStyle :GUIStyle;

var DescribtionStyle :GUIStyle;

var obj :GameObject[];

var login :boolean;

function getting(extension :String){

var url :String = "http://www.yourlink.org/" + extension;

var www : WWW = new WWW (url);

yield www;

s = www.text;

}

function write(uname :String, pw :String, pw2 :String){

if(pw == pw2){

if(uname.Contains("^") || uname.Contains("°") || uname.Contains("'") || uname.Contains("§") || uname.Contains("$") || uname.Contains("%") || uname.Contains("&") || uname.Contains("/") || uname.Contains("=") || uname.Contains("?") || uname.Contains("*") || uname.Contains("~") || uname.Contains("@")){

s = "Username isn't formal right";

}else{

if(uname.Length > 6){

if(pw.Length > 6){

var www : WWW = new WWW ("http://www.yourlinkorg/write_new.php?username="+username+"&pw="+pw);

yield www;

s = www.text;

}else s="Password is too short (min. 7 Chars)";

}else s="Username is too short (min. 7 Chars)";

}

}else{

s = "Passwords do not match";

}

}

function OnGUI () {

GUI.skin = skin;

username = GUI.TextField(Rect(obj[2].transform.position.x, obj[2].transform.position.y, 348, 48), username, 25);

pw = GUI.PasswordField(Rect(obj[4].transform.position.x, obj[4].transform.position.y, 348, 48), pw , "*"[0], 25);

pw2 = GUI.PasswordField(Rect(obj[6].transform.position.x, obj[6].transform.position.y, 348, 48), pw2 , "*"[0], 25);

GUI.Label(Rect(obj[0].transform.position.x, obj[0].transform.position.y,Screen.width , Screen.width), "Loginnation", LogoStyle);

GUI.Label(Rect(obj[1].transform.position.x, obj[1].transform.position.y, Screen.width, Screen.width), "Username:", DescribtionStyle);

GUI.Label(Rect(obj[3].transform.position.x, obj[3].transform.position.y, Screen.width, Screen.width), "Password:", DescribtionStyle);

GUI.Label(Rect(obj[7].transform.position.x, obj[7].transform.position.y, Screen.width, Screen.width), "Repeat password:", DescribtionStyle);

if(GUI.Button(Rect(obj[5].transform.position.x, obj[5].transform.position.y, 166, 39),"Enter")){

if(login){

getting("process.php?username="+username+"&passwort="+pw+"&info=success");

username = "";

pw = "";

pw2 = "";

}else{

write(username, pw, pw2);

}

}

if(GUI.Button(Rect(obj[9].transform.position.x, obj[9].transform.position.y, 166, 39),"New")){

login = false;

}

if(GUI.Button(Rect(obj[10].transform.position.x, obj[10].transform.position.y, 166, 39),"Login")){

login = true;

}

if(s != "true"){

GUI.Label(Rect(obj[8].transform.position.x, obj[8].transform.position.y, Screen.width, Screen.width), s, DescribtionStyle);

}else{

GUI.Label(Rect(obj[8].transform.position.x, obj[8].transform.position.y, Screen.width, Screen.width), "Successfully Logged in", DescribtionStyle);

}

}

function Update(){

if(s == "true"){

success = true;

}

if(success){

PlayerPrefs.SetString("username", username);

PlayerPrefs.SetString("passwort", pw);

Application.LoadLevel("MainPage");

}else{

PlayerPrefs.DeleteKey("username");

PlayerPrefs.DeleteKey("passwort");

}

if(login){

iTween.MoveTo(obj[5], iTween.Hash("x", Screen.width / 2 + 10, "y", Screen.height / 2 + 100)); //Button

iTween.MoveTo(obj[6], iTween.Hash("y", −600)); //pw2f

iTween.MoveTo(obj[7], iTween.Hash("x", Screen.width + 20));//pw2d

iTween.MoveTo(obj[8], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height / 2 + 173));

iTween.MoveTo(obj[9], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height / 2 + 100)); //registrier Button

iTween.MoveTo(obj[10], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height + 100)); //login Button

}else{

iTween.MoveTo(obj[5], iTween.Hash("x", Screen.width / 2 + 16, "y", Screen.height / 2 + 187)); //Button

iTween.MoveTo(obj[6], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height / 2 + 131));

iTween.MoveTo(obj[7], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height / 2 + 109));

iTween.MoveTo(obj[8], iTween.Hash("x", Screen.width / 2 − 245, "y", Screen.height / 2 + 240));

iTween.MoveTo(obj[9], iTween.Hash("x", −400, "y", Screen.height / 2 + 100)); //registrier Button

iTween.MoveTo(obj[10], iTween.Hash("x", Screen.width / 2 − 165, "y", Screen.height / 2 + 187)); //login Button

}

}

function Start () {

obj = new GameObject[11];

obj[0] = new GameObject(); // LOGO

obj[0].transform.position.x = Screen.width / 2 − 165;

obj[0].transform.position.y = Screen.height / 2 − 198;

obj[1] = new GameObject(); //Username describtion

obj[1].transform.position.x = Screen.width / 2 − 165;

obj[1].transform.position.y = Screen.height / 2 − 96;

obj[2] = new GameObject(); //Username text_field

obj[2].transform.position.x = Screen.width / 2 − 165;

obj[2].transform.position.y = Screen.height / 2 − 77;

obj[3] = new GameObject(); //password describtion

obj[3].transform.position.x = Screen.width / 2 − 165;

obj[3].transform.position.y = Screen.height / 2 + 5;

obj[4] = new GameObject(); //password field

obj[4].transform.position.x = Screen.width / 2 − 165;

obj[4].transform.position.y = Screen.height / 2 + 26;

obj[5] = new GameObject(); //login button

obj[5].transform.position.x = Screen.width / 2 − 165;

obj[5].transform.position.y = Screen.height / 2 + 100;

obj[6] = new GameObject(); //password 2 field

obj[6].transform.position.x = Screen.width / 2 − 165;

obj[6].transform.position.y = −600;

obj[7] = new GameObject(); //password 2 text

obj[7].transform.position.x = Screen.width + 20;

obj[7].transform.position.y = Screen.height / 2 + 109;

obj[8] = new GameObject(); //Info text

obj[8].transform.position.x = Screen.width / 2 − 165;

obj[8].transform.position.y = Screen.height / 2 + 173;

obj[9] = new GameObject(); //registrier button

obj[9].transform.position.x = Screen.width / 2 + 10;

obj[9].transform.position.y = Screen.height / 2 + 100;

obj[10] = new GameObject(); //Login Button

obj[10].transform.position.x = Screen.width / 2 + 10;

obj[10].transform.position.y = Screen.height / 2 + 100;

}

The php script called connect.php (in my case in the file "mysql_inc_32"):

PHP Code:

01.<?php

02.function connection(){

03.$verbindung = mysql_connect("localhost", "username" , "andpassword")

04.or die("Couldn't connect");

05.$con = mysql_select_db("your_database_name") or die ("Wrong databse");

06.

07.return $con;

08.

09.}

10.?>

So now the script called process.php:

PHP Code:

01.<?php

02.include("mysql_inc_32/connect.php");

03.

04.$con = connection();

05.

06.$s_1 = "SELECT * FROM userTB WHERE username = '" . $_GET['username'] . "' AND passwort = '" . md5($_GET['passwort']) . "'";

07.$sql = mysql_query($s_1);

08.if(mysql_num_rows($sql) > 0){

09.    while ($ds = mysql_fetch_object($sql)) {

10.            $img = $ds −> img;

11.            $birth = $ds −> birthday;

12.            $sex = $ds −> sex;

13.            $genre = $ds −> genre;

14.    }

15.    if($_GET['info'] == "img") {

16.

17.        echo($img);

18.

19.    }else if($_GET['info'] == "birth"){

20.

21.        echo($birth);

22.

23.    }else if($_GET['info'] == "sex"){

24.

25.        echo($sex);

26.

27.    }else if($_GET['info'] == "genre"){

28.

29.        echo($genre);

30.

31.    }else{

32.        echo("true");

33.    }

34.}else{

35.echo("Passwort or username is wrong");

36.}

37.?>
复制代码The script called write_new.php

PHP Code:

01.<?php

02.include("mysql_inc_32/connect.php");

03.
//unity3dhttp://www.unitymanual.com
04.$con = connection();

05.if(isset($_GET['username']) and isset($_GET['pw'])){

06.$s_1 = "SELECT * FROM userTB WHERE username = '" . $_GET['username'] . "'";

07.$sql = mysql_query($s_1);

08.if(mysql_num_rows($sql) > 0){

09.    echo("username schon vergeben");

10.}else{

11.$s_2 = "INSERT INTO userTB (username, passwort) VALUES ('" . $_GET['username'] . "', '" . md5($_GET['pw']) . "')";

12.$sql_2 = mysql_query($s_2);

13.if($sql_2 == true){

14.echo("Successfully registered");

15.}else{

16.echo("Error while registrating");

17.}

18.}

19.}else{ echo("Username and/or Password empty");}

20.?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: