151 lines
5.3 KiB
C#
151 lines
5.3 KiB
C#
|
using DG.Tweening; // Importing the DOTween library for smooth animations.
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems; // Using the EventSystem to handle UI interactions.
|
||
|
|
||
|
public class UIVisibilityController : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField]
|
||
|
private GameObject targetUIObj;
|
||
|
|
||
|
[SerializeField]
|
||
|
private GameObject triggerUIObj;
|
||
|
|
||
|
[SerializeField]
|
||
|
private GameObject canvasObj;
|
||
|
|
||
|
[SerializeField]
|
||
|
private Vector3 slideDirection = Vector3.zero;
|
||
|
|
||
|
[SerializeField]
|
||
|
private Vector3 slideDistance = Vector3.zero;
|
||
|
|
||
|
[SerializeField]
|
||
|
private float slideDuration = 0.2f;
|
||
|
|
||
|
// Cached positions and states for logic and animation.
|
||
|
private Vector3 targetOriginalPosition;
|
||
|
|
||
|
private Vector3 triggerOriginalPosition;
|
||
|
private Vector3 targetEndPosition;
|
||
|
private Vector3 triggerEndPosition;
|
||
|
private Vector3 originalCanvasPosition;
|
||
|
private bool isInActive = true; // Indicates if the UI is currently inactive.
|
||
|
private bool isOutActive = false; // Indicates if the UI is currently active.
|
||
|
private bool isMouseInTarget = false; // Is the mouse currently over the target UI?
|
||
|
private bool isMouseInTrigger = false; // Is the mouse currently over the trigger UI?
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
// Initial setup of component's states and values.
|
||
|
InitializeCanvasPosition();
|
||
|
InitializeSlideValues();
|
||
|
InitializeEventTrigger();
|
||
|
}
|
||
|
|
||
|
private void InitializeCanvasPosition()
|
||
|
{
|
||
|
if (canvasObj == null)
|
||
|
{
|
||
|
// Set canvasObj to the parent if not assigned.
|
||
|
canvasObj = transform.parent.gameObject;
|
||
|
}
|
||
|
originalCanvasPosition = canvasObj.transform.position;
|
||
|
}
|
||
|
|
||
|
private void InitializeSlideValues()
|
||
|
{
|
||
|
if (slideDirection == Vector3.zero)
|
||
|
{
|
||
|
// default slide direction is up (set to right).
|
||
|
slideDirection = Vector3.right;
|
||
|
Debug.LogError("SliderDirection not set, set default right");
|
||
|
}
|
||
|
if (slideDistance == Vector3.zero)
|
||
|
{
|
||
|
// default slide distance is based on targetUIObj dimensions.
|
||
|
RectTransform targetRT = targetUIObj.GetComponent<RectTransform>();
|
||
|
slideDistance = new Vector3(targetRT.rect.width, targetRT.rect.height, 0);
|
||
|
}
|
||
|
// Determine the start and end positions for sliding.
|
||
|
targetOriginalPosition = targetUIObj.transform.position;
|
||
|
triggerOriginalPosition = triggerUIObj.transform.position;
|
||
|
targetEndPosition = targetOriginalPosition + Vector3.Scale(slideDirection, slideDistance);
|
||
|
triggerEndPosition = triggerOriginalPosition + Vector3.Scale(slideDirection, slideDistance);
|
||
|
}
|
||
|
|
||
|
private void InitializeEventTrigger()
|
||
|
{
|
||
|
// Setting up event triggers for mouse interactions.
|
||
|
EventTrigger triggerObjET = triggerUIObj.GetComponent<EventTrigger>();
|
||
|
if (triggerObjET == null)
|
||
|
{
|
||
|
triggerObjET = triggerUIObj.AddComponent<EventTrigger>();
|
||
|
}
|
||
|
EventTrigger targetObjET = targetUIObj.GetComponent<EventTrigger>();
|
||
|
if (targetObjET == null)
|
||
|
{
|
||
|
targetObjET = targetUIObj.AddComponent<EventTrigger>();
|
||
|
}
|
||
|
triggerObjET.AddEventTrigger(EventTriggerType.PointerEnter, (eventData) => OnMouseIn(triggerUIObj));
|
||
|
triggerObjET.AddEventTrigger(EventTriggerType.PointerExit, (eventData) => OnMouseOut(triggerUIObj));
|
||
|
targetObjET.AddEventTrigger(EventTriggerType.PointerEnter, (eventData) => OnMouseIn(targetUIObj));
|
||
|
targetObjET.AddEventTrigger(EventTriggerType.PointerExit, (eventData) => OnMouseOut(targetUIObj));
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
// Check mouse interactions to determine whether to slide in or out the UI.
|
||
|
if ((isMouseInTrigger || isMouseInTarget) && isInActive)
|
||
|
{
|
||
|
SlideInScreen();
|
||
|
isInActive = false;
|
||
|
isOutActive = true;
|
||
|
}
|
||
|
else if (!isMouseInTrigger && !isMouseInTarget && isOutActive)
|
||
|
{
|
||
|
SlideOutScreen();
|
||
|
isInActive = true;
|
||
|
isOutActive = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnMouseIn(GameObject type)
|
||
|
{
|
||
|
// Update flags based on which UI object the mouse enters.
|
||
|
if (type == triggerUIObj)
|
||
|
{
|
||
|
isMouseInTrigger = true;
|
||
|
}
|
||
|
else if (type == targetUIObj)
|
||
|
{
|
||
|
isMouseInTarget = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnMouseOut(GameObject type)
|
||
|
{
|
||
|
// Update flags based on which UI object the mouse exits.
|
||
|
if (type == triggerUIObj)
|
||
|
{
|
||
|
isMouseInTrigger = false;
|
||
|
}
|
||
|
else if (type == targetUIObj)
|
||
|
{
|
||
|
isMouseInTarget = false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void SlideInScreen()
|
||
|
{
|
||
|
// Animate the UI objects to slide in.
|
||
|
triggerUIObj.transform.DOMove(triggerEndPosition, slideDuration).SetEase(Ease.OutCirc).Play();
|
||
|
targetUIObj.transform.DOMove(targetEndPosition, slideDuration).SetEase(Ease.OutCirc).Play();
|
||
|
}
|
||
|
|
||
|
private void SlideOutScreen()
|
||
|
{
|
||
|
// Animate the UI objects to slide out.
|
||
|
triggerUIObj.transform.DOMove(triggerOriginalPosition, slideDuration).SetEase(Ease.OutCirc).Play();
|
||
|
targetUIObj.transform.DOMove(targetOriginalPosition, slideDuration).SetEase(Ease.OutCirc).Play();
|
||
|
}
|
||
|
}
|