Thursday, 21 January 2016

Game Login script Unity c sharp php


Login Script :-
================

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoginScript : MonoBehaviour {
    public InputField usernameInputField;
    public InputField passwordInputField;
    public string username;
    public string password;
    public string answer;
    public string answer2;
    public string[] splitWords;
    string errorString;
    public Text errormsg;
    public Text DidnotenterText;
    string login_url = "http://www.yourdomain.com/dr/Login.php";
    public Image LoginSuccessfulPanel;
    public Image RegisterPanel;
    public Image LoginPanel;
    public Image RegistrationSuccessfullPanel;
    public Image ForgotPasswordPanel;
    public Text forgotusernametext;
    public Text sendingemailText;

    public Button loginButton;
    public Button forgotPasswordButton;
    public Button registerButton;

    public void Start () {
        LoginSuccessfulPanel.gameObject.SetActive (false);
        RegisterPanel.gameObject.SetActive (false);
        RegistrationSuccessfullPanel.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (false);

    }
    public void LoginButton()
    {
        if ((usernameInputField.text != "")  && (passwordInputField.text != "")) {
            StartCoroutine (RetrieveData ());
        } else {
            DidnotenterText.text = "* invalid  Username and Password";
        }
    }
    IEnumerator RetrieveData()
    {
        username = usernameInputField.text;
        password = passwordInputField.text;
        
        WWWForm form = new WWWForm ();
        form.AddField ("username1", username);
        form.AddField ("password1", password);

        WWW download1 = new WWW (login_url, form);
        
        yield return download1;
        answer = download1.text.ToString ();
        Debug.Log ("Ans" + answer);


        if (answer != "MisMatch") {
            DidnotenterText.text = " Loading .....";
            DidnotenterText.text = answer.ToString();
            splitWords = answer.Split ('-');

            /*GameControl.control.Coins = int.Parse(splitWords [0]);
            GameControl.control.Health = int.Parse(splitWords [1]);
            GameControl.control.Spirit = int.Parse(splitWords [2]);*/

            //Application.LoadLevel("CharSelection");
        }

        if (answer == "MisMatch") 
        {
            DidnotenterText.text = "* invalid  Username and Password";
            LoginSuccessfulPanel.gameObject.SetActive(false);
        }

        if (!string.IsNullOrEmpty (download1.error)) {
            print ("Error downloading: " + download1.error);
            errorString = download1.error;
            errormsg.text = errorString;

        } else {

        }
    }
    public void OkButton()
    {
        LoginSuccessfulPanel.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (false);
        usernameInputField.text = "";
        passwordInputField.text = "";

    }
    public void RegisterButton()
    {
        DidnotenterText.text = "";
        usernameInputField.text = "";
        passwordInputField.text = "";
        LoginSuccessfulPanel.gameObject.SetActive (false);
        RegisterPanel.gameObject.SetActive (true);

    }

    public void ForgotPassword()
    {
        loginButton.interactable = false;
        forgotPasswordButton.interactable = false;
        registerButton.interactable = false;
        usernameInputField.gameObject.SetActive(false);
        passwordInputField.gameObject.SetActive (false);
        ForgotPasswordPanel.gameObject.SetActive (true);
    }
        
    
}



Server Side Script :-
======================

<?php 
$servername = "Your_ServerName"; 
$username = "Your_UserName";
$password = "************";
$dbname = "Your_DB_Name";


$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


$Username2=$_POST['username1'];
$Emailid2 = $_POST['emailid1'];
$Password2 = $_POST['password1'];


if ($result->num_rows > 0) 
{ 
    $exits = 1; 
} 
else 
{ 
    $sql1 = "SELECT * FROM userinfo where username = '$Username2' AND password = '$Password2'";
    $result = $conn->query($sql1);
}


if ( $row = $result->fetch_assoc())
{ 
    echo $row["Coins"]."-".$row["Health"]."-".$row["Spirit"];
} 
else
{
    echo "MisMatch";
}
$conn->close(); 

?>






Game Register script Unity c sharp php


