123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using UnityEngine;
- using UnityEngine.UI;
- public class UIFader : MonoBehaviour
- {
- public float flashSpeed = 0.5f; // 闪烁速度
- public float minAlpha = 0f; // 最低透明度
- public float maxAlpha = 1f; // 最高透明度
- private Image[] childImages;
- private bool flashing = false;
- void OnEnable()
- {
- // 只获取子物体上的 Image
- childImages = GetComponentsInChildren<Image>(true);
- childImages = System.Array.FindAll(childImages, img => img.gameObject != this.gameObject);
- flashing = true;
- }
- void OnDisable()
- {
- flashing = false;
- // 恢复透明度为 1
- if (childImages != null)
- {
- foreach (var img in childImages)
- {
- if (img != null)
- {
- Color c = img.color;
- c.a = 1f;
- img.color = c;
- }
- }
- }
- }
- void Update()
- {
- if (!flashing || childImages == null) return;
- float alpha = Mathf.Lerp(minAlpha, maxAlpha, (Mathf.Sin(Time.time * flashSpeed) + 1f) / 2f);
- foreach (var img in childImages)
- {
- if (img != null)
- {
- Color c = img.color;
- c.a = alpha;
- img.color = c;
- }
- }
- }
- }
|