Data Persistence Three Ways (Saving and Loading in Unity)

Поделиться
HTML-код
  • Опубликовано: 20 сен 2024
  • Here's a copy of the final script if you need it. It takes only a couple changes to get this to look like the second type of persistence. For PlayerPrefs, there isn't a whole lot of typing, so I left that example out of the description, but it is in the beginning of the video.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    public class ScoreIncrease : MonoBehaviour
    {
    public Text scoreText;
    public int score;
    public static ScoreIncrease si;
    private void Awake()
    {
    if (si == null)
    si = this;
    else
    Destroy(this);
    DontDestroyOnLoad(this.gameObject);
    }
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    score++;
    scoreText.text = "Score: " + score;
    }
    if (Input.GetKeyDown(KeyCode.A))
    {
    SceneManager.LoadScene(1);
    }
    }
    public void Save()
    {
    score++;
    scoreText.text = "Score: " + score;
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath +
    "/scoreContainer.dat", FileMode.Create);
    ScoreContainer sc = new ScoreContainer();
    sc.score = score;
    bf.Serialize(file, sc);
    file.Close();
    }
    public void LoadScore()
    {
    if (File.Exists(Application.persistentDataPath +
    "/scoreContainer.dat"))
    {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath +
    "/scoreContainer.dat", FileMode.Open);
    ScoreContainer sc = (ScoreContainer)bf.Deserialize(file);
    file.Close();
    score = sc.score;
    scoreText.text = "Score: " + score;
    }
    }
    }
    [Serializable]
    public class ScoreContainer
    {
    public int score;
    }

Комментарии • 20

  • @TheDJboi2011
    @TheDJboi2011 3 года назад +8

    Data Persistence Three Ways
    1. PlayerPrefs - 3:14
    2. BinaryFormatter - 8:09
    3. Singleton Design Pattern - 14:32

  • @jackhepworth6565
    @jackhepworth6565 4 года назад +1

    BEST RUclipsR EVER!!! thanks m8 u made it so simple i understand it now :D

    • @wolfscrygames
      @wolfscrygames  4 года назад

      Glad it helped! I'm hoping to get back into making content asap.

  • @MiddleCross378
    @MiddleCross378 3 года назад +1

    CRAZY HELPFUL! Thank you so much for this!

  • @artodubuisson7621
    @artodubuisson7621 4 года назад +4

    Hey ! Does the binary formatting work on mobile or do I need to change something in order to make it work ? Thanks for the amazing tutorial though, loved it !

    • @wolfscrygames
      @wolfscrygames  4 года назад

      It does work on mobile, but be sure your file paths are valid. docs.unity3d.com/ScriptReference/Application-dataPath.html has more info as well as the page on persistent data path.

    • @artodubuisson7621
      @artodubuisson7621 4 года назад

      @@wolfscrygames Thanks a lot !

  • @karimshow777
    @karimshow777 5 месяцев назад

    Google bard (Gemini) recommended me your video lol

  • @RomanKrivocheev
    @RomanKrivocheev 3 года назад +1

    Thanks a lot! Great tutorial!

  • @aldigangster123
    @aldigangster123 4 года назад

    Hey, I'm currently digging deep to research ways how to have persistent data across multiple scenes and handle managers properly. Your last sentence caught my interest. Would love to see an advanced topic video on using Singletons with additive scene loading! This seems to be solid solution.
    I can't find any videos on this yet unfortunately.

    • @wolfscrygames
      @wolfscrygames  4 года назад +1

      Hi,
      You can create a scene with any singletons you want to keep active. You then use SceneManager.LoadScene("scene name", LoadSceneMode.Additive); to put a new scene in. You can use SceneManager.UnloadSceneAsync to unload a scene in order to switch to a new one. This process will keep the original first scene with all the singletons in it.

  • @landoringel868
    @landoringel868 4 года назад +1

    great tutorial!

  • @amrsadek5853
    @amrsadek5853 Год назад +1

    Im mostly interested in saving the data in a binary format. Is there a way to create a new file every time you run the game that contains new data, and to access this file even after closing unity?

    • @wolfscrygames
      @wolfscrygames  Год назад

      Sure, you'll want to add an incremental index to your filename, or some other unique identification like a time stamp.
      Save one file with last used index.
      Load that file on startup if it exists, and add 1 to current index.
      Append your main data file name with this index.
      Whatever folder you save it into will have these progressive save files.

  • @vAscA187
    @vAscA187 4 года назад

    thank you a mil buddy! i was sick of seeing player prefs tutorials :D
    also how do i force unity to keep camera width fixed and fit the height to the screen aspect ratio?
    in default unity has a fixed camera height and fits the width... is there a checkbox somewhere or do i have to play with the viewport from code?
    Cheers! :D

  • @goldengemgames2939
    @goldengemgames2939 Год назад

    can't seem to find my saved file in appdata for some reason.

  • @yonathanjaimes7142
    @yonathanjaimes7142 2 года назад +1

    Hi, thank you for share your knowledge, I would like to ask you how can I delete the binary file? Thank you.

    • @wolfscrygames
      @wolfscrygames  2 года назад

      It's actually a file in the path where you saved it. You can just go through your file browser and delete as needed.

  • @2n865
    @2n865 3 года назад

    Thank you!