Search Unity

UI not interactable after applying alpha fade

Discussion in 'UGUI & TextMesh Pro' started by gaglabs, Aug 14, 2020.

  1. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    I have my ui elements fading in and out based on whether they are in the camera view. Once they fade out and back in the buttons arent interactable anymore. This is the script that calls the fade-

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class HideUIElements : MonoBehaviour
    6. {
    7.  
    8.     public RectTransform myRectTransform;
    9.     public Camera myCamera;
    10.    
    11.     public List<Text> m_Text = new List<Text>();
    12. public List<RawImage> m_Raw = new List<RawImage>();
    13.      public List<Image> m_Image = new List<Image>();
    14.     public bool isFullyVisible;
    15.     void Update()
    16.     {
    17.       isFullyVisible = myRectTransform.IsVisibleFrom(myCamera);
    18.      
    19.     }
    20.  
    21.     void LateUpdate()
    22.     {
    23.         if(isFullyVisible)
    24.       {
    25.           for (int i = 0; i < m_Text.Count; i++)
    26.           {
    27.               m_Text[i].CrossFadeAlpha(1, 0.5f, true);
    28.           }
    29.           for (int t = 0; t < m_Image.Count; t++)
    30.           {
    31.               m_Image[t].CrossFadeAlpha(1, 0.5f, true);
    32.           }
    33.           for (int r = 0; r < m_Raw.Count; r++)
    34.           {
    35.               m_Raw[r].CrossFadeAlpha(1, 0.5f, true);
    36.           }
    37.                
    38.          
    39.       }else
    40.       {
    41.           for (int i = 0; i < m_Text.Count; i++)
    42.           {
    43.               m_Text[i].CrossFadeAlpha(0, 0.5f, true);
    44.           }
    45.           for (int t = 0; t < m_Image.Count; t++)
    46.           {
    47.               m_Image[t].CrossFadeAlpha(0, 0.5f, true);
    48.           }
    49.            for (int r = 0; r < m_Raw.Count; r++)
    50.           {
    51.               m_Raw[r].CrossFadeAlpha(0, 0.5f, true);
    52.           }
    53.       }
    54.     }
    55. }
    And this is the Extension script that is pulls from.

    Code (CSharp):
    1. using UnityEngine;
    2. public static class RendererExtensions
    3. {
    4.     /// <summary>
    5.     /// Counts the bounding box corners of the given RectTransform that are visible from the given Camera in screen space.
    6.     /// </summary>
    7.     /// <returns>The amount of bounding box corners that are visible from the Camera.</returns>
    8.     /// <param name="rectTransform">Rect transform.</param>
    9.     /// <param name="camera">Camera.</param>
    10.     private static int CountCornersVisibleFrom(this RectTransform rectTransform, Camera camera)
    11.     {
    12.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height); // Screen space bounds (assumes camera renders across the entire screen)
    13.         Vector3[] objectCorners = new Vector3[4];
    14.         rectTransform.GetWorldCorners(objectCorners);
    15.         int visibleCorners = 0;
    16.         Vector3 tempScreenSpaceCorner; // Cached
    17.         for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform
    18.         {
    19.             tempScreenSpaceCorner = camera.WorldToScreenPoint(objectCorners[i]); // Transform world space position of corner to screen space
    20.             if (screenBounds.Contains(tempScreenSpaceCorner)) // If the corner is inside the screen
    21.             {
    22.                 visibleCorners++;
    23.             }
    24.         }
    25.         return visibleCorners;
    26.     }
    27.     /// <summary>
    28.     /// Determines if this RectTransform is fully visible from the specified camera.
    29.     /// Works by checking if each bounding box corner of this RectTransform is inside the cameras screen space view frustrum.
    30.     /// </summary>
    31.     /// <returns><c>true</c> if is fully visible from the specified camera; otherwise, <c>false</c>.</returns>
    32.     /// <param name="rectTransform">Rect transform.</param>
    33.     /// <param name="camera">Camera.</param>
    34.     public static bool IsFullyVisibleFrom(this RectTransform rectTransform, Camera camera)
    35.     {
    36.         return CountCornersVisibleFrom(rectTransform, camera) == 1; // True if all 4 corners are visible
    37.     }
    38.     /// <summary>
    39.     /// Determines if this RectTransform is at least partially visible from the specified camera.
    40.     /// Works by checking if any bounding box corner of this RectTransform is inside the cameras screen space view frustrum.
    41.     /// </summary>
    42.     /// <returns><c>true</c> if is at least partially visible from the specified camera; otherwise, <c>false</c>.</returns>
    43.     /// <param name="rectTransform">Rect transform.</param>
    44.     /// <param name="camera">Camera.</param>
    45.     public static bool IsVisibleFrom(this RectTransform rectTransform, Camera camera)
    46.     {
    47.         return CountCornersVisibleFrom(rectTransform, camera) > 0; // True if any corners are visible
    48.     }
    49. }
    I cant figure out why its causing the UI elements to not be interactable anymore.
     
  2. Sam_Space

    Sam_Space

    Joined:
    Jun 28, 2019
    Posts:
    16
    I am having the same issue. I use a raycast to interact with the UI button panel, yet after I apply the fade it is no more interactable..