Error: Save all prefabs in resources folder

Discussion and help for Easy Save 3
Post Reply
tuanha
Posts: 12
Joined: Wed Jun 08, 2022 1:41 am

Error: Save all prefabs in resources folder

Post by tuanha »

I have a problem, i want to save all prefabs in resources folder, but when loading the list array only has the first element pointing to the prefab in the Shop3d/Nam/hair folder, the other elements are not related link to the prefab in the directory. Below is my code, please advise.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ES3PrefabManager : MonoBehaviour
{
	public Button btnSave;
	public Button btnLoad;

	public string id = System.Guid.NewGuid().ToString();
	public List<GameObject> prefabInstances = new List<GameObject>();

	void Start()
	{
		
		btnSave.onClick.AddListener(delegate {

			System.Object[] obj_temporary = Resources.LoadAll("Shop3d/Nam/hair", typeof(object));
			ES3.Save<int>(id + "count", obj_temporary.Length);
			foreach (GameObject obj in obj_temporary)
			{
				prefabInstances.Add(obj);
            }				
			ES3.Save<List<GameObject>>(id, prefabInstances);
		});

		btnLoad.onClick.AddListener(delegate {
            int count = ES3.Load<int>(id + "count", 0);
            // If there are prefabs to load, load them.
            if (count > 0)
            {
                prefabInstances = ES3.Load<List<GameObject>>(id);
            }
        });
	
	}


	public void DeletePrefabs()
	{
		// Delete the keys relating to this prefab.
		ES3.DeleteKey(id);
		ES3.DeleteKey(id + "count");
		// Destroy each created prefab, and then clear the List.
		for (int i = 0; i < prefabInstances.Count; i++)
			Destroy(prefabInstances[i].gameObject);
		prefabInstances.Clear();
	}
}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Error: Save all prefabs in resources folder

Post by Joel »

Hi there,

Have you followed the Saving and Loading Prefab Instances section of the Saving and Loading GameObjects guide?
https://docs.moodkie.com/easy-save-3/es ... 0instances

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tuanha
Posts: 12
Joined: Wed Jun 08, 2022 1:41 am

Re: Error: Save all prefabs in resources folder

Post by tuanha »

I followed and saved an array of 4000 objects located in the resources folder, when ES3 loaded it didn't refer to the objects in the resources folder. Then I right-clicked and selected Add references to managers, it solved, it can be referenced when loading into the list array.
I have a question, if using Add references to managers for a large number of objects, will it affect the device cache and game processing speed?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Error: Save all prefabs in resources folder

Post by Joel »

Hi there,

Using Add References to Manager simply puts a reference to that object in an array in your scene, so any affect on performance will be neglible.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tuanha
Posts: 12
Joined: Wed Jun 08, 2022 1:41 am

Re: Error: Save all prefabs in resources folder

Post by tuanha »

I have another question, how to remove an object's reference?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Error: Save all prefabs in resources folder

Post by Joel »

Hi there,

To do this, ensure there are no other references to the object in your scene (i.e. ensure it's not referenced by any scripts), and then press the Optimize button on the ES3ReferenceMgr Component of the Easy Save 3 Manager.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tuanha
Posts: 12
Joined: Wed Jun 08, 2022 1:41 am

Re: Error: Save all prefabs in resources folder

Post by tuanha »

Thanks for your help, here I see ES3 has many advantages
Post Reply