Data Persistence Three Ways (Saving and Loading in Unity)
0 بار بازدید -
5 سال پیش
-
Here's a copy of the
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;
}
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;
}
5 سال پیش
در تاریخ 1398/09/12 منتشر شده
است.
0
بـار بازدید شده