Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity UI Query Repeat Code In MaskableGraphic And Mask

Discussion in 'UGUI & TextMesh Pro' started by Jimmy_Lin, May 9, 2018.

  1. Jimmy_Lin

    Jimmy_Lin

    Joined:
    May 8, 2015
    Posts:
    10
    Code From Unity5.6 (Unity-Technologies-ui-378bd9240df3)
    MaskableGraphic.cs
    Code (CSharp):
    1.  
    2.         protected override void OnEnable()
    3.         {
    4.             base.OnEnable();
    5.             m_ShouldRecalculateStencil = true;
    6.             UpdateClipParent();
    7.             SetMaterialDirty();
    8.  
    9.             if (GetComponent<Mask>() != null)
    10.             {
    11.                 MaskUtilities.NotifyStencilStateChanged(this);
    12.             }
    13.         }
    14.  
    15.         protected override void OnDisable()
    16.         {
    17.             base.OnDisable();
    18.             m_ShouldRecalculateStencil = true;
    19.             SetMaterialDirty();
    20.             UpdateClipParent();
    21.             StencilMaterial.Remove(m_MaskMaterial);
    22.             m_MaskMaterial = null;
    23.  
    24.             if (GetComponent<Mask>() != null)
    25.             {
    26.                 MaskUtilities.NotifyStencilStateChanged(this);
    27.             }
    28.         }
    Mask.cs
    Code (CSharp):
    1.  protected override void OnEnable()
    2.         {
    3.             base.OnEnable();
    4.             if (graphic != null)
    5.             {
    6.                 graphic.canvasRenderer.hasPopInstruction = true;
    7.                 graphic.SetMaterialDirty();
    8.             }
    9.  
    10.             MaskUtilities.NotifyStencilStateChanged(this);
    11.         }
    12.  
    13.         protected override void OnDisable()
    14.         {
    15.             // we call base OnDisable first here
    16.             // as we need to have the IsActive return the
    17.             // correct value when we notify the children
    18.             // that the mask state has changed.
    19.             base.OnDisable();
    20.             if (graphic != null)
    21.             {
    22.                 graphic.SetMaterialDirty();
    23.                 graphic.canvasRenderer.hasPopInstruction = false;
    24.                 graphic.canvasRenderer.popMaterialCount = 0;
    25.             }
    26.  
    27.             StencilMaterial.Remove(m_MaskMaterial);
    28.             m_MaskMaterial = null;
    29.             StencilMaterial.Remove(m_UnmaskMaterial);
    30.             m_UnmaskMaterial = null;
    31.  
    32.             MaskUtilities.NotifyStencilStateChanged(this);
    33.         }
    MaskUtilities.cs
    Code (CSharp):
    1. public static void NotifyStencilStateChanged(Component mask)
    2.         {
    3.             var components = ListPool<Component>.Get();
    4.             mask.GetComponentsInChildren(components);
    5.             for (var i = 0; i < components.Count; i++)
    6.             {
    7.                 if (components[i] == null || components[i].gameObject == mask.gameObject)
    8.                     continue;
    9.  
    10.                 var toNotify = components[i] as IMaskable;
    11.                 if (toNotify != null)
    12.                     toNotify.RecalculateMasking();
    13.             }
    14.             ListPool<Component>.Release(components);
    15.         }
    i don't know why both of them use MaskUtilities.NotifyStencilStateChanged(this);
    From the source code,i think that's enough for only one place.
    Special condition?Mask is here,but is disable?
    I am not sure..
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,078
    when you disable the mask, the mask is not active anymore so you see everything below (unmasked). To trigger that the NotifyStencilStateChanged methos has to be called.

    That is what I guess at least.
     
  3. Jimmy_Lin

    Jimmy_Lin

    Joined:
    May 8, 2015
    Posts:
    10
    I agree your viewpoint.
    I think the code NotifyStencilStateChanged in Mask.cs Enable() and Disable() is enough..
    but why in MaskableGraphic.cs Enable() and Disable() .. the same Code Repeated;