Files
Continentis/Assets/OtherPlugins/Modern UI Pack/Scripts/Demo/DemoElementSway.cs

240 lines
7.3 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
#if !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace Michsky.MUIP
{
public class DemoElementSway : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
2026-03-20 11:56:50 -04:00
[Header("Resources")] [SerializeField] private DemoElementSwayParent swayParent;
2025-10-03 00:02:43 -04:00
[SerializeField] private Canvas mainCanvas;
[SerializeField] private RectTransform swayObject;
[SerializeField] private CanvasGroup normalCG;
[SerializeField] private CanvasGroup highlightedCG;
[SerializeField] private CanvasGroup selectedCG;
2026-03-20 11:56:50 -04:00
[Header("Settings")] [SerializeField] private float smoothness = 10;
2025-10-03 00:02:43 -04:00
[SerializeField] private float transitionSpeed = 8;
[SerializeField] [Range(0, 1)] private float dissolveAlpha = 0.5f;
2026-03-20 11:56:50 -04:00
[Header("Events")] [SerializeField] private UnityEvent onClick;
2025-10-03 00:02:43 -04:00
[HideInInspector] public bool wmSelected;
2026-03-20 11:56:50 -04:00
private bool allowSway;
private Vector3 cursorPos;
private Vector2 defaultPos;
2025-10-03 00:02:43 -04:00
2026-03-20 11:56:50 -04:00
private void Awake()
2025-10-03 00:02:43 -04:00
{
if (swayParent == null)
{
var tempSway = transform.parent.GetComponent<DemoElementSwayParent>();
2026-03-20 11:56:50 -04:00
if (tempSway == null) transform.parent.gameObject.AddComponent<DemoElementSwayParent>();
2025-10-03 00:02:43 -04:00
swayParent = tempSway;
}
defaultPos = swayObject.anchoredPosition;
normalCG.alpha = 1;
highlightedCG.alpha = 0;
}
2026-03-20 11:56:50 -04:00
private void Update()
2025-10-03 00:02:43 -04:00
{
#if ENABLE_LEGACY_INPUT_MANAGER
if (allowSway == true) { cursorPos = Input.mousePosition; }
#elif ENABLE_INPUT_SYSTEM
2026-03-20 11:56:50 -04:00
if (allowSway) cursorPos = Mouse.current.position.ReadValue();
2025-10-03 00:02:43 -04:00
#endif
2026-03-20 11:56:50 -04:00
if (mainCanvas.renderMode == RenderMode.ScreenSpaceOverlay)
ProcessOverlay();
else if (mainCanvas.renderMode == RenderMode.ScreenSpaceCamera)
ProcessSSC();
else if (mainCanvas.renderMode == RenderMode.WorldSpace) ProcessWorldSpace();
}
public void OnPointerClick(PointerEventData data)
{
onClick.Invoke();
}
public void OnPointerEnter(PointerEventData data)
{
allowSway = true;
swayParent.DissolveAll(this);
}
public void OnPointerExit(PointerEventData data)
{
allowSway = false;
swayParent.HighlightAll();
2025-10-03 00:02:43 -04:00
}
2026-03-20 11:56:50 -04:00
private void ProcessOverlay()
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
if (allowSway)
swayObject.position = Vector2.Lerp(swayObject.position, cursorPos, Time.deltaTime * smoothness);
else
swayObject.localPosition =
Vector2.Lerp(swayObject.localPosition, defaultPos, Time.deltaTime * smoothness);
2025-10-03 00:02:43 -04:00
}
2026-03-20 11:56:50 -04:00
private void ProcessSSC()
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
if (allowSway)
swayObject.position = Vector2.Lerp(swayObject.position, Camera.main.ScreenToWorldPoint(cursorPos),
Time.deltaTime * smoothness);
else
swayObject.localPosition =
Vector2.Lerp(swayObject.localPosition, defaultPos, Time.deltaTime * smoothness);
2025-10-03 00:02:43 -04:00
}
2026-03-20 11:56:50 -04:00
private void ProcessWorldSpace()
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
if (allowSway)
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
var clampedPos = new Vector3(cursorPos.x, cursorPos.y, mainCanvas.transform.position.z / 6f);
swayObject.position = Vector3.Lerp(swayObject.position, Camera.main.ScreenToWorldPoint(clampedPos),
Time.deltaTime * smoothness);
}
else
{
swayObject.localPosition =
Vector3.Lerp(swayObject.localPosition, defaultPos, Time.deltaTime * smoothness);
2025-10-03 00:02:43 -04:00
}
}
public void Dissolve()
{
2026-03-20 11:56:50 -04:00
if (wmSelected)
2025-10-03 00:02:43 -04:00
return;
StopCoroutine("DissolveHelper");
StopCoroutine("HighlightHelper");
StopCoroutine("ActiveHelper");
StartCoroutine("DissolveHelper");
}
public void Highlight()
{
2026-03-20 11:56:50 -04:00
if (wmSelected)
2025-10-03 00:02:43 -04:00
return;
StopCoroutine("DissolveHelper");
StopCoroutine("HighlightHelper");
StopCoroutine("ActiveHelper");
StartCoroutine("HighlightHelper");
}
public void Active()
{
2026-03-20 11:56:50 -04:00
if (wmSelected)
2025-10-03 00:02:43 -04:00
return;
StopCoroutine("DissolveHelper");
StopCoroutine("HighlightHelper");
StopCoroutine("HighlightHelper");
StartCoroutine("ActiveHelper");
}
public void WindowManagerSelect()
{
wmSelected = true;
StopCoroutine("ActiveHelper");
StopCoroutine("HighlightHelper");
StartCoroutine("WMSelectHelper");
}
public void WindowManagerDeselect()
{
wmSelected = false;
StartCoroutine("WMDeselectHelper");
StartCoroutine("DissolveHelper");
}
2026-03-20 11:56:50 -04:00
private IEnumerator DissolveHelper()
2025-10-03 00:02:43 -04:00
{
while (normalCG.alpha > dissolveAlpha)
{
normalCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
highlightedCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
yield return null;
}
highlightedCG.alpha = 0;
normalCG.alpha = dissolveAlpha;
highlightedCG.gameObject.SetActive(false);
}
2026-03-20 11:56:50 -04:00
private IEnumerator HighlightHelper()
2025-10-03 00:02:43 -04:00
{
while (normalCG.alpha < 1)
{
normalCG.alpha += Time.unscaledDeltaTime * transitionSpeed;
highlightedCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
yield return null;
}
normalCG.alpha = 1;
highlightedCG.alpha = 0;
highlightedCG.gameObject.SetActive(false);
}
2026-03-20 11:56:50 -04:00
private IEnumerator ActiveHelper()
2025-10-03 00:02:43 -04:00
{
highlightedCG.gameObject.SetActive(true);
while (highlightedCG.alpha < 1)
{
normalCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
highlightedCG.alpha += Time.unscaledDeltaTime * transitionSpeed;
yield return null;
}
highlightedCG.alpha = 1;
normalCG.alpha = 0;
}
2026-03-20 11:56:50 -04:00
private IEnumerator WMSelectHelper()
2025-10-03 00:02:43 -04:00
{
selectedCG.gameObject.SetActive(true);
while (selectedCG.alpha < 1)
{
normalCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
highlightedCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
selectedCG.alpha += Time.unscaledDeltaTime * transitionSpeed;
yield return null;
}
highlightedCG.alpha = 0;
normalCG.alpha = 0;
selectedCG.alpha = 1;
}
2026-03-20 11:56:50 -04:00
private IEnumerator WMDeselectHelper()
2025-10-03 00:02:43 -04:00
{
while (selectedCG.alpha > 0)
{
selectedCG.alpha -= Time.unscaledDeltaTime * transitionSpeed;
yield return null;
}
selectedCG.alpha = 0;
selectedCG.gameObject.SetActive(false);
}
}
}