Best way to save data in Unity game

json unity3d persistence unity5 saving-data

26017 просмотра

4 ответа

79 Репутация автора

I was wondering... What's the best way to save data in Unity games. JSONs? If so, how? Thanks

Автор: messedupsongguy Источник Размещён: 23.12.2015 12:06

Ответы (4)

25 плюса

3888 Репутация автора

Short answer to your long question!

Here are some of the different Ways and Methods to Save data for Unity Projects:

  1. Platform-Independent: One way of saving data in Unity3D in a Platform-independent way is to use the PlayerPrefs class. (Learn More 1, 2).

  2. PERSISTENCE - SAVING AND LOADING DATA using DontDestroyOnLoad, PlayerPrefs, and data serialization Video Tutorial by unity.

  3. Server Side: You can also use a Server for saving data (like combination of PHP and MySQL Database). You can use it to save Score Data, user profile etc., Learn More From Unity Wiki

  4. For saving the in-game data to a Hard drive in a format that can be understood and loaded later on, use a .NET/Mono feature known as Serialization. Learn More

  5. Simple JSON guide about Unity is available at Unity Wiki or officially you can see JSON serialization

  6. SQLite (an embedded database for your app) is another great option for you get the Free Package, it is simple and easy (and my favorite) if you know SQL.

Автор: Muhammad Faizan Khan Размещён: 23.12.2015 07:06

1 плюс

431 Репутация автора

See my previous answer for a similar situation and a simple way to save data.

Автор: kagkar Размещён: 23.12.2015 03:46

4 плюса

445 Репутация автора

You can use many assets that are available for free and paid in asset store.

Save Game Free - XML and JSON saving and loading.


Saver.Save<T> (T data, string fileName);


Saver.Save<MyData> (myData, "myData"); // The .json extension will be added automatically

Save Game Pro - Binary saving and loading. fast and secure. Easy to use.


SaveGame.Save<T> (T data, string identifier);


SaveGame.Save<int> (score, "score");
Автор: Hasan Bayat Размещён: 25.05.2017 05:36

2 плюса

1460 Репутация автора

If you want to store your data in server there is a simple way with PHP and MySQL. What you have to do is:

Get what ever data you want from your server just in single string (code is below):


$server_name = "localhost";
$server_user = "Er.Ellison";
$server_pass = "supersecretPassword";
$server_db   = "game_test_db";

$connection = new mysqli($server_name , $server_user , $server_pass , $server_db);
if(!$connection) {
  die("Connection failed !" . mysqli_connect_error());

$query = "SELECT * FROM items";
$result = mysqli_query($connection , $query);

if(mysqli_num_rows($result) > 0){
  while($row = mysqli_fetch_array($result)){
    echo "id:" . $row['id'] . "|username:" . $row['username'] . "|type:" . $row['type'] . "|score:" . $row['score'] . ";";

And note that you MUST SEPARATE ANY string you want with a ; or any thing that you are comfortable with that and remember that we going to use it in C# in Unity.

Now you should get the data from your web like this (it will be a long string):

enter image description here

Now go to the Unity and create a C# script and attached that to what ever object in your scene and open the script up, then use this kind of code to manipulate the data that you retrieved from your database:

public class DataLoader : MonoBehaviour {

public string[] items;

// Use this for initialization
IEnumerator Start () {
    WWW itemsData = new WWW ("http://localhost/_game/test/itemsdata.php");  
    yield return itemsData;
    string itemsDataStrign = itemsData.text;
    print (itemsDataStrign);
    items = itemsDataStrign.Split (';');
    print (GetDataValue(items[0] , "cost:"));

string GetDataValue(string data, string index) {
    string value = data.Substring (data.IndexOf(index) + index.Length);
    if (value.Contains ("|")) {
        value = value.Remove (value.IndexOf("|"));
    return value; 


You just NOW retrieved data from database, check the image from unity console:

enter image description here

I made this for who that may be, like me, stuck in database problems!

Автор: a_m_dev Размещён: 09.08.2017 08:37
Вопросы из категории :