ViewData API を使用すると、ドメインの再読み込み後、またはエディターの再起動時に UI 固有の状態データを保持できます。永続的なデータは、各 EditorWindow
に格納されます。それぞれの VisualElement
には persistenceKey
があり、ViewData
の永続性を可能にするためにはこれを設定する必要があります。
ここでは、実装されたコントロールと新しいオブジェクトの ViewData 永続性を有効にする方法を説明します。
要素がすでに永続性をサポートしている場合は、persistenceKey
を設定して、データを保存する必要があることをシステムに指示します。persistenceKey
は他のビジュアル要素のキーと違い、一意である必要があります。
新しい VisualElement
を作成すると、永続データをサポートすることができます。最初の手順は、要素クラスの 1 つ以上の Serializable
クラス内で永続データをカプセル化することです。
[Serializable]
public class ExtraData
{
public int m_Value = 0;
}
public ExtraData m_ExtraData;
2 番目の手順は、永続データが変更されるたびに SavePersistentData()
メソッドを呼び出すことです。これにより、データが正しく保存されます。
public int value
{
get { return m_Value; }
set
{
// ここに行うことを記述
SavePersistentData();
}
}
最後の手順は OnPersistentDataReady()
をオーバーライドすることです。
// 永続データ保存場所にアクセスできることを確認したら、
// 実際の最初の作業をここに記述します
protected override void OnPersistentDataReady()
{
base.OnPersistentDataReady();
// 必須ではないが、現在の親に基づいて一意のキーを取得します
// これは、一意の `persistenceKey` を含みます
var key = GetFullHierarchicalPersistenceKey();
// 新しい ExtraData オブジェクトを取得、または作成します
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.