-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathDefaultVolumeProfileSettingsPropertyDrawer.cs
More file actions
159 lines (137 loc) · 7.63 KB
/
DefaultVolumeProfileSettingsPropertyDrawer.cs
File metadata and controls
159 lines (137 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.Rendering
{
/// <summary>
/// Base implementation for drawing Default Volume Profile UI in Graphics Settings.
/// </summary>
public abstract partial class DefaultVolumeProfileSettingsPropertyDrawer : PropertyDrawer
{
// UUM-77758: Due to how PropertyDrawers are created and cached, there is no way to retrieve them reliably
// later. We know that only one DefaultVolumeProfile exists at any given time, so we can access it through
// static variables.
static SerializedProperty s_DefaultVolumeProfileSerializedProperty;
static DefaultVolumeProfileEditor s_DefaultVolumeProfileEditor;
VisualElement m_Root;
/// <summary>SerializedObject representing the settings object</summary>
protected SerializedObject m_SettingsSerializedObject;
/// <summary>SerializedProperty representing the Default Volume Profile</summary>
protected SerializedProperty m_VolumeProfileSerializedProperty;
/// <summary>Foldout state</summary>
protected EditorPrefBool m_DefaultVolumeProfileFoldoutExpanded;
/// <summary>VisualElement containing the DefaultVolumeProfileEditor</summary>
protected VisualElement m_EditorContainer;
/// <summary>Default Volume Profile label width</summary>
protected const int k_DefaultVolumeLabelWidth = 260;
/// <summary>Info box message</summary>
protected abstract GUIContent volumeInfoBoxLabel { get; }
/// <summary>
/// CreatePropertyGUI implementation.
/// </summary>
/// <param name="property">Property to create UI for</param>
/// <returns>VisualElement containing the created UI</returns>
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
m_Root = new VisualElement();
var header = CreateHeader();
if (header != null)
m_Root.Add(header);
m_SettingsSerializedObject = property.serializedObject;
m_VolumeProfileSerializedProperty = property.FindPropertyRelative("m_VolumeProfile");
m_DefaultVolumeProfileFoldoutExpanded = new EditorPrefBool($"{GetType()}.DefaultVolumeProfileFoldoutExpanded", true);
m_EditorContainer = new VisualElement();
if (!RenderPipelineManager.pipelineSwitchCompleted)
// Defer creation of the UI until the render pipeline is created and VolumeManager is initialized
RenderPipelineManager.activeRenderPipelineCreated += CreateDefaultVolumeProfileEditor;
else
CreateDefaultVolumeProfileEditor();
m_Root.Add(CreateAssetFieldUI());
m_Root.Add(m_EditorContainer);
return m_Root;
}
/// <summary>
/// Creates the header for the Volume Profile editor.
/// </summary>
/// <returns>VisualElement containing the header. Null for no header.</returns>
protected virtual VisualElement CreateHeader() => null;
/// <summary>
/// Creates the Default Volume Profile editor.
/// </summary>
protected void CreateDefaultVolumeProfileEditor()
{
RenderPipelineManager.activeRenderPipelineCreated -= CreateDefaultVolumeProfileEditor;
VolumeProfile profile = m_VolumeProfileSerializedProperty.objectReferenceValue as VolumeProfile;
if (profile == null)
return;
if (profile == VolumeManager.instance.globalDefaultProfile)
VolumeProfileUtils.EnsureAllOverridesForDefaultProfile(profile);
if (s_DefaultVolumeProfileSerializedProperty != m_VolumeProfileSerializedProperty)
{
s_DefaultVolumeProfileSerializedProperty = m_VolumeProfileSerializedProperty;
s_DefaultVolumeProfileEditor = new DefaultVolumeProfileEditor(profile, m_SettingsSerializedObject);
}
m_EditorContainer.Add(s_DefaultVolumeProfileEditor.Create());
m_EditorContainer.Q<HelpBox>("volume-override-info-box").text = volumeInfoBoxLabel.text;
if (m_DefaultVolumeProfileFoldoutExpanded.value)
m_EditorContainer.style.display = DisplayStyle.Flex;
}
/// <summary>
/// Destroys the Default Volume Profile editor.
/// </summary>
protected void DestroyDefaultVolumeProfileEditor()
{
m_EditorContainer.style.display = DisplayStyle.None;
m_EditorContainer?.Clear();
if (s_DefaultVolumeProfileEditor != null)
s_DefaultVolumeProfileEditor.Destroy();
s_DefaultVolumeProfileEditor = null;
s_DefaultVolumeProfileSerializedProperty = null;
}
/// <summary>
/// Implementation of the Default Volume Profile asset field.
/// </summary>
/// <returns>VisualElement containing the UI</returns>
protected abstract VisualElement CreateAssetFieldUI();
/// <summary>
/// Context menu implementation for Default Volume Profile.
/// </summary>
/// <typeparam name="TSetting">Default Volume Profile Settings type</typeparam>
/// <typeparam name="TRenderPipeline">Render Pipeline type</typeparam>
public abstract class DefaultVolumeProfileSettingsContextMenu2<TSetting, TRenderPipeline> : IRenderPipelineGraphicsSettingsContextMenu2<TSetting>
where TSetting : class, IDefaultVolumeProfileSettings
where TRenderPipeline : RenderPipeline
{
/// <summary>
/// Path where new Default Volume Profile will be created.
/// </summary>
protected abstract string defaultVolumeProfilePath { get; }
void IRenderPipelineGraphicsSettingsContextMenu2<TSetting>.PopulateContextMenu(TSetting setting, SerializedProperty _, ref GenericMenu menu)
{
bool canCreateNewAsset = RenderPipelineManager.currentPipeline is TRenderPipeline;
VolumeProfileUtils.AddVolumeProfileContextMenuItems(ref menu,
setting.volumeProfile,
s_DefaultVolumeProfileEditor == null ? null : s_DefaultVolumeProfileEditor.allEditors,
overrideStateOnReset: true,
defaultVolumeProfilePath: defaultVolumeProfilePath,
onNewVolumeProfileCreated: createdProfile =>
{
s_DefaultVolumeProfileSerializedProperty.objectReferenceValue = createdProfile;
s_DefaultVolumeProfileSerializedProperty.serializedObject.ApplyModifiedProperties();
VolumeProfile initialAsset = null;
var initialAssetSettings = EditorGraphicsSettings.GetRenderPipelineSettingsFromInterface<IDefaultVolumeProfileAsset>();
if (initialAssetSettings.Length > 0)
{
if (initialAssetSettings.Length > 1)
throw new InvalidOperationException("Found multiple settings implementing IDefaultVolumeProfileAsset, expected only one");
initialAsset = initialAssetSettings[0].defaultVolumeProfile;
}
VolumeProfileUtils.UpdateGlobalDefaultVolumeProfile<TRenderPipeline>(createdProfile, initialAsset);
},
onComponentEditorsExpandedCollapsed: s_DefaultVolumeProfileEditor == null ? null : s_DefaultVolumeProfileEditor.RebuildListViews,
canCreateNewAsset);
}
}
}
}