-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathLookDevVolumeProfileSettingsPropertyDrawer.cs
More file actions
155 lines (131 loc) · 6.92 KB
/
LookDevVolumeProfileSettingsPropertyDrawer.cs
File metadata and controls
155 lines (131 loc) · 6.92 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
using System;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.UIElements;
namespace UnityEditor.Rendering.HighDefinition
{
[CustomPropertyDrawer(typeof(LookDevVolumeProfileSettings))]
[SupportedOnRenderPipeline(typeof(HDRenderPipelineAsset))]
class LookDevVolumeProfileSettingsPropertyDrawer : PropertyDrawer
{
VisualElement m_Root;
Editor m_LookDevVolumeProfileEditor;
int m_LookDevVolumeProfileHash = -1;
SerializedObject m_SettingsSerializedObject;
SerializedProperty m_VolumeProfileSerializedProperty;
EditorPrefBool m_DefaultVolumeProfileFoldoutExpanded;
VisualElement m_EditorContainer;
const int k_DefaultVolumeLabelWidth = 260;
static readonly GUIContent k_LookDevVolumeProfileAssetLabel = EditorGUIUtility.TrTextContent("LookDev Profile");
const int k_ImguiContainerLeftMargin = 32;
const int k_AssetFieldBottomMargin = 6;
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
m_Root = new VisualElement();
var label = new Label("LookDev");
label.style.unityFontStyleAndWeight = FontStyle.Bold;
label.style.paddingTop = 6;
m_Root.Add(label);
m_SettingsSerializedObject = property.serializedObject;
m_VolumeProfileSerializedProperty = property.FindPropertyRelative("m_VolumeProfile");
m_DefaultVolumeProfileFoldoutExpanded = new EditorPrefBool($"{GetType()}.LookDevVolumeProfileFoldoutExpanded", false);
m_EditorContainer = new VisualElement();
var assetFieldUI = CreateAssetFieldUI();
assetFieldUI.style.marginBottom = k_AssetFieldBottomMargin;
m_Root.Add(assetFieldUI);
m_EditorContainer.Add(CreateVolumeProfileEditorUI());
m_EditorContainer.style.marginLeft = -k_ImguiContainerLeftMargin;
m_Root.Add(m_EditorContainer);
return m_Root;
}
Editor GetLookDevDefaultVolumeProfileEditor(VolumeProfile lookDevAsset)
{
int currentHash = (lookDevAsset != null) ? lookDevAsset.GetHashCode() : -1;
if (currentHash != m_LookDevVolumeProfileHash)
{
Editor.DestroyImmediate(m_LookDevVolumeProfileEditor);
m_LookDevVolumeProfileEditor = null;
m_LookDevVolumeProfileHash = currentHash;
}
Editor.CreateCachedEditor(lookDevAsset, typeof(VolumeProfileEditor), ref m_LookDevVolumeProfileEditor);
return m_LookDevVolumeProfileEditor;
}
VisualElement CreateAssetFieldUI()
{
var lookDevVolumeProfileSettings = GraphicsSettings.GetRenderPipelineSettings<LookDevVolumeProfileSettings>();
VisualElement profileLine = new();
var toggle = new Toggle();
toggle.AddToClassList(Foldout.toggleUssClassName);
var checkmark = toggle.Q(className: Toggle.checkmarkUssClassName);
checkmark.AddToClassList(Foldout.checkmarkUssClassName);
var field = new ObjectField(k_LookDevVolumeProfileAssetLabel.text)
{
tooltip = k_LookDevVolumeProfileAssetLabel.tooltip,
objectType = typeof(VolumeProfile),
style =
{
flexShrink = 1,
}
};
field.BindProperty(m_VolumeProfileSerializedProperty);
field.AddToClassList("unity-base-field__aligned"); //Align with other BaseField<T>
field.Q<Label>().RegisterCallback<ClickEvent>(evt => toggle.value ^= true);
toggle.RegisterValueChangedCallback(evt =>
{
m_EditorContainer.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None;
m_DefaultVolumeProfileFoldoutExpanded.value = evt.newValue;
});
toggle.SetValueWithoutNotify(m_DefaultVolumeProfileFoldoutExpanded.value);
m_EditorContainer.style.display = m_DefaultVolumeProfileFoldoutExpanded.value ? DisplayStyle.Flex : DisplayStyle.None;
profileLine.Add(toggle);
profileLine.Add(field);
profileLine.style.flexDirection = FlexDirection.Row;
field.style.flexGrow = 1;
field.RegisterValueChangedCallback(evt =>
{
if (evt.newValue == evt.previousValue)
return;
if (GraphicsSettings.TryGetRenderPipelineSettings<LookDevVolumeProfileSettings>(out var lookDevVolumeProfileSettings))
{
lookDevVolumeProfileSettings.volumeProfile = evt.newValue as VolumeProfile;
m_VolumeProfileSerializedProperty.objectReferenceValue = lookDevVolumeProfileSettings.volumeProfile;
m_VolumeProfileSerializedProperty.serializedObject.ApplyModifiedProperties();
m_SettingsSerializedObject.Update();
}
m_LookDevVolumeProfileHash = -1; // Invalidate the hash, to allow the IMGUI container recreate the editor
});
return profileLine;
}
static Lazy<GUIStyle> s_ImguiContainerScopeStyle = new(() => new GUIStyle
{
padding = new RectOffset(k_ImguiContainerLeftMargin, 0, 0, 0)
});
VisualElement CreateVolumeProfileEditorUI()
{
return new IMGUIContainer(() =>
{
using var imguiContainerScope = new EditorGUILayout.VerticalScope(s_ImguiContainerScopeStyle.Value);
using var indentLevelScope = new EditorGUI.IndentLevelScope();
var lookDevAsset = m_VolumeProfileSerializedProperty.objectReferenceValue as VolumeProfile;
if (lookDevAsset != null && m_DefaultVolumeProfileFoldoutExpanded.value)
{
var editor = GetLookDevDefaultVolumeProfileEditor(lookDevAsset) as VolumeProfileEditor;
bool oldEnabled = GUI.enabled;
GUI.enabled = AssetDatabase.IsOpenForEdit(lookDevAsset);
editor.OnInspectorGUI();
GUI.enabled = oldEnabled;
if (lookDevAsset.Has<VisualEnvironment>())
EditorGUILayout.HelpBox("VisualEnvironment is not modifiable and will be overridden by the LookDev", MessageType.Warning);
if (lookDevAsset.Has<HDRISky>())
EditorGUILayout.HelpBox("HDRISky is not modifiable and will be overridden by the LookDev", MessageType.Warning);
}
else
{
EditorGUILayout.HelpBox("No Look Dev Volume Profile assigned. A default profile will be created automatically when you open the Look Dev tool (Window > Rendering > Look Dev).", MessageType.Info);
}
});
}
}
}