Question by
ParteMisto · Aug 16, 2017 at 11:39 AM ·
spritesavefilestream
How can i save a sprite with FileStream
I have this code that creates shops that can interact with each other. Basically I want to save which items are in which shops (lists). However when i tried to use FileStream I couldn't save the sprites from the items. Anyone could help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[System.Serializable]
public class Item
{
public string itemName;
public Sprite icon;
public float price = 1f;
}
public class ShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public ShopScrollList otherShop2;
public ShopScrollList otherShop3;
public ShopScrollList otherShop4;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
public float gold = 20f;
public int shop2 = 0;
public int shop3 = 0;
public int shop4 = 0;
public Button backbutton;
// Use this for initialization
void Start () {
RefreshDisplay ();
if (System.IO.File.Exists ("save.dat")) {
using (Stream stream = File.Open ("save.dat", FileMode.Open)) {
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();
List <Item> itemList = (List<Item>)bformatter.Deserialize (stream);
}
}
}
public void RefreshDisplay()
{
myGoldDisplay.text = "" + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
private void AddButtons()
{
for (int i = 0; i < itemList.Count; i++) {
Item item = itemList [i];
GameObject newButton = buttonObjectPool.GetObject ();
newButton.transform.SetParent (contentPanel, false);
SampleButton sampleButton = newButton.GetComponent<SampleButton> ();
sampleButton.Setup (item, this);
}
}
private void RemoveButtons()
{
while (contentPanel.childCount > 0) {
GameObject toRemove = transform.GetChild (0).gameObject;
buttonObjectPool.ReturnObject (toRemove);
}
}
public void TryTransferItemToOtherShop (Item item)
{
if (this.gold >= item.price) {
gold -= item.price;
AddItem (item, otherShop);
RemoveItem (item, this);
RefreshDisplay ();
otherShop.RefreshDisplay ();
}
if (otherShop2 != null){
if (shop2 == 1) {
AddItem (otherShop2.itemList [0], this);
AddItem (item, otherShop2);
PlayerPrefs.SetString ("cesto", item.itemName);
otherShop2.itemList.RemoveAt (0);
RemoveItem (item, this);
shop2 = 0;
backbutton.onClick.Invoke();
RefreshDisplay ();
otherShop2.RefreshDisplay ();
}
}
if (otherShop3 != null){
if (shop3 == 1) {
AddItem (otherShop3.itemList [0], this);
AddItem (item, otherShop3);
otherShop3.itemList.RemoveAt (0);
RemoveItem (item, this);
shop3 = 0;
backbutton.onClick.Invoke();
RefreshDisplay ();
otherShop3.RefreshDisplay ();
}
}
if (otherShop4 != null){
if (shop4 == 1) {
AddItem (otherShop4.itemList [0], this);
AddItem (item, otherShop4);
otherShop4.itemList.RemoveAt (0);
RemoveItem (item, this);
shop4 = 0;
backbutton.onClick.Invoke();
RefreshDisplay ();
otherShop4.RefreshDisplay ();
}
}
}
private void AddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
FileStream fs = new FileStream ("save.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (fs, itemList);
fs.Close ();
}
private void RemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--) {
if (shopList.itemList [i] == itemToRemove) {
shopList.itemList.RemoveAt (i);
}
}
FileStream fs = new FileStream ("save.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize (fs, itemList);
fs.Close ();
}
}
Comment
Your answer
