Creating persistent object in Edit mode

As the documentation for [EditorUtility.IsPersistent][1] says:

Returns false if the object lives in the scene. Typically this is a game object or component but it could also be a material that was created from code and not stored in an asset but instead stored in the scene.

That's right, I'm creating a material from code, inside an EditorWindow class. How to make it persist and save to disk?

  • [SerializableField] doesn't work.
  • Setting the variable to public doesn't work.
  • [EditorPrefs][2] can't help me, because it's only for value types.
  • Any other option?

To clarify, I want to retain the variable reference upon closing Unity3D and opening the project again, I don't even know if it's possible.


I just followed instructions found here, and created the material on disk using [AssetDatabase][4]. Although the materials effectively are stored on Assets, the reference itself is lost, so I have to load them everytime in my EditorWindow. To illustrate this point, I uploaded 2 screenshots:

In this, we can see that the materials are loaded, and IsPersistent returns true for all.


I restart Unity3D, and now the references are gone! But the materials are still in the Assets folder.


more ▼

asked Oct 18, 2011 at 01:52 PM

Veehmot gravatar image

899 42 44 54

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Ah, nevermind. I was needing [SerializableField] on the variables.

I understand a little better how it works: **EditorUtility.IsPersistent** tells me if my objects is going to be erased next time Unity3D restarts, because it isn't stored in disk, it isn't stored anywhere! I can't really make it persistent without creating an asset. I was just creating the Material on the fly, but I never told Unity3D where to save it. So I couldn't expect him to save it automatically in a random position.

Seems like the way to go is to create an asset, be it hidden using [HideFlags][2] or not. Afer we create an asset, now Unity3D will be able to save the object in disk. The reference itself will be lost, unless you make the reference public, or put [SerializableField] attribute above the variable.

My mistake was to think that [SerializableField] would work even if I didn't create an asset. I guess it doesn't!

If anyone can tell me if I'm right with this, I would appreciate it.

more ▼

answered Oct 18, 2011 at 04:07 PM

Veehmot gravatar image

899 42 44 54

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Oct 18, 2011 at 01:52 PM

Seen: 2856 times

Last Updated: Oct 19, 2011 at 07:45 PM