Skip to content

Commit

Permalink
feat: revive SoftMask.alpha
Browse files Browse the repository at this point in the history
`SoftMask.alpha` is obsolete because it can be replaced by `Graphic.color.a`.
Run `ProjectSettings > UI > SoftMask > Upgrade All Assets V1 to V2` to change this.
close #172
  • Loading branch information
mob-sakai committed Jul 21, 2024
1 parent fd34abe commit 8080cb2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ csharp_style_var_elsewhere = true:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

#
resharper_keep_existing_embedded_arrangement = true

# Arguments
csharp_arguments_literal = named:suggestion
csharp_arguments_string_literal = named:suggestion
Expand Down Expand Up @@ -135,7 +138,7 @@ dotnet_naming_style._camel_case.capitalization = camel_case

# Code style defaults
dotnet_sort_system_directives_first = true
csharp_preserve_single_line_statements = false:none
csharp_preserve_single_line_statements = false
csharp_prefer_static_local_function = true:suggestion
csharp_prefer_simple_using_statement = false:none
csharp_style_prefer_switch_expression = true:suggestion
Expand Down Expand Up @@ -187,7 +190,7 @@ csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = do_not_ignore
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Coffee.UISoftMaskInternal.AssetModification;
using UnityEditor;
using UnityEngine;

#pragma warning disable CS0612, CS0618 // Type or member is obsolete

namespace Coffee.UISoftMask
{
internal class SoftMaskComponentModifier_Alpha : ComponentModifier<SoftMask>
{
protected override bool ModifyComponent(SoftMask softMask, bool dryRun)
{
if (softMask.m_Alpha < 0 || 1 <= softMask.m_Alpha) return false;

if (!dryRun)
{
softMask.alpha = softMask.m_Alpha;
softMask.m_Alpha = -1;

EditorUtility.SetDirty(softMask.gameObject);
}

return true;
}

public override string Report()
{
return " -> SoftMask.alpha API has been changed. " +
"Use CanvasGroup component and CanvasGroup.alpha instead.\n";
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public UISoftMaskModifierRunner()
componentModifiers = new IComponentModifier[]
{
new SoftMaskComponentModifier_Softness(),
new SoftMaskComponentModifier_Alpha(),
new SoftMaskComponentModifier_PartOfParent(),
new SoftMaskableComponentModifier()
}
Expand All @@ -25,6 +26,7 @@ public UISoftMaskModifierRunner()
componentModifiers = new IComponentModifier[]
{
new SoftMaskComponentModifier_Softness(),
new SoftMaskComponentModifier_Alpha(),
new SoftMaskComponentModifier_PartOfParent(),
new SoftMaskableComponentModifier()
}
Expand Down
20 changes: 20 additions & 0 deletions Packages/src/Runtime/MaskingShape/MaskingShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ public MinMax01 softnessRange
}
}

/// <summary>
/// The transparency of the masking graphic.
/// </summary>
public float alpha
{
get => graphic ? graphic.color.a : 1;
set
{
value = Mathf.Clamp01(value);
if (!this || Mathf.Approximately(alpha, value)) return;

if (graphic)
{
var color = graphic.color;
color.a = value;
graphic.color = color;
}
}
}

protected override void OnEnable()
{
UpdateContainer();
Expand Down
27 changes: 26 additions & 1 deletion Packages/src/Runtime/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public enum MaskingMode
[Range(0f, 1f)]
private float m_AntiAliasingThreshold;

[SerializeField]
[Obsolete]
internal float m_Alpha = -1;

[SerializeField]
[Obsolete]
private float m_Softness = -1;
Expand All @@ -85,8 +89,8 @@ public enum MaskingMode
[Obsolete]
private bool m_PartOfParent;

private CanvasGroup _canvasGroup;
private CommandBuffer _cb;

private List<SoftMask> _children;
private bool _hasResolutionChanged;
private bool _hasSoftMaskBufferDrawn;
Expand Down Expand Up @@ -254,6 +258,27 @@ public MinMax01 softnessRange
}
}

/// <summary>
/// The transparency of the masking graphic.
/// </summary>
public float alpha
{
get => graphic ? graphic.color.a : 1;
set
{
value = Mathf.Clamp01(value);
if (!this || Mathf.Approximately(alpha, value)) return;

isDirty = true;
if (graphic)
{
var color = graphic.color;
color.a = value;
graphic.color = color;
}
}
}

/// <summary>
/// Clear color for the soft mask buffer.
/// </summary>
Expand Down

0 comments on commit 8080cb2

Please sign in to comment.