Saving and Loading a GameObject's Position

Examples using Easy Save's API code
Post Reply
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Saving and Loading a GameObject's Position

Post by Joel »

Saving and Loading a GameObject's position

The following script can be attached to a GameObject to save it's position when it's Destroyed (this happens when the scene is changed, and when the application quits), and load it's position when the scene loads.

Code: Select all

using UnityEngine;

public class SavePosition : MonoBehaviour
{
    // Generate a unique ID for this GameObject.
    public string guid = System.Guid.NewGuid().ToString();

    void Awake()
    {
        transform.localPosition = ES3.Load<Vector3>(guid, transform.localPosition);
    }

    void OnDestroy()
    {
        ES3.Save<Vector3>(guid, transform.localPosition);
    }
}
Attachments
SavePosition.cs
(383 Bytes) Downloaded 887 times
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post by hrohibil »

Please help, I want to use the save and load on buttons.

How do i set this up?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading a GameObject's Position

Post by Joel »

Hi there,

Buttons are part of Unity's functionality, so you can find information on this in their docs:
https://learn.unity.com/tutorial/creating-ui-buttons

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post by hrohibil »

Thank you for reply.

I know how to get the buttons work, but i could not directly transfer the code from the :

Code: Select all

public class SavePosition : MonoBehaviour
{
    // Generate a unique ID for this GameObject.
    public string guid = System.Guid.NewGuid().ToString();

    void Awake()
    {
        transform.localPosition = ES3.Load<Vector3>(guid, transform.localPosition);
    }

    void OnDestroy()
    {
        ES3.Save<Vector3>(guid, transform.localPosition);
    }
}
So what code would i enter to have my players position?
Thank you
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading a GameObject's Position

Post by Joel »

Hi there,

You would simply hook up the Save button to the OnDestroy method, and the Load button to the Awake method.

As the questions you are asking are general Unity programming questions rather than ones specific to Easy Save, I recommend you ask for more advice on the Unity forums. Unfortunately I'm only able to assist with things directly relating to Easy Save.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
hrohibil
Posts: 10
Joined: Thu Aug 19, 2021 5:46 am

Re: Saving and Loading a GameObject's Position

Post by hrohibil »

Thanks Joel

It works perfectly now...

Br Hamid
student80
Posts: 1
Joined: Sat May 27, 2023 1:05 pm

Re: Saving and Loading a GameObject's Position

Post by student80 »

If I use this to save a unit's HP, Exp , etc. Will a bad actor be able to open the app and edit the save file directly ? I saw the encryption function but don't really want to mess up app store submission guidelines...
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading a GameObject's Position

Post by Joel »

Hi there,

No file stored on a user's device is entirely secure. Even the Unity application itself can be tampered with. I recommend taking a look at the encryption guide for more information:

https://docs.moodkie.com/easy-save-3/es ... mpression/

This tends to be less of an issue on iOS as the user would need to jailbreak their device in order to access the file. However, if security is of utmost importance to you then you would need to consult a security analyst as you would likely need a solution which is bespoke to your project.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply