Page 1 of 1

Saving to Resources from Editor

Posted: Sat Apr 19, 2014 11:20 am
by Joel
Question from the Unity Forums:
Sorry for bumping this but I have a question regarding saving a file to Resources folder.

Currently it only works if you run the game inside the editor. Is it possible to run ES2.Save to the Resources folder without clicking the Play button? I'm working on an editor window that process files, encrypt them and save them into Resources folder. It isn't convenient having to press play in order to do that.
Answer:

You can use Easy Save in Editor scripts in the same way you can use it from Game scripts. So you can write your save code in an Editor script and run that, or even call the save code in your game scripts from an editor script.

All the best,
Joel

Re: Saving to Resources from Editor

Posted: Tue Oct 07, 2014 2:29 am
by ly61
hi,
sry for bumping this again as I don't think this is working? when I try to save using editor window, an error message pops out saying "Destroy may not be called from edit mode! Use DestroyImmediate instead." and the function aborted. Am I doing anything wrong?
Thanks!

Re: Saving to Resources from Editor

Posted: Tue Oct 07, 2014 9:35 am
by Joel
Hi there,

This is very unusual because as far as I'm aware, Easy Save never calls Destroy.

Could you post the code you are using?

All the best,
Joel

Re: Saving to Resources from Editor

Posted: Tue Oct 07, 2014 12:48 pm
by ly61
strange... my code is as below. this function is called by an OnEnable function of an EditorWindow

Code: Select all

	public void RefreshWindow()
	{
		Debug.Log("RefreshWindow called for ItemGenreEditor window.");

		string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

		if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
			path = path+ "/GameProjData/Item/GenreList.txt";
		else if (Application.platform == RuntimePlatform.WindowsPlayer)
			path = path + @"\GameProjData\Item\GenreList.txt";
		else
			Debug.Log("Error: unexpected platform.");

		string pathWithTag = path + "?tag=allGenreNames";

		if (ES2.Exists(pathWithTag))
		{
			Debug.Log("file and tag exist.");
			allGenres = ES2.LoadList<string>(pathWithTag);
		}
		else
		{
			Debug.Log("Error: file found but no data in it. creating one.");
			ES2.Save(new List<string>(), path);
		}

	}
all problem is pointing to this line:
allGenres = ES2.LoadList<string>(pathWithTag); (allGenres is a List<string> declared outside of the function.)

if I don't click play, error message would be ""Destroy may not be called from edit mode! Use DestroyImmediate instead.". if I do press play, a different error message would say "EndOfStreamException: Failed to read past end of stream.".

Thanks for help!

Re: Saving to Resources from Editor

Posted: Tue Oct 07, 2014 11:25 pm
by ly61
ah sorry its my other script messed it up. its all been solve now.
Thanks!

edit:

actually, after some more testing I was able to recreate the problem: if I open Unity and refresh the editor window (using the script i posted above) before pressing the play button, the error message about "destroy" would pop up. but once I press the play button, even if I press it again to stop playing, there won't be any errors using ES.Load functions.

Re: Saving to Resources from Editor

Posted: Tue Nov 11, 2014 10:22 pm
by Jason
Hey there,

I'm also getting this error:

Destroy may not be called from edit mode! Use DestroyImmediate instead.
Also think twice if you really want to destroy something in edit mode. Since this will destroy objects permanently.
UnityEngine.Object:Destroy(Object)
ES2TypeManager:InitializeTypeList()
ES2TypeManager:GetES2Type(Type)
ES2Writer:Write(List`1, String)
BlockManager:WriteData() (at Assets/Editor/Code/BlockManager.cs:241)
BlockManager:AddBlock() (at Assets/Editor/Code/BlockManager.cs:179)
BlockManager:OnGUI() (at Assets/Editor/Code/BlockManager.cs:134)
UnityEditor.DockArea:OnGUI()


And it is fixed when I hit play and then exit play mode, but it's still an inconvenience.

As a note, when this error occurs it creates an ES2Init(Clone) object in my hierarchy.

Re: Saving to Resources from Editor

Posted: Wed Nov 12, 2014 9:58 am
by Joel
Hi Jason,

This will be fixed in v2.53, which we'll be submitting as soon as Unity approves v2.52.

All the best,
Joel

Re: Saving to Resources from Editor

Posted: Sat Nov 15, 2014 6:40 am
by Jason
I've now installed v2.5.3. The old error is gone, but it's been replaced with a new one when attempting to read data using ES2Reader:

NullReferenceException: Object reference not set to an instance of an object
ES2TypeManager.GetES2Type (Byte key)
ES2Reader.ReadAll ()
ES2.LoadAll (System.String path)
BlockManager.ReadData () (at Assets/Editor/Code/BlockManager.cs:436)
BlockManager.BeginEdit () (at Assets/Editor/Code/BlockManager.cs:258)
BlockManager.OnGUI () (at Assets/Editor/Code/BlockManager.cs:188)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

Re: Saving to Resources from Editor

Posted: Sat Nov 15, 2014 1:18 pm
by Joel
Hi again Jason,

Would you be able to send me the code you're using? For some reason I'm not getting the same error at my end.

Thanks,
Joel

Re: Saving to Resources from Editor

Posted: Mon Nov 17, 2014 6:50 pm
by Joel
Hi again Jason,

Had another bash at this and I've managed to replicate your error. Looks like the initialisation isn't getting called in Editor under certain circumstances, so it's necessary to initialise it yourself before calling ES2 methods.

Currently the easiest way to do this is to call this before using ES2 in the Editor:
GameObject init = new GameObject();
init.AddComponent<ES2Init>().Awake();
DestroyImmediate(init);
But I'll change it so that in v2.54 you'll simply be able to do:
ES2Init.Init();
All the best,
Joel