-
-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathUISoftMaskProjectSettings.cs
More file actions
257 lines (221 loc) · 9.92 KB
/
UISoftMaskProjectSettings.cs
File metadata and controls
257 lines (221 loc) · 9.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma warning disable CS0414
using System.Linq;
using Coffee.UISoftMaskInternal;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_MODULE_VR
using UnityEngine.XR;
#endif
namespace Coffee.UISoftMask
{
public class UISoftMaskProjectSettings : PreloadedProjectSettings<UISoftMaskProjectSettings>
{
public enum SoftMaskableTracker
{
Automatic,
Manual
}
private static bool s_UseStereoMock;
[Header("Setting")]
[Tooltip("Enable SoftMask globally.")]
[SerializeField]
internal bool m_SoftMaskEnabled = true;
[Tooltip("Enable stereo rendering for VR devices.")]
[SerializeField]
private bool m_StereoEnabled = true;
[Tooltip("Sensitivity of transform that automatically rebuilds the soft mask buffer.")]
[SerializeField]
private TransformSensitivity m_TransformSensitivity = TransformSensitivity.Medium;
[Tooltip("Determines how to add SoftMaskable components under SoftMask.\n" +
"Automatic: SoftMaskable components are added automatically as needed.\n" +
"Manual: You need to add SoftMaskable components explicitly.")]
[SerializeField]
private SoftMaskableTracker m_SoftMaskable = SoftMaskableTracker.Automatic;
[Header("Editor")]
[Tooltip("Hide the automatically generated components.\n" +
" - MaskingShapeContainer\n" +
" - TerminalMaskingShape")]
[SerializeField]
private bool m_HideGeneratedComponents = true;
[HideInInspector]
[SerializeField]
private ShaderVariantRegistry m_ShaderVariantRegistry = new ShaderVariantRegistry();
public static ShaderVariantRegistry shaderRegistry => instance.m_ShaderVariantRegistry;
public static ShaderVariantCollection shaderVariantCollection => shaderRegistry.shaderVariantCollection;
public static bool softMaskEnabled => instance.m_SoftMaskEnabled;
public static bool useStencilOutsideScreen => true;
#if UNITY_MODULE_VR
public static bool stereoEnabled =>
#if UNITY_EDITOR
Application.isPlaying &&
#endif
softMaskEnabled && instance.m_StereoEnabled && XRSettings.enabled;
#else
public static bool stereoEnabled => false;
#endif
public static HideFlags hideFlagsForTemp => instance.m_HideGeneratedComponents
? HideFlags.DontSaveInEditor | HideFlags.NotEditable | HideFlags.HideInHierarchy | HideFlags.HideInInspector
: HideFlags.DontSaveInEditor | HideFlags.NotEditable;
public static TransformSensitivity transformSensitivity
{
get => instance.m_TransformSensitivity;
set => instance.m_TransformSensitivity = value;
}
public static bool addSoftMaskableAutomatically
{
get => instance.m_SoftMaskable == SoftMaskableTracker.Automatic;
set => instance.m_SoftMaskable = value
? SoftMaskableTracker.Automatic
: SoftMaskableTracker.Manual;
}
public static bool useStereoMock
{
get => s_UseStereoMock;
set
{
if (s_UseStereoMock == value) return;
s_UseStereoMock = value;
ResetAllSoftMasks();
}
}
private static void ResetAllSoftMasks()
{
var softMasks = InternalListPool<SoftMask>.Rent();
var components = InternalListPool<IMaskable>.Rent();
foreach (var softMask in Misc.FindObjectsOfType<SoftMask>())
{
// #208: Accessing game object transform hierarchy before loading of scene has completed.
if (!softMask.gameObject.scene.isLoaded) continue;
softMask.GetComponentsInParent(true, softMasks);
if (1 < softMasks.Count) continue;
softMask.GetComponentsInChildren(true, components);
components.ForEach(c => c.RecalculateMasking());
}
InternalListPool<IMaskable>.Return(ref components);
InternalListPool<SoftMask>.Return(ref softMasks);
}
#if !UNITY_EDITOR
protected override void OnEnable()
{
base.OnEnable();
m_ShaderVariantRegistry.InitializeShaderLookup();
}
#endif
#if UNITY_EDITOR
[InitializeOnLoadMethod]
private static void InitializeOnLoadMethod()
{
#if !LEGACY_TMP_ENABLE
const string tmpSupport = "TextMeshPro Support (Unity 6)";
const string version = "v3.3.0 (Unity 6)";
#else
const string tmpSupport = "TextMeshPro Support";
const string version = "v3.3.0";
#endif
ShaderSampleImporter.RegisterShaderSamples(new[]
{
// TextMeshPro Support/TextMeshPro Support (Unity 6)
("Hidden/TextMeshPro/Distance Field SSD (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Mobile/Distance Field SSD (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Distance Field Overlay (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Mobile/Distance Field Overlay (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Bitmap (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Mobile/Bitmap (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Distance Field (SoftMaskable)", tmpSupport, version),
("Hidden/TextMeshPro/Mobile/Distance Field (SoftMaskable)", tmpSupport, version),
// Spine Support
("Hidden/Spine/SkeletonGraphic (SoftMaskable)", "Spine Support", "v3.3.0"),
("Hidden/Spine/SkeletonGraphic Fill (SoftMaskable)", "Spine Support", "v3.3.0"),
("Hidden/Spine/SkeletonGraphic Grayscale (SoftMaskable)", "Spine Support", "v3.3.0"),
("Hidden/Spine/SkeletonGraphic Multiply (SoftMaskable)", "Spine Support", "v3.3.0"),
("Hidden/Spine/SkeletonGraphic Screen (SoftMaskable)", "Spine Support", "v3.3.0")
});
ShaderSampleImporter.RegisterDeprecatedShaders(new[]
{
("e65241fa80a374114b3f55ed746c04d9", "Hidden-TMP_SDF-Mobile-SoftMaskable-Unity6.shader"),
("935b7be1c88464d2eb87204fdfab5a38", "Hidden-TMP_SDF-SoftMaskable-Unity6.shader")
});
EditorApplication.update += ShaderSampleImporter.Update;
CgincPathSync.RegisterShaders("/(TMPro|TMPro_Properties).cginc", new[]
{
"Hidden/TextMeshPro/Distance Field (SoftMaskable)",
"Hidden/TextMeshPro/Mobile/Distance Field (SoftMaskable)",
"Hidden/TextMeshPro/Distance Field SSD (SoftMaskable)",
"Hidden/TextMeshPro/Mobile/Distance Field SSD (SoftMaskable)",
"Hidden/TextMeshPro/Distance Field Overlay (SoftMaskable)",
"Hidden/TextMeshPro/Mobile/Distance Field Overlay (SoftMaskable)",
"Hidden/TextMeshPro/Bitmap (SoftMaskable)",
"Hidden/TextMeshPro/Mobile/Bitmap (SoftMaskable)",
"Hidden/TextMeshPro/Distance Field (SoftMaskable)",
"Hidden/TextMeshPro/Mobile/Distance Field (SoftMaskable)"
});
}
protected override void OnEnable()
{
base.OnEnable();
m_ShaderVariantRegistry.InitializeShaderLookup();
m_ShaderVariantRegistry.onShaderRequested = ShaderSampleImporter.ImportShaderIfSelected;
m_ShaderVariantRegistry.ClearCache();
}
protected override void OnCreateAsset()
{
m_ShaderVariantRegistry.InitializeIfNeeded(this);
m_ShaderVariantRegistry.RegisterOptionalShaders(this);
}
protected override void OnInitialize()
{
m_ShaderVariantRegistry.InitializeIfNeeded(this);
}
private void Reset()
{
m_ShaderVariantRegistry.InitializeIfNeeded(this);
m_ShaderVariantRegistry.RegisterOptionalShaders(this);
}
private void OnValidate()
{
ResetAllSoftMasks();
ResetAllHideFlags<MaskingShapeContainer>(hideFlagsForTemp);
ResetAllHideFlags<TerminalMaskingShape>(hideFlagsForTemp);
}
private static void ResetAllHideFlags<T>(HideFlags flags) where T : Component
{
foreach (var component in Misc.FindObjectsOfType<T>())
{
component.hideFlags = flags;
EditorUtility.SetDirty(component);
}
}
[SettingsProvider]
private static SettingsProvider CreateSettingsProvider()
{
return new PreloadedProjectSettingsProvider("Project/UI/Soft Mask");
}
private class Postprocessor : AssetPostprocessor
{
private static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[] ___, string[] ____)
{
if (Misc.isBatchOrBuilding) return;
// Register optional shaders when shaders are imported.
if (imported.Concat(deleted).Any(path => path.EndsWith(".shader")))
{
MaterialRepository.Clear();
// Refresh
if (hasInstance)
{
shaderRegistry.ClearCache();
shaderRegistry.RegisterOptionalShaders(instance);
}
// Refresh all UIEffect instances.
foreach (var c in Misc.FindObjectsOfType<SoftMaskable>()
.Concat(Misc.GetAllComponentsInPrefabStage<SoftMaskable>()))
{
c.ReleaseMaterial();
c.SetMaterialDirty();
}
}
}
}
#endif
}
}