- Home /
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);
}
}
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.
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();
}
}
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.
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.
Your answer
Follow this Question
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