Page 1 of 1

Saved sprite doesn't load on another scene

Posted: Wed Sep 26, 2018 12:58 pm
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");}

Re: Saved sprite doesn't load on another scene

Posted: Wed Sep 26, 2018 11:24 pm
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

Re: Saved sprite doesn't load on another scene

Posted: Thu Sep 27, 2018 4:48 am
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.

Re: Saved sprite doesn't load on another scene

Posted: Thu Sep 27, 2018 7:37 am
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

Re: Saved sprite doesn't load on another scene

Posted: Thu Oct 14, 2021 1:46 am
by cptscrimshaw
I saw that this feature was added, but I wasn't able to find documentation on how to implement it (I'm running into the same trouble of having sprite references not being saved). Apologies for necroing this thread if this was covered elsewhere - I looked around for a while.

Thanks!

Re: Saved sprite doesn't load on another scene

Posted: Thu Oct 14, 2021 9:44 am
by Joel
Hi there,

This is done automatically assuming that your Sprite isn’t generated or modified at runtime.

Please could you create a new thread if you are having issues showing the code you are using to save and load your Sprite so I can try to replicate it at my end.

All the best,
Joel