Register Script :-
==================
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Register : MonoBehaviour {
    public InputField usernameInputField;
    public InputField EmailidInputField;
    public InputField PasswordInputField;
    public string username;
    public string emailid;
    public string password;
    public string ErrorString;
    public Text ErrorMsg;
    string register_url = "http://www.yourdomain.com/dr/Register1.php";
    public Image InternetCheckPanel;
    public Image RegisterSuccessfulPanel;
    public Text DidnotenterText;
    public Image LoginPanel;
    public Image RegisterPanel;
    public Button Join;
    public Text EmailErrorText;
    public Text UsernameErrorText;
    public Text RegisterCheckText;
    public string answer;


    void Start () {
        InternetCheckPanel.gameObject.SetActive (false);
        RegisterSuccessfulPanel.gameObject.SetActive (false);
    }

    public void JoinButton()
    {
        string UserEmail = EmailidInputField.text;
        string UserEntername = usernameInputField.text;
        if ((usernameInputField.text != "") && (EmailidInputField.text != "") && (PasswordInputField.text != "")) 
        {
            if((!IsValidUsername(UserEntername)) && (IsvalidEmailId(UserEmail)))
            {
                UsernameErrorText.text = "Username should be minimum 8 to 12 characters";
                EmailErrorText.text = "";
            }
            if((IsValidUsername(UserEntername)) && (!IsvalidEmailId(UserEmail)))
            {
                EmailErrorText.text = "Invalid Email Format";
                UsernameErrorText.text = "";
            }

            if((IsValidUsername(UserEntername)) && (IsvalidEmailId(UserEmail)))
            {
                StartCoroutine (StoreData ());
                EmailErrorText.text = "";
                UsernameErrorText.text = "";
                RegisterSuccessfulPanel.gameObject.SetActive(true);
                RegisterCheckText.text = "Loading.....";
            }
            
            if((!IsValidUsername(UserEntername)) && (!IsvalidEmailId(UserEmail)))
            {
                EmailErrorText.text = "Invalid Email Format";
                UsernameErrorText.text = "Username should be minimum 8 to 12 characters...";
            }
        }

        else
        {
            DidnotenterText.text = "Please fill above all fields";
        }
    }
    IEnumerator StoreData()
    {
        username = usernameInputField.text;
        emailid = EmailidInputField.text;
        password = PasswordInputField.text;
        WWWForm form = new WWWForm ();
        form.AddField ("username1", username);
        form.AddField ("emailid1", emailid);
        form.AddField ("password1", password);
        
        WWW download = new WWW( register_url, form );
        yield return download;
        answer = download.text.ToString ();

        if (answer == "Successfully Registered") {
            RegisterCheckText.text = "Successfully Registered";
        } 
        else 
        {
            RegisterCheckText.text = "Username or Email Already Existed";
        }
        if (!string.IsNullOrEmpty (download.error)) {
            print ("Error downloading: " + download.error);
            ErrorString = download.error;
            ErrorMsg.text = ErrorString;
        } 
        else {

        }
    }

    public void OkButton()
    {
        if (RegisterCheckText.text == "Successfully Registered") {
            Time.timeScale = 1;
            InternetCheckPanel.gameObject.SetActive (false);
            RegisterSuccessfulPanel.gameObject.SetActive (false);
            RegisterPanel.gameObject.SetActive (false);
            LoginPanel.gameObject.SetActive (true);
            usernameInputField.text = "";
            PasswordInputField.text = "";
            EmailidInputField.text = "";
            Join.interactable = false;
            usernameInputField.interactable = true;
            EmailidInputField.interactable = true;
            PasswordInputField.interactable = true;
            Join.interactable = true;
        } else 
            if(RegisterCheckText.text == "Username or Email Already Existed")
        {
            RegisterSuccessfulPanel.gameObject.SetActive (false);
        }
    }

    public static bool IsvalidEmailId(string InputEmail)
    {
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,5})+)$");
        Match match = regex.Match (InputEmail);
        if (match.Success)
            return true;
        else
            return false;
    }
    public static bool IsValidUsername(string InputUsername)
    {
        Regex regex1 = new Regex("^[a-zA-Z]{8,12}$");
        Match match1 = regex1.Match (InputUsername);
        if (match1.Success)
            return true;
        else
            return false;
    }
    public void BackButton()
    {
        RegisterPanel.gameObject.SetActive (false);
        LoginPanel.gameObject.SetActive (true);
    }

}



