/// <summary> /// Called when the visibility of the view has changed. /// </summary> protected virtual void VisibilityChanged() { if (AlphaProperty.IsUndefined(this, UIViewTemplates.Default) && IsVisibleProperty.IsUndefined(this, UIViewTemplates.Default) && RaycastBlockModeProperty.IsUndefined(this, UIViewTemplates.Default) && IsInteractableProperty.IsUndefined(this, UIViewTemplates.Default)) { return; } // to change alpha, visibility and raycast we need a canvas group attached if (CanvasGroup == null) { var canvasGroup = GameObject.GetComponent <CanvasGroup>(); CanvasGroup = canvasGroup == null?GameObject.AddComponent <CanvasGroup>() : canvasGroup; } // set alpha value var alpha = AlphaProperty.IsUndefined(this) ? 1 : Alpha; CanvasGroup.alpha = IsVisible ? alpha : 0; // set raycast block mode if (RaycastBlockMode == RaycastBlockMode.Always) { CanvasGroup.blocksRaycasts = true; } else if (RaycastBlockMode == RaycastBlockMode.Never) { CanvasGroup.blocksRaycasts = false; } else { CanvasGroup.blocksRaycasts = (IsVisible && alpha > 0); } if (IsInteractableProperty.IsUndefined(this)) { CanvasGroup.interactable = IsVisible && alpha > 0; } else { CanvasGroup.interactable = IsInteractable; } }
/// <summary> /// Returns boolean indicating if background is visible. /// </summary> private bool BackgroundIsVisible() { if (BackgroundColorProperty.IsUndefined(this)) { return(false); } if (BackgroundColor.a <= 0) { return(false); } if (!AlphaProperty.IsUndefined(this) && Alpha <= 0) { return(false); } return(IsVisible); }
/// <summary> /// Tests if mouse is over this view. /// </summary> public bool ContainsMouse(Vector3 mousePosition, bool testChildren = false, bool ignoreFullScreenViews = false) { // get root canvas UnityEngine.Canvas canvas = LayoutRoot.Canvas; // alpha transparent bool alphaTest = AlphaProperty.IsUndefined(this) ? true : Alpha > 0.99f; // for screen space overlay the camera should be null Camera worldCamera = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera; if (RectTransformUtility.RectangleContainsScreenPoint(this.RectTransform, mousePosition, worldCamera) && (!ignoreFullScreenViews || !IsFullScreen) && GameObject.activeInHierarchy && alphaTest) { return(true); } if (!testChildren) { return(false); } foreach (var child in LayoutChildren) { UIView view = child as UIView; if (view == null) { continue; } if (view.ContainsMouse(mousePosition, true, ignoreFullScreenViews)) { return(true); } } return(false); }