From 29a1c7d7cbb13b440407f75d57571baed66bd3b8 Mon Sep 17 00:00:00 2001 From: bottlefish <781230111@qq.com> Date: Tue, 30 Jun 2026 16:14:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(peipei):=20=E9=94=99=E8=89=B2=20Error=20ov?= =?UTF-8?q?erlay=20=E6=B7=A1=E5=85=A5=E6=B7=A1=E5=87=BA=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 自动绑定 sibling Error Sprite,并在 WrongColor 状态切换时同步 overlay alpha。 Co-authored-by: Cursor --- .../Scripts/MiniGame/Peipei/Eye/EyeTarget.cs | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Assets/Scripts/MiniGame/Peipei/Eye/EyeTarget.cs b/Assets/Scripts/MiniGame/Peipei/Eye/EyeTarget.cs index 53e3f7a3c..2fb0a934f 100644 --- a/Assets/Scripts/MiniGame/Peipei/Eye/EyeTarget.cs +++ b/Assets/Scripts/MiniGame/Peipei/Eye/EyeTarget.cs @@ -1,3 +1,4 @@ +using System; using AibisDream; using AibisDream.Kit; using AibisDream.Utility; @@ -6,17 +7,23 @@ using UnityEngine; public class EyeTarget : MonoBehaviour { + private static readonly int AlphaId = Shader.PropertyToID("_Alpha"); + public Transform targetTransform; [Header("Optional overlay sprite for UF imagination")] [SerializeField] private SpriteRenderer imageSpriteRender; + [Header("Wrong-color overlay (e.g. plantError); auto-resolved from sibling if unset")] + [SerializeField] private SpriteRenderer wrongColorSpriteRenderer; + [SerializeField] private Shader unifiedSpriteShader; [SerializeField] private EyeTargetVisual unifiedVisual; public Color _color; private EyeVisualSettings visualSettings = new(); + private Material wrongColorMaterial; private void Awake() { @@ -27,6 +34,7 @@ public class EyeTarget : MonoBehaviour return; } + EnsureWrongColorOverlay(); EnsureUnifiedVisual(spriteRenderer); DisableLegacyChildRenderers(); DisableLegacySiblingRenderers(); @@ -53,6 +61,7 @@ public class EyeTarget : MonoBehaviour public void KillVisualTweens() { unifiedVisual?.KillActiveTweens(); + wrongColorMaterial?.DOKill(); } private void EnsureVisualReady() @@ -115,11 +124,13 @@ public class EyeTarget : MonoBehaviour public void SetToNoWrongColor() { unifiedVisual?.SetWrongColor(0f); + SetWrongColorOverlayAlpha(0f); } public void SetToWrongColor() { unifiedVisual?.SetWrongColor(1f); + SetWrongColorOverlayAlpha(1f); } public void FadeOut(float duration) @@ -139,11 +150,13 @@ public class EyeTarget : MonoBehaviour public void WrongColorIn(float duration) { unifiedVisual?.TweenWrongColor(1f, duration)?.SetEase(Ease.InOutSine); + TweenWrongColorOverlay(1f, duration); } public void WrongColorOut(float duration) { unifiedVisual?.TweenWrongColor(0f, duration)?.SetEase(Ease.InOutSine); + TweenWrongColorOverlay(0f, duration); } public void BlurIn(float duration) @@ -291,6 +304,90 @@ public class EyeTarget : MonoBehaviour /// /// 禁用同组下旧版 overlay(如 posterNoColor / computernocolor),避免盖住 unified 效果。 /// + private void EnsureWrongColorOverlay() + { + if (wrongColorSpriteRenderer == null && transform.parent != null) + { + string errorName = $"{gameObject.name}Error"; + foreach (var renderer in transform.parent.GetComponentsInChildren(true)) + { + if (string.Equals(renderer.gameObject.name, errorName, StringComparison.OrdinalIgnoreCase)) + { + wrongColorSpriteRenderer = renderer; + break; + } + } + } + + if (wrongColorSpriteRenderer == null) + { + return; + } + + wrongColorMaterial = Application.isPlaying + ? wrongColorSpriteRenderer.material + : wrongColorSpriteRenderer.sharedMaterial; + wrongColorSpriteRenderer.enabled = true; + SetWrongColorOverlayAlpha(0f); + } + + private void SetWrongColorOverlayAlpha(float alpha) + { + if (wrongColorSpriteRenderer == null) + { + return; + } + + alpha = Mathf.Clamp01(alpha); + if (alpha > 0f) + { + wrongColorSpriteRenderer.gameObject.SetActive(true); + wrongColorSpriteRenderer.enabled = true; + } + + if (wrongColorMaterial != null && wrongColorMaterial.HasProperty(AlphaId)) + { + wrongColorMaterial.SetFloat(AlphaId, alpha); + } + + if (alpha <= 0f) + { + wrongColorSpriteRenderer.gameObject.SetActive(false); + } + } + + private void TweenWrongColorOverlay(float alpha, float duration) + { + if (wrongColorSpriteRenderer == null || wrongColorMaterial == null || !wrongColorMaterial.HasProperty(AlphaId)) + { + return; + } + + if (alpha > 0f) + { + wrongColorSpriteRenderer.gameObject.SetActive(true); + wrongColorSpriteRenderer.enabled = true; + } + + wrongColorMaterial.DOKill(); + wrongColorMaterial + .DOFloat(alpha, AlphaId, duration) + .SetEase(Ease.InOutSine) + .OnComplete(() => + { + if (alpha <= 0f && wrongColorSpriteRenderer != null) + { + wrongColorSpriteRenderer.gameObject.SetActive(false); + } + }); + } + + private static bool IsWrongColorOverlayRenderer(SpriteRenderer renderer) + { + return renderer != null + && renderer.gameObject.name.EndsWith("Error", StringComparison.OrdinalIgnoreCase); + } + private void DisableLegacySiblingRenderers() { Transform groupRoot = transform.parent; @@ -316,6 +413,11 @@ public class EyeTarget : MonoBehaviour continue; } + if (IsWrongColorOverlayRenderer(renderer)) + { + continue; + } + renderer.enabled = false; } }