parent
e603263804
commit
a312a489a3
@ -1,18 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
|
||||
public class SaveLoad : MonoBehaviour
|
||||
// Example class to save and load
|
||||
[Serializable]
|
||||
class PlayerData
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
public int score;
|
||||
public string name;
|
||||
}
|
||||
|
||||
class SaveLoad
|
||||
{
|
||||
// Save method
|
||||
public void Save(PlayerData data, string fileName)
|
||||
{
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
using (FileStream stream = new FileStream(fileName, FileMode.Create))
|
||||
{
|
||||
formatter.Serialize(stream, data);
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
// Load method
|
||||
public PlayerData Load(string fileName)
|
||||
{
|
||||
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
using (FileStream stream = new FileStream(fileName, FileMode.Open))
|
||||
{
|
||||
return (PlayerData)formatter.Deserialize(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue