How playerprefs for sprite (image) save?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class ChangeAvatar : MonoBehaviour
{
public Image NewImage;
public Sprite newSprite;

public Image OldImage;
public Sprite newSprite2;

public void Start()
{
DontDestroyOnLoad(this.gameObject);
this.NewImages();
this.OldImages();
}

public void NewImages()
{

NewImage.sprite = newSprite;
PlayerPrefs.Save();
}

public void OldImages()
{

OldImage.sprite = newSprite2;
PlayerPrefs.Save();
}
}

There is how save sprite?

I don’t understand what you are trying to save, since you already have Start calling both methods and setting the value of NewImage and OldImage to sprites that are most likely connected in your inspector.

If you are changing the sprites in newSprite and newSprite2 in another script, then that is where you want to determine what those sprites are. You can’t save sprites as a playerpref, so you have to save a string or int or something else that helps you determine what sprite to use, if that is what you are trying to do. PlayerPrefs saves values, but is limited. Calling PlayerPrefs.Save on it’s own without setting any values does nothing.

You can easily look up Unity PlayerPrefs to know more about them.

1 Like