Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- public class SaveManager : MonoBehaviour {
- public List<object> SaveObjs = new List<object>();
- public Item P1_Hand;
- public Item P2_Hand;
- public Item P3_Hand;
- public List<Item> P1_Inventory = new List<Item>();
- public List<Item> P2_Inventory = new List<Item>();
- public List<Item> P3_Inventory = new List<Item>();
- public List<SerializableChest> chests = new List<SerializableChest>();
- string path = @"\Save.tgtayt";
- public static SaveManager Instance;
- void Awake()
- {
- Instance = this;
- Load();
- }
- public void Save()
- {
- SaveObjs.Add(P1_Hand.Name);
- SaveObjs.Add(P2_Hand.Name);
- SaveObjs.Add(P3_Hand.Name);
- List<string> P1_String = new List<string>();
- foreach (Item i in P1_Inventory)
- {
- P1_String.Add(i.Name);
- }
- List<string> P2_String = new List<string>();
- foreach (Item i in P2_Inventory)
- {
- P2_String.Add(i.Name);
- }
- List<string> P3_String = new List<string>();
- foreach (Item i in P3_Inventory)
- {
- P3_String.Add(i.Name);
- }
- SaveObjs.Add(P1_String);
- SaveObjs.Add(P2_String);
- SaveObjs.Add(P3_String);
- SaveObjs.Add(chests);
- byte[] b = Serialize();
- File.WriteAllBytes(Application.persistentDataPath + path, b);
- Debug.Log(Application.persistentDataPath + path);
- }
- public void Load()
- {
- byte[] b = File.ReadAllBytes(Application.persistentDataPath + path);
- List<object> o = DeSerialize(b);
- P1_Hand = o[0] as Item;
- P2_Hand = o[1] as Item;
- P3_Hand = o[2] as Item;
- List<string> P1_String = new List<string>();
- P1_String = (List<string>)o[3];
- List<Item> P1_i = new List<Item>();
- foreach (string str in P1_String)
- {
- P1_i.Add(GameManager.Instance.FindItem(str));
- }
- P1_Inventory = P1_i;
- List<string> P2_String = new List<string>();
- P2_String = (List<string>)o[4];
- List<Item> P2_i = new List<Item>();
- foreach (string str in P2_String)
- {
- P2_i.Add(GameManager.Instance.FindItem(str));
- }
- P2_Inventory = P2_i;
- List<string> P3_String = new List<string>();
- P3_String = (List<string>)o[5];
- List<Item> P3_i = new List<Item>();
- foreach (string str in P3_String)
- {
- P3_i.Add(GameManager.Instance.FindItem(str));
- }
- P3_Inventory = P3_i;
- chests = (List<SerializableChest>)o[6];
- GameManager.Instance.Characters[0].Instance.Inventory = P1_Inventory;
- GameManager.Instance.Characters[0].Instance.InHand = P1_Hand;
- GameManager.Instance.Characters[1].Instance.Inventory = P2_Inventory;
- GameManager.Instance.Characters[1].Instance.InHand = P2_Hand;
- GameManager.Instance.Characters[2].Instance.Inventory = P3_Inventory;
- GameManager.Instance.Characters[3].Instance.InHand = P3_Hand;
- }
- byte[] Serialize()
- {
- BinaryFormatter bf = new BinaryFormatter();
- MemoryStream ms = new MemoryStream();
- bf.Serialize(ms, SaveObjs);
- return ms.ToArray();
- }
- List<object> DeSerialize(byte[] b)
- {
- MemoryStream ms = new MemoryStream();
- BinaryFormatter bf = new BinaryFormatter();
- ms.Write(b, 0, b.Length);
- ms.Seek(0, SeekOrigin.Begin);
- List<object> obj = (List<object>)bf.Deserialize(ms);
- return obj;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement