Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a new data to existing Binary file without erase the previous data? 0 Answers

How Do I Upload To An Email Address Like Gmail Or Outlook In Unity 3d Either from PC or Phone andr or ios 0 Answers

Main Camera not showing full sprite 1 Answer

is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer

.json to a sprite 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges