Skip to content

Commit 61d483b

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

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

Packages/src/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ tutorial.
375375

376376
### Project Settings
377377

378-
![](https://github.com/user-attachments/assets/5e27df42-f117-421c-adc5-f6e53154ee4c)
378+
![](https://github.com/mob-sakai/mob-sakai/releases/download/docs/1759018902322.png)
379379

380380
You can adjust the project-wide settings for SoftMaskForUGUI. (`Edit > Project Settings > UI > Soft Mask`)
381381

@@ -384,6 +384,9 @@ You can adjust the project-wide settings for SoftMaskForUGUI. (`Edit > Project S
384384
- **Transform Sensitivity**: `Low`, `Medium`, `High`
385385
- Adjust the transformation sensitivity for the soft mask buffer update.
386386
- The higher the sensitivity, the more frequently the soft mask buffer is updated.
387+
- **Soft Maskable**: Determines how to add `SoftMaskable` components under `SoftMask`.
388+
- `Automatic`: `SoftMaskable` components are added automatically as needed.
389+
- `Manual`: You need to add `SoftMaskable` components explicitly.
387390
- **Hide Generated Component**: Automatically hide the generated `MaskingShapeContainer` and `TerminalMaskingShape`
388391
components.
389392
- **Optional Shaders (SoftMaskable)**: A list of shaders that will be prioritized when a soft-maskable shader is

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: 20 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,12 @@ public class UISoftMaskProjectSettings : PreloadedProjectSettings<UISoftMaskProj
2733
[SerializeField]
2834
private TransformSensitivity m_TransformSensitivity = TransformSensitivity.Medium;
2935

36+
[Tooltip("Determines how to add SoftMaskable components under SoftMask.\n" +
37+
"Automatic: SoftMaskable components are added automatically as needed.\n" +
38+
"Manual: You need to add SoftMaskable components explicitly.")]
39+
[SerializeField]
40+
private SoftMaskableTracker m_SoftMaskable = SoftMaskableTracker.Automatic;
41+
3042
[Header("Editor")]
3143
[Tooltip("Hide the automatically generated components.\n" +
3244
" - MaskingShapeContainer\n" +
@@ -66,6 +78,14 @@ public static TransformSensitivity transformSensitivity
6678
set => instance.m_TransformSensitivity = value;
6779
}
6880

81+
public static bool addSoftMaskableAutomatically
82+
{
83+
get => instance.m_SoftMaskable == SoftMaskableTracker.Automatic;
84+
set => instance.m_SoftMaskable = value
85+
? SoftMaskableTracker.Automatic
86+
: SoftMaskableTracker.Manual;
87+
}
88+
6989
public static bool useStereoMock
7090
{
7191
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)