I assume it can be done with Unity, since MMOs are referenced in the description of this forum. But how?
I guess for starters, I’d need to set up some kinda database… I’ve tinkered with PHP and MySQL before, but does Unity have commands to issue these types of querries and accept a response?
And how would the bigger picture work once validation is possible? Do I read and write persistant player stats to the database arbitrarily during runtime? Or would it be better to just grab the entire table when the player joins and work with it client-side until the player logs off? (Or would doing this much stuff client-side be an open invitation to cheaters?)
Unity can interface with MySQL no problem. You could probably read through this for starters:
I’m going to need to do something like this soon myself, so it’s been floating around in the back of my mind. For login I’m planning to use a SQL database so it’s easier to create accounts from a web page. While i’ll probably use PlayerPrefs to save player stats, position etc since the game server and website server will be on separate machines.
Are you free to discuss the gritty implementation details?
I would like to think that once I get Unity talking to MySQL, I could save some of those PlayerPrefs to the server somehow. Not neccessarily everything, but important gameplay-related stuff like acquired stats and the player’s position in the world. That needs to be server-side.
I may need some kinda middleware app running on the server to keep track of potential database changes throughout the day, then at night it would roll the changes over into the database with one big table call instead of a bunch of little ones. I’ve heard about that trick being used before somewhere. I think maybe KOL does it? It’s supposed to be a lot faster or something.
Anywho, how is the basic MySQL call done in Unity?
Edit: Ah, WWWForm. Interesting. I’ll have to see if I can get this working tomorrow.
AddBinaryData looks especially intriguing. Does this mean I could dump some arbitrary block of data such as a mesh or a texture into a bytearray, send it to the server for long-term storage, then retrieve it later for use in the Unity app? That’s a freaking powerful technique if it works like that!
Made a couple scripts for unity that might help you. They’re not complete and haven’t been tested yet, but i’m pretty sure everything in them is right. (although there might be a slightly better method, i’m far from a pro at this stuff)
This is for verifying a user account exists on login:
var phpscript = "http://www.host.com/validatelogin.php";
private var nametext = "";
private var passtext = "";
function OnGUI () {
nametext = GUI.TextField (Rect (10, 10, 130, 20), nametext);
passtext = GUI.TextField (Rect (10, 40, 130, 20), passtext);
if (GUI.Button (Rect (150,10,90,20), "Login")) {
senddata ();
}
}
function senddata () {
var form = new WWWForm();
form.AddField("Name", nametext);
form.AddField("Pass", passtext);
var w = WWW(phpscript, form);
yield w;
if (w.data == "good") {
//log them on, when that's done load playerpref data instantiate char etc
}
}
This would be attached to the object controlled by the player. It would save the position of the player out to player prefs and the health to a SQL database (using a php script that you’d have to write) :
var phpscript = "http://www.host.com/outputdata.php";
function OnPlayerDisconnected() {
var info = transform.GetComponent("StatsScript");
PlayerPrefs.SetFloat(info.playername+"xpos",transform.position.x);
PlayerPrefs.SetFloat(info.playername+"ypos",transform.position.y);
PlayerPrefs.SetFloat(info.playername+"zpos",transform.position.z);
var form = new WWWForm();
form.AddField("Name", info.playername);
form.AddField("Pass", info.password);
form.AddField("Health", info.health);
var w = WWW(phpscript, form);
yield w;
Network.Destroy(gameObject);
}
edit: oh, and i think this might be interesting to you concerning the importing meshes thing you mentioned:
That info is really useful TJB, I am helping a friend with something similar.
There is one thing that I am not totally sure about. The URL you refer to as $phpscript - I understand it needs to have a validation script in it, but how would this return a value to work? Does it somehow need to return the value “good” in your code?
It’s fairly self explanatory and simple, but it should get you started. The code goes in a text file with the php ending and is put on your server. The webserver processes whatever is sent to it using this code and sends out “good” if the password and username is right.
Sorry I can’t show how to actually hook it up to the mysql database.
Oh! Also, if you want to hide the password that’s typed in unity (so it outputs stars instead of the password) this might be helpful:
var passtext = "";
function OnGUI () {
var letternum = 0;
var letternumaft = 0;
var dispass = "";
var temppass = "";
for (var element in passtext) {
dispass += "*";
letternum++;
}
dispass = GUI.TextField (Rect (25,135,250,30), dispass);
for (var element in dispass) {
letternumaft++;
var strn = element.ToString();
if (strn != "*") passtext += element;
}
if (letternum > letternumaft) {
letternum = 1;
for (var element in passtext) {
if (letternum <= letternumaft) {
var chara = element.ToString();
temppass+=chara;
letternum++;
}
}
passtext = temppass;
}
}
It’s not perfect… backspace will always remove the last character (if you move cursor halfway through the characters it’ll still remove the last)… but i don’t think that’s a big deal… most people don’t go back and change the 3rd of 10 characters in there password after they’ve typed the whole thing…
how could i just simply add a login screen and everything so i could just allow people to log in and play something, after registering. I also need to know how to make a registering thing. it would be AMAZING if you could just put that all together, even the scene switch thing, and simply give it to me via the forum in a package. but that would be asking for a lot. I’D BE SO HAPPY IF YOU COULD! and if you don’t want to, which I would think any person really would, you REALLY don’t have to. or you could just add a multiplayer function to the game i make. but again, you really don’t have to. thanks…
it does sound pretty challenging to do that. and it is also half the fun to figure it out yourself. the thing is i’ve tried that. i can’t understand it. check my signature!
What part are you having a problem with? Also I assume you want the login to be in the game not on the web page, correct? The first thing you need is a GUI that includes 2 labels, 2 text fields, and a Login button. The text fields and labels are for the user name and password. With these you can set a username and password variable. When you hit login it should send these to a web address using the WWWForm and WWW classes. See Unity - Scripting API: WWWForm
The hard part is setting up the http and sql servers. If you have your own machine to set up things on, I suggest the Apache web server and MySQL sql server.
I don’t think I’ve seen a tutorial specifically on login pages. I think you’ll just have to go through the documentation. I’ve attached a file that I’ve created that controls the majority of my GUI system. It has options for various backgrounds, options menus, a multiplayer host/join menu, etc. You should be able to use this as an example to create what you need. I’ve divided most of the code into independent functions. So the OptionsMenu is a function, MainMenu is a function, etc. Keep asking questions, I’ll be happy to give you a hand, but it helps if they’re more specific than “How do I do .”
thanks guys, big help. should i be using scripts like Server, Button, Sender, or Password Field? or maybe that’s just in the package. your always tons of help, and thanks again.
Jake, just attach the .js to any game object in the scene. I’ve got an empty object I call GameManager on which I call DontDestroyOnLoad and that I attach all management scripts to. This includes the GUI script I posted as well as one I’m working on called MultiplayerManager, SimManager, etc. Each of these scripts handles a different component of my application.
For the login screen, I’d put all the related code in some kind of network manager or multiplayer manager. If you want you could put the GUI code for the login in the same script or as I do, put all the GUI code in one file. That is really just your preference. A single function should do it. Something like this modified version of the highscores example in the WWWForm docs:
function DoLogin(name : String, password : String) : boolean {
// Create a form object for sending high score data to the server
var form = new WWWForm();
form.AddField( "game", "MyGameName" );
form.AddField( "playerName", name );
form.AddField( "password", password );
// Create a download object
var download = new WWW( login_url, form );
// Wait until the download is done
yield download;
if(download.error) {
print( "Error logging in: " + download.error );
return false;
}
else {
return true;
}
}
Then the GUI code would look something like:
var name = "";
var pass = "";
var netManager : <NetManagerScriptName>;
var doLogin = true;
var loggingin = false;
function OnGUI(){
if(doLogin){
GUILayout.BeginArea(Rect(<define your rect here>));
GUILayout.BeginHorizontal();
GUILayout.Label("User Name");
GUILayout.TextField(name);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Password");
GUILayout.TextField(pass);
GUILayout.EndHorizontal();
if(!loggingin GUILayout.Button("Submit")){
loggingin=true;
doLogin=!netManager.DoLogin(name,pass);
}
else if(GUILayout.Button("Cancel Login")){
loggingin=false;
doLogin=true;
}
GUILayout.EndArea();
}
}
Now that is untested, I just whipped that up while writing this message, but that is the general idea.