20

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

messedupsongguy
  • 319
  • 2
  • 4
  • 10
  • https://docs.unity3d.com/Manual/JSONSerialization.html – Vapid Dec 28 '17 at 09:34
  • https://docs.unity3d.com/ScriptReference/PlayerPrefs.html (see https://youtu.be/gM73B5PyGh8 for help), https://docs.mongodb.com/realm/sdk/dotnet/unity/ (see https://youtu.be/8jo_S02HLkI) and many others :) – Dominic Frei Sep 29 '21 at 11:44

3 Answers3

46

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. PlayerPrefs is a static class and it is very easy to use but not reliable.

  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 a combination of PHP and MySQL Database). You can use it to save Score Data, user profiles, inventory, etc., Learn More From Unity Wiki. You can also use third-party solutions like firebase etc.

  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 to get the Free Package, it is simple and easy (and my favorite) if you know SQL.

  7. Scriptable Object: it's a data container. Useful for unchanging data. Suitable for large unchanging data and amazingly reduce your project's memory.

enter image description here

The above is taken from my blog post on Data Saving Techniques for Unity3d Applications.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • let's say I have several events one after another which will be updated through web server from time to time, and to save all events data in devices, what should i use? Will it be ok in serializtion, saving all events' data and updating the data later? All i see is save and load & no edit or update in serializaiton tutuorials. does it clean old data to save new data? – Zoedia Mar 31 '16 at 10:51
  • @Zoedia i guess it is better that you ask separate question with some significant details. So maybe i can understand and answer – Muhammad Faizan Khan Dec 05 '16 at 05:01
  • Great answer just sad to see #1 item player prefences this leads to people saving data in that unsafe place. But none the less very clear answer. – Jeroen De Clercq Aug 10 '17 at 21:09
  • @JeroenDeClercq # 1 is the most easy to save data in unity application. I don't know if it is unsafe. – Muhammad Faizan Khan Aug 11 '17 at 05:04
  • If anyone wants to go the JSON route without a third-party library, Unity has some official JSON Serialization tools https://docs.unity3d.com/Manual/JSONSerialization.html – Vapid Dec 28 '17 at 09:32
  • I have updated the answer. added your link thanks – Muhammad Faizan Khan Dec 30 '17 at 07:46
  • `PlayerPrefs` are ment to store user **preferences** (therefore the name) like e.g. the sound volume and should **NOT** be used for storing important data like game progress as they might get deleted by updating the app! – derHugo Sep 28 '19 at 05:49
  • @derHugo thanks for the comment, please can you share any link about this thing that app data (using playerprefs) is when we update the app. I need so that i can update this fact in the answer. – Muhammad Faizan Khan Sep 28 '19 at 06:33
  • The more important thing about PlayerPrefs is that they are not encrypted. The are saved in plain text (for example: on Windows in the registry) and can therefore just be edited by the user. Keep that in mind when saving data there that's supposed to be safe. – Dominic Frei Sep 29 '21 at 11:47
  • But you can save the encrypted text yourself – Muhammad Faizan Khan Sep 30 '21 at 04:32
6

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

Save Game Free - XML and JSON saving and loading.

Syntax:

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

Example:

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.

Syntax:

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

Example:

SaveGame.Save<int> (score, "score");
Hasan Bayat
  • 926
  • 1
  • 13
  • 24
1

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

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

<?php

//SERVER CONNECTION 
$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
$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.

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

enter image description here

STEP 3:
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; 
}

}

STEP 4:
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!

amdev
  • 6,703
  • 6
  • 42
  • 64
  • I think `$reuslt` in your code (three instances) is a typo. – halfer Aug 09 '17 at 13:54
  • By the way, when you see messages addressed to you, please do reply to them. I've downvoted this question, and will undownvote once you have replied. Please be willing to make edits where necessary, to benefit future readers. – halfer Aug 10 '17 at 16:48
  • 3
    sorry sorry sorry sorry , i've just working on a really serious project and i just come over here when i can , by the way thanx for editing man , i will reform it ... – amdev Aug 10 '17 at 18:54