How to save and load a VECTOR2 and VECTOR3 with PlayerPrefs in Unity

Introduction

The Play­er­Prefs class of Uni­ty does not have a spe­cif­ic method to store vec­tors, how­ev­er it has three func­tions that allow to store data of type inte­ger, float and strings, the types of vari­able int and float are known as prim­i­tive vari­ables and with them it is pos­si­ble to recon­struct oth­er more com­plex data struc­tures, and is that a vec­tor of R2 or R3, if we think about it, is noth­ing more than an array of three vari­ables of type float, that each one occu­pies a posi­tion or has a meaning.

Dear read­er

ūüć∑ūüßź

Unity package to download with implemented solution

Below you can down­load the Uni­ty pack­age with the exam­ple we are going to ana­lyze in this arti­cle, so you can import it and test it direct­ly on your own computer.

GameDe¬≠v¬≠Traum ‚ÄĒ Save Vec¬≠tors with Play¬≠er Prefs

files from a unity package with implemented solution for saving and loading vector3 in unity using playerprefs
Fig. 1: Files that are added to your project when import­ing the down­load package.

Analysis on how to save a Vector3 with PlayerPrefs

The basic idea is to decom­pose the vec­tor into its com­po­nents that are float data and then store those com­po­nents indi­vid­u­al­ly with Play­er­Prefs, then, when load­ing the data, retrieve each com­po­nent from mem­o­ry and cre­ate a new vec­tor using those components.

In the scene that comes in the down¬≠load pack¬≠age the solu¬≠tion is already assem¬≠bled, in fig¬≠ure 2 we see how the hier¬≠ar¬≠chy is com¬≠posed, we have a GameOb¬≠ject called "Save¬≠DataScript" which is the one that has assigned the Script that we are going to ana¬≠lyze (see fig¬≠ure 3) and this is respon¬≠si¬≠ble for sav¬≠ing and load¬≠ing data. Then we have anoth¬≠er GameOb¬≠ject called "Object¬≠ToSave¬≠Po¬≠si¬≠tion" that is a cube to which we will save its posi¬≠tion to be able to load it when start¬≠ing the scene. Notice that in the inspec¬≠tor in fig¬≠ure 3, our Script has a ref¬≠er¬≠ence to this object, this will allow you to read and mod¬≠i¬≠fy its vari¬≠ables or exe¬≠cute func¬≠tions on this GameObject, 

I always insist that under­stand­ing the con­cept of ref­er­ence in pro­gram­ming is a very impor­tant pil­lar in the devel­op­ment of appli­ca­tions with Uni­ty, in this link you will find a playlist of my chan­nel about ref­er­ences in pro­gram­ming and dif­fer­ent meth­ods to find them.

GameObject of the scene that will contain the script to save and load a Vector3 in Unity
Fig. 2: GameOb¬≠ject of the scene that will con¬≠tain the script to save and load a Vector3 in Unity.
Script to save and load a Vector3 in Unity seen from the inspector
Fig. 3: Script to save and load a Vector3 in Uni­ty seen from the inspector.

Script that is responsible for saving and loading the vector

In fig¬≠ure 4 we see a part of the data sav¬≠ing script that comes in the pack¬≠age, we can see the GameOb¬≠ject type vari¬≠able called "objectWith¬≠Saved¬≠Po¬≠si¬≠tion" that appears in the inspec¬≠tor in fig¬≠ure 3 and also the Awake and Start meth¬≠ods, which are func¬≠tions use for ini¬≠tial¬≠iza¬≠tion that Uni¬≠ty exe¬≠cutes auto¬≠mat¬≠i¬≠cal¬≠ly at dif¬≠fer¬≠ent times with¬≠in the life cycle of the appli¬≠ca¬≠tion. Inside the Awake func¬≠tion is exe¬≠cut¬≠ed a cus¬≠tom func¬≠tion called "Load¬≠Da¬≠ta" which is respon¬≠si¬≠ble for load¬≠ing the infor¬≠ma¬≠tion, this "Load¬≠Da¬≠ta" func¬≠tion is defined by our¬≠selves, we see it later. 

The data load¬≠ing is some¬≠thing that nor¬≠mal¬≠ly hap¬≠pens when start¬≠ing a scene, some¬≠times prob¬≠lems can arise depend¬≠ing on where the data load¬≠ing is done, remem¬≠ber that the Start func¬≠tions are exe¬≠cut¬≠ed one after anoth¬≠er for each GameOb¬≠ject in an order that we can not pre¬≠dict or that would be tedious to pre¬≠dict, imag¬≠ine that a Script in its Start func¬≠tion uses vari¬≠ables from anoth¬≠er Script that has not yet loaded its data!

Script to save and load Vector3 in Unity, variables and initialization
Fig. 4: Script to save and load Vector3 in Uni­ty, vari­ables and initialization.

