Redis in Unity
Hello everyone, I want to make a multiplayer application with online scores. They suggested me to work the data of the scores in memory to access quickly, I mean, they recommended me to use Redis with Unity. It is the first time that I work with information in real time. Does anyone know if there is another way of storing databases in memory in Unity or if it is necessary to use Redis? Thanks
Answer by GetLitGames · Oct 06, 2020 at 10:57 AM
PlayFab would be the best way, and they have leaderboards. You can avoid the whole database access complexity of mapping classes, and building relational data and all that.
Another consideration is that you need authentication anyways, how will you determine who the user is that is storing the score? You will need some kind of user authentication. PlayFab lets you store simple key/value pairs, so you could simply store "HighScore" with a number or you could store a key named "UserData" and a json string of a serialized object with complex data. The choice is yours. Without user authentication, how will you protect your Redis database? If you leave the Redis database open on a public IP, you have to deal with hackers. Will you embed a Redis username/password for logging into the Redis database connection in your Unity 3D game? If so, someone can easily get that username and password then change whatever data they want in the database. Without user authentication, this is a serious problem. But this may just be a college project for you, and security isn't meant to be addressed in this project.
As far as Redis, if you use it locally, it would only be saved locally which for a multiplayer game you would probably want it remote. You should be able to use most .NET data access libraries that you could get via NUGET. The only question is whether it will work on the platform you build for. In most cases, you should be able to find data access libraries for Redis. If you use Redis remotely, with a dedicated Redis database server on a public IP address that would be fine. Again, you just need to find a Redis .NET assembly that lets you use Redis remotely.
Entity Framework can access a Redis database, and you might want to try that first. You would be using LINQ queries and probably fluent mappings. https://www.cdata.com/kb/tech/redis-ado-codefirst.rst
However if you want to try other libraries, this one is an example: https://github.com/ServiceStack/ServiceStack.Redis
In any event, you would generally want to get a ".NET Standard library" build of whatever data access library you decide to use, although if they don't offer ".NET Standard" then you could use a .NET 3.5 or even 4.6 build and it should work.
You should seriously consider just using PlayFab though and not worrying about building databases, you will save a lot of time although then you will have a dependency on PlayFab.