Server Side Script :-
======================
<?php 
$servername = "Your_ServerName"; 
$username = "Your_UserName";
$password = "************";
$dbname = "Your_DB_Name";


$conn = new mysqli($servername, $username, $password, $dbname); 


if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
$exits = 0; 

$Username2=$_POST['username1'];
$Emailid2 = $_POST['emailid1'];
$Password2 = $_POST['password1'];

$query = "SELECT * FROM userinfo where username = '$Username2' OR emailid = '$Emailid2'";
$result = mysqli_query($conn,$query);
$num = mysqli_num_rows($result);

if($num == 0)
{
    $sql = "INSERT INTO userinfo (username,emailid,password) VALUES ('$Username2','$Emailid2','$Password2')";
    $result1 = mysqli_query($conn,$sql);
    echo "Successfully Registered";
}
else
{
    echo "Username already existed" ;
}
    
mysqli_close($conn);

?>






Wednesday, 20 January 2016

Object Oriented Programing Concepts Unity C#



  • Object is a self contained component. it contains properties and methods need to make a certain type of data useful
  • Class is a blue print/ prototype/ set of instruction to build a specific type of data useful.
  • Inheritance enables new objects to take on the properties of existing object.
  • Encapsulation is about Hiding complexity. or
  • Encapsulation is a process of hiding irrelevant data from the user.
  • Polymorphism - The ability to define more then one function with single name.
  • Overriding is a technique of redefining an inherited method.  or
  • Overriding is a practice of changing of parent class method in a child class.
  • Overloading is a process which we can give a single method with multiple definition.
  • Interface - An interface is a collection of method declaration that allows unrelated objects to communicate with each other.
    1. Interface can contains methods, properties, events, indexers or any combination of those 4 member types
    2. Interface can not contains constants, fields, Operators, instance constructors 

Friend List DB Structure for Game

Primary Key  :-  Primary key uniquely identify a record in the table.
Foreign Key  :-  Foreign key is a field in the table that is primary key in another table.


Table Name: User
Columns:
    UserID PK
    EmailAddress
    Password
    Gender

TableName: Friends
Columns:
    UserID PK FK
    FriendID PK FK
    (This table features a composite primary key made up of the two foreign 
     keys, both pointing back to the user table. One ID will point to the
     logged in user, the other ID will point to the individual friend
     of that user)
Example Usage:
Table User
—————
UserID EmailAddress           Password    Gender 
—————————————————————
1      satya@thered.black      satya@123     M      
2      siva@thered.black        siva@123       M      
3      naveen@thered.black   naveen@123  M      

Table Friends
——————
UserID FriendID
———————
1      2
1      3
2      3


This will show that satya is friends with both siva and naveen and that siva is also friends with naveen. 



Now when showing the friends-list to a particular user, a simple sql would do that:

Select FriendID 
From Friends 
Where UserID = " where is the id of the logged-in user “

If you want to find siva Friends ?

SELECT  FriendID
FROM    Friends
WHERE   UserID = ‘2’
UNION ALL
SELECT  UserID
FROM    Friends

WHERE   FriendID = ‘2’

Monday, 28 December 2015

iTween for unity

We can able to do transition effects by using iTween

we can move panels, buttons, 3d cubes etc....

File Name : - iTween.cs
https://www.assetstore.unity3d.com/en/#!/content/84


Just place a above file at unity assets folder do your transition effects code.

Sample Code :- 

using UnityEngine;
using System.Collections;

public class TestT : MonoBehaviour {
    public GameObject cc;
    public void buttonlll()
    {
        iTween.MoveTo (cciTween.Hash ("x",-600));    
    }

}