En los jue¬≠gos se sue¬≠len ten¬≠er ata¬≠jos para guarda¬≠do y car¬≠ga r√°p¬≠i¬≠da, en la figu¬≠ra 5 ten¬≠emos unas instruc¬≠ciones que hacen pre¬≠cisa¬≠mente esto, noten que al pre¬≠sion¬≠ar F5 se eje¬≠cu¬≠ta una fun¬≠ci√≥n lla¬≠ma¬≠da "Save¬≠Da¬≠ta" que se encar¬≠ga de hac¬≠er el guarda¬≠do, ser√≠a con¬≠ve¬≠niente que el guarda¬≠do de todas las vari¬≠ables nece¬≠sarias se haga den¬≠tro de esa fun¬≠ci√≥n o que en el inte¬≠ri¬≠or se lla¬≠men a otras fun¬≠ciones que se encar¬≠guen de guardar otros datos, de esa for¬≠ma una vez que eje¬≠cu¬≠ta¬≠mos la instruc¬≠ci√≥n Save¬≠Da¬≠ta esta¬≠mos seguros de que toda la infor¬≠ma¬≠ci√≥n se ha guarda¬≠do, lo mis¬≠mo para la fun¬≠ci√≥n "Load¬≠Da¬≠ta" que se encar¬≠ga de leer los datos guarda¬≠dos en la memo¬≠ria e ini¬≠cializar las vari¬≠ables con esos datos.

In games we usu¬≠al¬≠ly have short¬≠cuts for sav¬≠ing and fast load¬≠ing, in fig¬≠ure 5 we have some instruc¬≠tions that do just this, notice that when press¬≠ing F5 a func¬≠tion called "Save¬≠Da¬≠ta" is exe¬≠cut¬≠ed that is in charge of sav¬≠ing all the rel¬≠e¬≠vant infor¬≠ma¬≠tion and the same for the "Load¬≠Da¬≠ta" func¬≠tion when press¬≠ing F8.

Functions for testing the saving and loading of a Vector3 in Unity
Fig. 5: Func¬≠tions for test¬≠ing the sav¬≠ing and load¬≠ing of a Vector3 in Unity.

Example on how to save a Vector3 using PlayerPrefs

Fig­ure 6 shows the con­tent of the Save­Da­ta func­tion, which is respon­si­ble for sav­ing the vec­tor data that will lat­er allow us to recon­struct it. Note that first a decom­po­si­tion of the vec­tor to be saved is made in its X, Y and Z vari­ables, which are stored in the tem­po­rary vari­ables "posi­tionX", "posi­tionY" and "posi­tionZ".

The sav­ing of data with Play­er­Prefs is done in the last three instruc­tions, using the "Set­Float" func­tion from Play­er­Prefs,note the name that is passed as a label of these saves, these names must be used in the data load to retrieve them.

Instructions to SAVE a Vector3 data type with PlayerPrefs in Unity
Fig. 6: Instruc¬≠tions to SAVE a Vector3 data type with Play¬≠er¬≠Prefs in Unity.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Example on how to load a Vector3 using PlayerPrefs

Fig¬≠ure 7 shows the con¬≠tent of the Load¬≠Da¬≠ta func¬≠tion that is respon¬≠si¬≠ble for load¬≠ing the data stored in mem¬≠o¬≠ry and make a recon¬≠struc¬≠tion of the vec¬≠tor, the load is the reverse process, first we retrieve the data from mem¬≠o¬≠ry, we do this with the "Get¬≠Float" func¬≠tion from Play¬≠er¬≠Prefs, pass¬≠ing the label that was used for each data and in this exam¬≠ple I include the val¬≠ue 0 in case there is no pre¬≠vi¬≠ous¬≠ly stored infor¬≠ma¬≠tion, this allows us to call it direct¬≠ly in Start or Awake and to make sure there are no con¬≠flicts the first time the appli¬≠ca¬≠tion is run.

The next instruc­tion is in charge of cre­at­ing Vector3 from the data retrieved from mem­o­ry. Vector3 and in gen­er­al most class­es have con­struc­tors that allow us to cre­ate the data by giv­ing them ini­tial val­ues, we use that con­struc­tor with the "new" keyword.

The prob¬≠lem does not end here, as we have read the infor¬≠ma¬≠tion, cre¬≠ate the vec¬≠tor but we have not yet told our GameOb¬≠ject to posi¬≠tion itself on that Vector3 coor¬≠di¬≠nate, this is done in the last instruc¬≠tion in Fig¬≠ure 7.

Instructions to LOAD a Vector3 data type with PlayerPrefs in Unity
Fig. 7: Instruc¬≠tions to LOAD a Vector3 data type with Play¬≠er¬≠Prefs in Unity.

Scroll to Top
Secured By miniOrange