Saved sprite doesn't load on another scene

Discussion and help for Easy Save 3
Post Reply
User avatar
fanaei
Posts: 26
Joined: Thu Aug 23, 2018 7:54 am

Saved sprite doesn't load on another scene

Post by fanaei »

Hi,
I'm saving a Sprite for my inventory item and it loads fine when I stay on the scene that I was. But if I go to another scene, when I load the inventory, everything loads fine except Sprites. They're null and I don't know why!
I'm using this code for saving:

Code: Select all

ES3.Save<Sprite[]> ("itemSprite", itemSprite);
And this one for loading:

Code: Select all

if (ES3.KeyExists ("itemSprite")) {itemSprite = ES3.Load<Sprite[]> ("itemSprite");}

User avatar
Joel
Moodkie Staff
Posts: 3385
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saved sprite doesn't load on another scene

Post by Joel »

Hi there,

This is likely because the reference ID of the Sprite will be different in different scenes.

The easiest way to fix this is to copy the Easy Save 3 Manager from the scene where you saved the Sprite. And then in the scene where you're loading the Sprite, delete the Easy Save 3 Manager and paste the one you just copied from the other scene. Finally, press Refresh References on the ES3ReferenceMgr component of the Easy Save 3 Manager object to ensure that all of the references for your scene are added to the new manager.

All the best,
Joel
Joel @ Moodkie Interactive
Buy Easy Save | Contact | Guides | Docs | Getting started

User avatar
fanaei
Posts: 26
Joined: Thu Aug 23, 2018 7:54 am

Re: Saved sprite doesn't load on another scene

Post by fanaei »

Thank you Joel. But I have lots of scenes in my game and this solution doesn't seem a suitable way.
Do you have a better solution?
Also if you fix this problem inside your plugin, it would be so great.

User avatar
Joel
Moodkie Staff
Posts: 3385
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saved sprite doesn't load on another scene

Post by Joel »

Hi there,

I've created a feature request for this here: https://moodkie.com/forum/viewtopic.php ... 5118#p5118

However, please be aware that such an implementation would be non-trivial, and we've not encountered any other users who have required this feature yet, so I cannot give an estimate on when this feature would be included.

In the meantime the other alternative would be to create your own list of references to Sprites, and save indices into this list. For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteManager : MonoBehaviour 
{
	// Drag your Sprites into this array in the Editor.
	public Sprite[] sprites;

	public void SaveSpriteArray(string key, Sprite[] sprites)
	{
		ES3.Save<int[]> (key, GetIndexes(sprites));
	}

	public Sprite[] LoadSpriteArray(string key)
	{
		return GetSprites(ES3.Load<int[]>(key, new Sprite[0]));
	}


	// Get the index of a single sprite.
	private int GetIndex(Sprite sprite)
	{
		for(int i=0; i<sprites.Length; i++)
			if (sprites == sprite)
				return i;
		Debug.LogError("Sprite "+sprite.name+" does not exist in sprite manager");
		return -1;
	}

	// Get the indices for an array of Sprites.
	private int[] GetIndexes(Sprite[] spriteArray)
	{
		var indices = new int[spriteArray.Length];
		for(int i = 0; i < spriteArray.Length; i++)
			indices = GetIndex(spriteArray );
		return indices;
	}

	// Gets the Sprite for an index.
	private Sprite GetSprite(int index)
	{
		return sprites[index];	
	}

	// Gets the Sprites for an array of indices.
	private Sprite[] GetSprites(int[] indices)
	{
		var spriteArray = new Sprite[indices.Length];
		for(int i = 0; i < spriteArray.Length; i++)
			spriteArray  = GetSprite(indices);
		return spriteArray;	
	}
}


You would then put your Sprites into the sprites array in the Editor and use it's SaveSpriteArray and LoadSpriteArray methods to save and load your sprites.

If you did not want to manually put in a manager for each scene, you could create a prefab in a Resources folder with this Component, and load this prefab from Resources to access the manager.

All the best,
Joel
Joel @ Moodkie Interactive
Buy Easy Save | Contact | Guides | Docs | Getting started

Post Reply