Mobile Game Tutorial part 14 – Save System.

Hello, in this video i will show you how to create Save System for our game:

CODE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveSystem : MonoBehaviour
{
    public static SaveSystem instance { get; set; }

    public void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != null)
        {
            Destroy(gameObject);
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Save()
    {
        RocketController RC = FindObjectOfType<RocketController>();
        LaunchPlatform LP = FindObjectOfType<LaunchPlatform>();

        PlayerPrefs.SetFloat("money", GameManager.instance.money);

        PlayerPrefs.SetFloat("thrustForceLVL", RC.thrustForceLVL);
        PlayerPrefs.SetFloat("maxFuelLVL", RC.maxFuelLVL);
        PlayerPrefs.SetFloat("strafeSpeedLVL", RC.strafeSpeedLVL);
        PlayerPrefs.SetFloat("launchForceMultiplierLVL", LP.launchForceMultiplierLVL);
    }

    public void LoadMoney()
    {
        GameManager.instance.money = PlayerPrefs.GetFloat("money");
    }

    public void LoadRocketParam()
    {
        RocketController RC = FindObjectOfType<RocketController>();

        RC.thrustForceLVL = PlayerPrefs.GetFloat("thrustForceLVL");
        RC.maxFuelLVL = PlayerPrefs.GetFloat("maxFuelLVL");
        RC.strafeSpeedLVL = PlayerPrefs.GetFloat("strafeSpeedLVL");
    }

    public void LoadPlatformParams()
    {
        LaunchPlatform LP = FindObjectOfType<LaunchPlatform>();
        LP.launchForceMultiplierLVL = PlayerPrefs.GetFloat("launchForceMultiplierLVL");       
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *