From 8080cb24091ec49511e28c3c5ed92ff3477b5867 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:41:01 +0900 Subject: [PATCH] feat: revive `SoftMask.alpha` `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 --- .editorconfig | 7 ++-- .../SoftMaskComponentModifier_Alpha.cs | 32 +++++++++++++++++++ .../SoftMaskComponentModifier_Alpha.cs.meta | 11 +++++++ .../UISoftMaskModifierRunner.cs | 2 ++ .../src/Runtime/MaskingShape/MaskingShape.cs | 20 ++++++++++++ Packages/src/Runtime/SoftMask.cs | 27 +++++++++++++++- 6 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs create mode 100644 Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs.meta diff --git a/.editorconfig b/.editorconfig index 2e7d770..d26cb51 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 @@ -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 @@ -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 diff --git a/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs b/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs new file mode 100644 index 0000000..9a1f626 --- /dev/null +++ b/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs @@ -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 + { + 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"; + } + } +} diff --git a/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs.meta b/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs.meta new file mode 100644 index 0000000..3ef5bb9 --- /dev/null +++ b/Packages/src/Editor/AssetModification/SoftMaskComponentModifier_Alpha.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ebc69e30d39584993acc659154a974cd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Editor/AssetModification/UISoftMaskModifierRunner.cs b/Packages/src/Editor/AssetModification/UISoftMaskModifierRunner.cs index 1af1150..bf68736 100644 --- a/Packages/src/Editor/AssetModification/UISoftMaskModifierRunner.cs +++ b/Packages/src/Editor/AssetModification/UISoftMaskModifierRunner.cs @@ -15,6 +15,7 @@ public UISoftMaskModifierRunner() componentModifiers = new IComponentModifier[] { new SoftMaskComponentModifier_Softness(), + new SoftMaskComponentModifier_Alpha(), new SoftMaskComponentModifier_PartOfParent(), new SoftMaskableComponentModifier() } @@ -25,6 +26,7 @@ public UISoftMaskModifierRunner() componentModifiers = new IComponentModifier[] { new SoftMaskComponentModifier_Softness(), + new SoftMaskComponentModifier_Alpha(), new SoftMaskComponentModifier_PartOfParent(), new SoftMaskableComponentModifier() } diff --git a/Packages/src/Runtime/MaskingShape/MaskingShape.cs b/Packages/src/Runtime/MaskingShape/MaskingShape.cs index dd8a064..ef3d472 100644 --- a/Packages/src/Runtime/MaskingShape/MaskingShape.cs +++ b/Packages/src/Runtime/MaskingShape/MaskingShape.cs @@ -136,6 +136,26 @@ public MinMax01 softnessRange } } + /// + /// The transparency of the masking graphic. + /// + 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(); diff --git a/Packages/src/Runtime/SoftMask.cs b/Packages/src/Runtime/SoftMask.cs index 05f6a46..263c858 100644 --- a/Packages/src/Runtime/SoftMask.cs +++ b/Packages/src/Runtime/SoftMask.cs @@ -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; @@ -85,8 +89,8 @@ public enum MaskingMode [Obsolete] private bool m_PartOfParent; + private CanvasGroup _canvasGroup; private CommandBuffer _cb; - private List _children; private bool _hasResolutionChanged; private bool _hasSoftMaskBufferDrawn; @@ -254,6 +258,27 @@ public MinMax01 softnessRange } } + /// + /// The transparency of the masking graphic. + /// + 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; + } + } + } + /// /// Clear color for the soft mask buffer. ///