How to SAVE and LOAD data in Unity3D with Realm SDK | Persistence | Databases | Tutorial | Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to a new tutorial today we're not going to talk about playing games but rather how to make them more specifically how to use realm sdk to persist your data in unity i'm going to show you where to download the sdk how to edit to your project and how to write the necessary code to save and load your data let's get started realm is an open source cross-platform database available for many different platforms since we will be working with unity we'll be using the realm.net sdk this is not yet available through the unity package manager so we need to download it from the github repository directly i will put a link into the description when you go to releases you can find the latest release right on top of the page within the assets you'll find two unity files make sure to choose the file that says unity.bundle and download it before we actually start integrating realm into our unity project let's create a small example i'll be creating it from scratch so you can follow along all the way and see how easy it is to integrate well into your project we will be using the unity editor version 2021.2.0 alpha 10. we need to use this alpha version because there is a bug in the current lts version preventing realm from working properly i'll give it a name and choose 2d template for this example we won't be doing much in the unity editor itself most of the example would take place in code all we need to do here is to add a square object the square will change its color when we click on it and as soon as we add realm to the project the color will be persisted to the database and load it again when we start the game the square needs to be clickable therefore we need to add a collider i will choose a box collider 2d in this case finally we'll add a script to the square call it square and open the script first thing we're going to do before we actually implement the squares behavior is to add another class that will hold our data the color of the square we call this color entity all we need for now are three properties for the colors red green and blue there will be of type float to match unity engine's color properties and we'll default them to 0 giving us an initial black color backing the square mono behavior i'll add a color entity property since we need that in several locations during the awake of the square we'll create a new color entity instance and then set the color of that square to the newly created color entity by accessing its sprite renderer and setting its color when we go back to the unity editor and enter player mode we'll see a black square alright let's add the color change since we added a collider to the square we can use the on mouse down event to listen for mouse clicks all we want to do here is to assign three random values to our color entity we'll use random.range and clamp it between 0 and 1. finally we need to update the square with these colors to avoid duplicated code i grabbed the line from awake where we set the color and put it in its own function now we just call it an away and after every mask let's have a look at the result initially we get our default black color again and with every mouse click the color changes when i stop and start again we'll of course end up in the initial default again since the color is not yet saved let's do that next to add realm to the project we go to window package manager from here we click the plus icon and choose add package from tarball you now have to choose the tarball downloaded earlier keep in mind that unity does not import the package and save it within your project but use exactly this file wherever it is if you move it your project won't work anymore i recommend moving this file from the downloads to the project folder first as soon as it is imported you should feed in the custom section of your package list that's all we need to do in the unity editor let's go back to visual studio let's start with our color entity first we want to import the round package by adding using realms the way around knows which objects are meant to be saved in a database is by subclassing realm objects that's all we have to do here really to make our life a little bit easier though i'll also add some more things first we want to have a primary key by which we can later find the object we're looking for easily we'll just use the object name for that and add an attribute on top of it called primary key next we add a default initializer to create a new realm object for this class and a convenience initializer that sets the object name right away okay back to our square we need to import realm here as well then we'll create a property for the realm itself this will later let us access our database and all we need to do to get access to it is to instantiate it we will do this during awake as well since it only needs to be done once now that we're done setting up our realm we can go ahead and look at how to perform some simple crowd operations first we want to actually create the object in the database we do this by calling n notice that i have to put this into a block that is passed to the right function we need to do this to tell our realm that we are about to change data if another process was changing data at the same time we could end up in a corrupt database the write function makes sure that every other process is blocked from writing to the database at the time we are performing this change another thing i'd like to add is a check if the color entity which is created already exists if so we don't need to create it again in effect can't since primary keys have to be unique we do this by asking our realm for the color entity we're looking for identified by its primary key i just call it square for now now i check if the object could be found and only if not we'll be creating it with exactly the same primary key and add it to the realm whenever we update the color and therefore update the properties of our color entity we change data in our database therefore we also need to wrap our mouse click within a write block let's see how that looks in unity when we start the game we see the initial black state we can randomly update the color by clicking on the square and when we stop and start play mode again we see the color persists now let's quickly recap what we've done we added the round package in unity and imported it in our script we added the super class realm object to our class that's supposed to be saved and then all we needed to do is to make sure we always start the right transaction when we're changing data notice that we did not need any transaction to actually read the data down here in the set color function alright that's it for this tutorial i hope you've learned how to use realm to save and load data in unity if this video was helpful for you please consider leaving a like and subscribe for more content if you have any questions just leave a comment down below see you next time
Info
Channel: dodoTV42
Views: 1,765
Rating: undefined out of 5
Keywords: realm, unity, persistence, save, load, gamedev, unity3d, tutorial, unity3d tutorial, unity3d game tutorial, realm sdk, realm .net sdk, unity tutorial, unity game development, unity game tutorial, unity game dev, guide, how to, brackeys, playerprefs, playerprefs unity, unity brackeys, playerprefs brackeys, playerprefs unity tutorial, dotnet, c sharp, c sharp for beginners, c sharp for unity
Id: 8jo_S02HLkI
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Tue Apr 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.