Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to save constantly updated data?

Discussion in 'Scripting' started by Racm_Games, Jan 12, 2016.

  1. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Hi everyone,

    I have being reading about scriptable objects and storing data in json files and other methods to store data. Well I have never saved data in any of my projects so its a new thing for me, so I was wondering what will be the best approach to what I have in mind, I don't want to begin doing something and find out at last that it won't work, or that there was a simpler way of doing it, so here it is:

    I want to show ads in the game, I will be using UnityAds for this purpose and what I basically want is to limit the ad views per day and make some kind of waiting system so that the player doesn't spam on the Ad system of the game to get rewards.
    For the player to be able to get access to the rewards he will need to click on a button that will show him the ad and then the rewards he can choose from. I want something similar to what crossy road once did.
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    You can basically save any data to Application.persistentDataPath using the usual I/O classes provided. I myself use a combination of Easy Save (from the Asset Store) and JSON.NET for serializing the data. Easy Save because it may encrypt data out of the box, otherwise your customers may just edit the data. For serializing there are other classes as well, but I found JSON.NET so far to suit all my needs. There you just put in your C# models and serialize them to a JSON string. Works with many data types.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You'll want to save the current time when an ad is watched. However if you use the device time this is easily open to abuse as the user can just put the time forward on the device and your program will think enough time has elapsed to show another ad.

    Getting the world time is the best approach I found, but the online server pool at pool.ntp.org was terribly unreliable and often timed out when querying the time, which led to unacceptable delays showing the ads.

    In the end I plumped for hosting a small PHP script on my website server which prints the current time, and then I just download this page and decode the text to get the time. Works perfectly with the WWW class and is very quick.

    The PHP script I use is
    Code (CSharp):
    1.  
    2. <?php
    3.    $dt = new DateTime();
    4.    print $dt->format('Y,m,d,H,i,s');
    5. ?>
    6.  
    The only downside to this is that you need an active internet connection for this to work, but as this is also the case for showing the Unity ads I didn't view it as a problem.

    This is the WorldTime function that I use (It's in a static class), I implemented it with a callback so that I can be notified when the time has been read successfully.

    Code (CSharp):
    1.  
    2.     public static void GetWorldTime(MonoBehaviour mono, System.Action<DateTime> CallBack = null)
    3.     {
    4.         mono.StartCoroutine(_getTime(CallBack));
    5.     }
    6.  
    7.     static IEnumerator _getTime(System.Action<DateTime> CallBack)
    8.     {
    9.         var timeOut = Time.time + 10f;
    10.         WWW www = new WWW(SERVER_ADDRESS);
    11.  
    12.         while(!www.isDone && Time.time<timeOut)
    13.         {
    14.             yield return null;
    15.         }
    16.  
    17.         if(!www.isDone || !string.IsNullOrEmpty(www.error))
    18.         {
    19.             if(CallBack != null) CallBack.Invoke(GetDummyDate());
    20.             yield break;
    21.         }
    22.  
    23.         DateTime dt = new DateTime();
    24.         var parts = www.text.Split(new char[]{','});
    25.         if(parts.Length==6)
    26.         {
    27.             dt = new DateTime(
    28.                 int.Parse(parts[0]),
    29.                 int.Parse(parts[1]),
    30.                 int.Parse(parts[2]),
    31.                 int.Parse(parts[3]),
    32.                 int.Parse(parts[4]),
    33.                 int.Parse(parts[5])
    34.                 );
    35.         }
    36.  
    37.         if(CallBack != null) CallBack.Invoke(dt);
    38.     }
     
    Last edited: Jan 12, 2016
  4. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    I don't know if I quite get it but, lets see.. you mean I need to save the objects that are getting the data through any method I prefer to my Application.persistentDataPath, in your case you use Easy Save, this will encrypt the data for security purposes, and then when needed, use JSON.NET to serialise the information and get the values previously saved... I will need to read a lot!! :( Since I don't know anything about these methods or how they work... if you got any good tutorial I will very much appreciate it.
     
  5. Racm_Games

    Racm_Games

    Joined:
    Sep 23, 2015
    Posts:
    19
    Hey thanks a lot for sharing!! Very nice to have these scripts.