Files
DuckovMods/CharacterPreview/HideSelfOnLeisure.cs

64 lines
1.9 KiB
C#
Raw Normal View History

2025-11-18 18:45:14 +08:00
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CharacterPreview
{
public class HideSelfOnLeisure:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
private Image image;
private Color CurrentColor => ModBehaviour.config.data.canEdit ? Color.green : Color.red;
private Color HideColor => ModBehaviour.config.data.canEdit
? new Color(Color.green.r, Color.green.g, Color.green.b, 0.1f)
: new Color(Color.red.r, Color.red.g, Color.red.b, 0.1f);
public float hideTime = 3f;
public float animationTime = 1f;
private float timer = 0;
private float animationTimer = 0;
private void Start()
{
image = GetComponent<Image>();
// if (!image)
// gameObject.SetActive(false);
}
private void Update()
{
if (timer < hideTime)
{
timer += Time.deltaTime;
if (timer >= hideTime)
{
animationTimer = 0;
}
}
if (animationTimer < animationTime)
{
animationTimer += Time.deltaTime;
// if(animationTimer >= animationTime)
// animationTimer=animationTime;
var t = animationTimer / animationTime;
t = Mathf.Clamp01(t);
image.color = Color.Lerp(CurrentColor, HideColor, t);
}
}
public void OnPointerEnter(PointerEventData eventData)
{
if(!image)return;
timer = hideTime;
animationTimer = animationTime;
image.color = CurrentColor;
}
public void OnPointerExit(PointerEventData eventData)
{
if (!image) return;
timer = 0;
}
}
}