ViewData API를 사용하여 도메인이 다시 로드되거나 에디터가 다시 시작한 후에 UI별 상태 데이터가 지속되도록 만드십시오. 지속 데이터는 각 EditorWindow
에 저장됩니다. 각 VisualElement
에는 ViewData
지속성을 활성화하기 위해 설정해야 하는 persistenceKey
가 들어 있습니다.
여기에서는 구현된 컨트롤과 신규 오브젝트를 위해 ViewData 지속성을 활성화하는 방법의 예를 보여드립니다.
요소가 이미 지속성을 지원하는 경우 persistenceKey
를 설정하여 시스템에 데이터 저장이 필요함을 알리십시오. persistenceKey
는 다른 시각적 요소에 사용되는 키와 달라야 합니다.
새로운 VisualElement
를 생성하는 경우 지속 데이터를 지원하도록 만들 수 있습니다. 첫 번째 단계는 지속 데이터를 요소 클래스 내에서 하나 이상의 Serializable
클래스 안에 캡슐화하는 것입니다.
[Serializable]
public class ExtraData
{
public int m_Value = 0;
}
public ExtraData m_ExtraData;
두 번째 단계는 지속 데이터가 변할 때마다 SavePersistentData()
메서드를 호출하는 것입니다. 이는 데이터가 올바르게 저장되도록 만들기 위함입니다.
public int value
{
get { return m_Value; }
set
{
// do stuff
SavePersistentData();
}
}
마지막 단계는 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);
}
위 예제는 고유한 키를 생성하고 할당하는 방법을 보여줍니다. 키가 할당되면 GetOrCreatePersistentData()
가 지속성 상태인 오브젝트 또는 있는 그대로의 오브젝트를 반환합니다.
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?
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:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.