Skip to content

Commit 82e9d4d

Browse files
committed
feat: mode to manually add SoftMaskable components
close #253
1 parent fcebd4a commit 82e9d4d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

Packages/src/Runtime/SoftMask.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,9 @@ private void SetDirtyAndNotifyIfBufferSizeChanged()
528528

529529
private void AddSoftMaskableOnChildren()
530530
{
531-
if (!isActiveAndEnabled || !SoftMaskingEnabled()) return;
531+
if (!SoftMaskingEnabled()) return;
532532

533-
this.AddComponentOnChildren<SoftMaskable>(true);
533+
SoftMaskUtils.AddSoftMaskableOnChildren(this, true);
534534
}
535535

536536
private void OnBeforeCanvasRebuild()

Packages/src/Runtime/SoftMaskable.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public float power
128128
private void OnEnable()
129129
{
130130
UpdateHideFlags();
131-
this.AddComponentOnChildren<SoftMaskable>(false);
131+
SoftMaskUtils.AddSoftMaskableOnChildren(this, false);
132+
132133
_shouldRecalculateStencil = true;
133134
if (TryGetComponent(out _graphic))
134135
{
@@ -192,7 +193,7 @@ private void OnDestroy()
192193

193194
private void OnTransformChildrenChanged()
194195
{
195-
this.AddComponentOnChildren<SoftMaskable>(false);
196+
SoftMaskUtils.AddSoftMaskableOnChildren(this, false);
196197
}
197198

198199
private void OnTransformParentChanged()
@@ -280,6 +281,7 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
280281
}, baseMaterial);
281282
Profiler.EndSample();
282283
}
284+
283285
Profiler.EndSample();
284286

285287
Profiler.BeginSample("(SM4UI)[SoftMaskableMaterial] Create > Set Properties");

Packages/src/Runtime/UISoftMaskProjectSettings.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ namespace Coffee.UISoftMask
1212
{
1313
public class UISoftMaskProjectSettings : PreloadedProjectSettings<UISoftMaskProjectSettings>
1414
{
15+
public enum SoftMaskableTracker
16+
{
17+
Automatic,
18+
Manual
19+
}
20+
1521
private static bool s_UseStereoMock;
1622

1723
[Header("Setting")]
@@ -27,6 +33,13 @@ public class UISoftMaskProjectSettings : PreloadedProjectSettings<UISoftMaskProj
2733
[SerializeField]
2834
private TransformSensitivity m_TransformSensitivity = TransformSensitivity.Medium;
2935

36+
[Tooltip(
37+
"Determines how to add SoftMaskable component to supported components under SoftMask.\n" +
38+
"Automatic: Automatically add SoftMaskable component to supported components under SoftMask.\n" +
39+
"Manual: Add SoftMaskable component manually by yourself.")]
40+
[SerializeField]
41+
private SoftMaskableTracker m_SoftMaskable = SoftMaskableTracker.Automatic;
42+
3043
[Header("Editor")]
3144
[Tooltip("Hide the automatically generated components.\n" +
3245
" - MaskingShapeContainer\n" +
@@ -66,6 +79,14 @@ public static TransformSensitivity transformSensitivity
6679
set => instance.m_TransformSensitivity = value;
6780
}
6881

82+
public static bool addSoftMaskableAutomatically
83+
{
84+
get => instance.m_SoftMaskable == SoftMaskableTracker.Automatic;
85+
set => instance.m_SoftMaskable = value
86+
? SoftMaskableTracker.Automatic
87+
: SoftMaskableTracker.Manual;
88+
}
89+
6990
public static bool useStereoMock
7091
{
7192
get => s_UseStereoMock;

Packages/src/Runtime/Utilities/SoftMaskUtils.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,16 @@ private static Material GetSoftMaskingMaterial(ref Material mat, BlendOp op)
220220
#endif
221221
return mat;
222222
}
223+
224+
/// <summary>
225+
/// Adds SoftMaskable components to all children of the specified MonoBehaviour.
226+
/// </summary>
227+
public static void AddSoftMaskableOnChildren(MonoBehaviour self, bool includeSelf)
228+
{
229+
if (!self || !self.isActiveAndEnabled) return;
230+
if (!self.isActiveAndEnabled) return;
231+
232+
self.AddComponentOnChildren<SoftMaskable>(includeSelf);
233+
}
223234
}
224235
}

0 commit comments

Comments
 (0)