Use the ViewData API to persist UI-specific state data after a domain reloads or when the Editor restarts. Persistent data is stored on each EditorWindow
. Each VisualElement
has a persistenceKey
that must be set in order to enable ViewData
persistence.
This topic provides examples on how to enable ViewData persistence for implemented controls and new objects.
If an element already supports persistence, set the persistenceKey
to tell the system that it needs to save data. The persistenceKey
must be unique compared against the keys for other visual elements.
If you create a new VisualElement
, you can have it support persistent data. The first step is to encapsulate your persistent data within one or more Serializable
classes inside your element class:
[Serializable]
public class ExtraData
{
public int m_Value = 0;
}
public ExtraData m_ExtraData;
The second step is to call the SavePersistentData()
method whenever the persistent data changes. This ensures that the data is saved properly:
public int value
{
get { return m_Value; }
set
{
// do stuff
SavePersistentData();
}
}
The last step is to override OnPersistentDataReady()
:
// We do our real initial work here, once we know we can access our
// persistent data store.
protected override void OnPersistentDataReady()
{
base.OnPersistentDataReady();
// Optionally get a more unique key based on current parents.
// This includes our own `persistenceKey`.
var key = GetFullHierarchicalPersistenceKey();
// Get or create a new ExtraData objects.
m_ExtraData = GetOrCreatePersistentData<ExtraData>(m_ExtraData, key);
}
The example above also demonstrates how to generate and assign a unique key. Once the key is assigned, GetOrCreatePersistentData()
returns the object with the persistence state, or as-is.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!