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 /
avatar image
0
Question by Bondolero · Dec 02, 2015 at 10:06 AM · scripting beginnerplayerprefssave file

How to save sprite to Playerprefs

Hi, i'm a new developer and i am making a mobile game. I barely understand c# so could someone help me because i can't find any solution for my problem i don't understand why isn't this script working

console give me an error on this line: PlayerPrefs.SetString ("CurrentSprite", sr.sprite);

my script:

using UnityEngine; using System.Collections;

public class playerspriteimagechange : MonoBehaviour {

 public GameObject ball;
 public Sprite pinksquare;
 public Sprite triangle;
 public Sprite ball2;
 private SpriteRenderer sr;



 void Start()
 {
     sr = GetComponent<SpriteRenderer> ();

 }


 public void onClick()
 {

     sr.sprite = triangle;
     PlayerPrefs.SetString ("CurrentSprite", sr.sprite);

 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cherno · Dec 02, 2015 at 10:50 AM

You can't use PlayerPrefs to save a component like a SpriteRenderer, plain and simple. If you raly want to use PlayerPrefs for this, then you have to find a workaround. For example, you could store the name of the GameObject the SR is on, and then load that name again and use it with GameObject.Find to get teh GameObject in the scene, and then use GetComponent to access the SpriteRenderer.

Edit: If you are getting an error, please post the actual error message.

Comment
Add comment · Share
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
avatar image
1

Answer by Al-Maliki · Apr 08, 2019 at 07:25 AM

you must convert it to byte then convert it to string for saving its sprite to "PlayerPrefs"


Here's a sample code:

 using UnityEngine;
 using System;
 
 public class SpriteManager : MonoBehaviour
 {
     [SerializeField]
     private Sprite mySprite;

     private void Start()
     {
         this.SaveSprite();        
     }

     private void SaveSprite()
     {
         Texture2D texture = this.sprite.texture;
         byte[] texAsByte = texture.EncodeToPNG();
         string texAsString = Convert.ToBase64String(texAsByte);
 
         string myKey = "MySprite";
         PlayerPrefs.SetString(myKey, texAsString);
         PlayerPrefs.Save();
     }
 }


Comment
Add comment · Show 1 · Share
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
avatar image TheGameLearner · Apr 09, 2019 at 05:30 AM 0
Share

PlayerPrefs are used to store data in memory, but sprites are downloaded and already present at a predefined location(either an asset or as resources if downloaded from asset bundles). Either way, we have the complete sprite at a location, it would be easier to store a reference to it rather than copy it as Byte array, convert that to string and store it... That sounds very expensive both memory wise and performance wise.

avatar image
0

Answer by TheGameLearner · Jul 25, 2018 at 09:11 AM

Replying Late but saw the question was not marked with an accepted answer :D

You can only store String in PlayerPrefs if you use "PlayerPrefs.SetString ("CurrentSprite", sr.sprite);" What you can do is store all sprites that you might be changing through in an array and store the Int value of accessing it. When your game begins, re-assign the correct sprites using PlayerPrefs in awake so the player will not see old sprites.

In case you needed a workaround and used any other way, let me know. I personally use this idea most of the time.

Comment
Add comment · Share
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

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

7 People are following this question.

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

Related Questions

Not sure what is wrong with my timer script. 1 Answer

I'm not sure what is wrong with my script 0 Answers

Unity problem when i convert playerprefs save to the standard file io save 0 Answers

Coins not updating after making shop transactions 0 Answers

Save different game object's datas using the same script? 3 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