I have read some answers here in Unity forums, but i still don't know where to begin adding a database. I have an experience in MySql. Can i use it in my Log in form in a standalone game?
most of the answers I've read are like for Online or Web games, like using WWW.
can i also use it for an offline standAlone game?
I hope someone can enlighten me about these things.
Thanks a lot!
Ohh Jeez! No, you do not have to go that crazy; if you just want to store simple things like the player's name, his score, and what level he was on, use PlayerPrefs; you don't need to use MySQL at all, that would be overkill.
See the PlayerPrefs docs for details: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
It is a very simple solution for saving simple data to the local computer in Unity, like ints, strings, and floats.
So in your game, when you want to save, you would do something like:
PlayerPrefs.SetString("PlayerName", "Derek");
PlayerPrefs.SetInt("PlayerScore", 1337);
PlayerPrefs.SetInt("CurrentLevel", 2);
Then when you start your game, you can retrieve all that stuff, and do whatever you want with it:
var playerName = PlayerPrefs.GetString("Player Score")
var score = PlayerPrefs.GetInt("PlayerScore");
var currentLevel= PlayerPrefs.GetInt("CurrentLevel");
Application.LoadLevel(currentLevel);
Derek hit the spot.
I just want to contribute with some nice thoughts about savegame data. It can be beneficial to have a struct or class describing the data in a type safe manner, so it is easy to find the different settings with intellisense. Then you would have a load and save function to save or load to player prefs. You could even use reflection to automate the save/load process.
With abstracting the storage layer away, you can later change from PlayerPrefs to ini files or xml files or sql tables. Your code would work none the less.
Example:
public class Savegame
{
public string playerName;
public int playerScore;
public int currentLevel;
public static Savegame Load()
{
Savegame loaded = new Savegame();
loaded.Reload();
return loaded;
}
public void Reload()
{
playerName = PlayerPrefs.GetString("playerName");
playerScore = PlayerPrefs.GetInt("playerScore");
currentLevel = PlayerPrefs.GetInt("currentLevel");
}
public void Save()
{
PlayerPrefs.SetString("playerName", playerName);
PlayerPrefs.SetInt("playerScore", playerScore);
PlayerPrefs.SetInt("currentLevel", currentLevel);
}
}
Then you can use it such as
void Start()
{
Savegame game = Savegame.Load();
Application.LoadLevel(game.currentLevel);
}
or
void Start()
{
game.currentLevel = Application.currentLevel;
game.Save();
}
I add yoyos comment here for formatting purposes.
public class Savegame
{
public string playerName
{
get { return PlayerPrefs.GetString("playerName"); }
set { PlayerPrefs.SetString("playerName", value); }
}
// and so on...
}