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.
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.
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
<?php
$dt = new DateTime();
print $dt->format('Y,m,d,H,i,s');
?>
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.
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.