Skip to main content

Posts

Showing posts from December, 2019

FlatBuffers for Unity, part 3 (examples)

This is a third and hoping last part of cycle about using flatbuffers in Unity. Here we take a look at usage example. There is an example of PingPong game. It is not about fascinating gameplay it is just demonstration of flatbuffers usage. First of all, there are three .fbs-files (BallCoordinates, MoveDirection and PlayerAction) that we need to compile into c# and python (.cs and .py respectively). Do not forget that you can choose a flatc-compiler (although there is default one for each of platforms: Windows, MacOS, Linux). After compiling you have to move python files into server-part folder. (Scripts->Server->FbsCompilled). It is time to prepare server: Install python and pip   Optionally you can create python environment: python -m venv /path/to/new/virtual/environment Install requirements from file "requirements.txt" (with the help of pip): pip install -r requirements.txt Run flask server with the help of script run_server.bat. There should be output:

FlatBuffers for Unity, part 2

In previous part we have considered different ways to serialize and deserialize data. In this chapter I tell about tool that facilitates using of flatbuffers in Unity. What should it do? I have highlighted three main points. First of all it should enable to create .fbs messages from Unity Editor. Just as right-click action. Next, it should allow to compile .fbs messages to C# classes (and some other languages). And finally, it should be flexible and allow user to tune tool for his needs. Creating .fbs messages This point is quite easy: we need to add a new menu item "Create .fbs" to Assets menu. You can do it easily by adding a MenuItem attribute to your method. Additionally you can add a validation method that enables/disables menu item. I used this opportunity to check that there is a selected folder (when we need to create a new file). More information you can find here . [MenuItem("Assets/Create/FlatBuffers message")] [MenuItem("Assets/UnityFbs/C

FlatBuffers for Unity, part 1 (overview)

Seems every game development faces a problem: how to serialize and deserialize data? This question occurs in two main scenarios: saving/loading and network communication (how to send data to server or computer). In both cases you want to save some game data (items, players or perhaps game state or action) and restore this data later (might be on other server). Most common solutions What approaches does Unity suggest for that? I have found these variants: PlayerPrefs ( link ); String serializers ( JSON or XML ); Binary serilization; Let me take short overview of each of them. PlayerPrefs  It is one of the most simple way to store data in Unity. It uses built-in system PlayerPrefs that allows to save data in system key-value storage. It has only three functions to store base types of data (int, float and string). PlayerPrefs is not about performance or data transferring. Pros and cons: + Simple to use + Built-in - Does not support data transferring - Does not support co