Files
AppleHillsProduction/Assets/Scripts/DamianExperiments/Minigame/FlashBehaviour.cs

118 lines
3.5 KiB
C#
Raw Normal View History

2025-10-20 13:21:57 +02:00
using UnityEngine;
using System.Collections;
public class FlashBehaviour : MonoBehaviour
{
public GameObject square; // Assign in inspector or find in Start
public float flashInDuration = 0.05f;
public float flashOutDuration = 0.2f;
//Photo display settings
/*public GameObject photoCanvas;
private SpriteRenderer photoSpriteRenderer;
public Vector3 photoScale = new Vector3(0.2f, 0.2f, 1f);
public Vector3 photoPosition = new Vector3(2, 2, 0);*/
2025-10-20 13:21:57 +02:00
private Color squareColor;
private Coroutine flashCoroutine;
private SpriteRenderer flashSpriteRenderer;
2025-10-20 13:21:57 +02:00
void Start()
{
if (square == null)
square = transform.Find("Square")?.gameObject;
if (square != null)
flashSpriteRenderer = square.GetComponent<SpriteRenderer>();
2025-10-20 13:21:57 +02:00
if (flashSpriteRenderer != null)
2025-10-20 13:21:57 +02:00
{
squareColor = flashSpriteRenderer.color;
2025-10-20 13:21:57 +02:00
squareColor.a = 0;
}
// Initialize photoSpriteRenderer if photoCanvas is assigned
/*if (photoCanvas != null)
{
photoSpriteRenderer = photoCanvas.GetComponent<SpriteRenderer>();
}*/
2025-10-20 13:21:57 +02:00
}
public void TriggerFlash()
{
// Use coroutine to ensure capture happens after rendering
//StartCoroutine(CaptureAndShow());
// Flash Effect:
2025-10-20 13:21:57 +02:00
if (flashCoroutine != null)
StopCoroutine(flashCoroutine);
flashCoroutine = StartCoroutine(FlashRoutine());
}
// Capture screen and show photo
/*private IEnumerator CaptureAndShow()
{
yield return new WaitForEndOfFrame();
Texture2D screenTexture = ScreenCapture.CaptureScreenshotAsTexture();
Debug.Log("Captured Screen Texture: " + screenTexture.width + "x" + screenTexture.height);
StartCoroutine(ShowCapturedImage(screenTexture));
}*/
2025-10-20 13:21:57 +02:00
private IEnumerator FlashRoutine()
{
// Fade in
float t = 0;
while (t < flashInDuration)
{
t += Time.deltaTime;
SetAlpha(Mathf.Lerp(0, 1, t / flashInDuration));
yield return null;
}
SetAlpha(1);
// Fade out
t = 0;
while (t < flashOutDuration)
{
t += Time.deltaTime;
SetAlpha(Mathf.Lerp(1, 0, t / flashOutDuration));
yield return null;
}
SetAlpha(0);
}
private void SetAlpha(float alpha)
{
if (flashSpriteRenderer != null)
2025-10-20 13:21:57 +02:00
{
squareColor.a = alpha;
flashSpriteRenderer.color = squareColor;
2025-10-20 13:21:57 +02:00
}
}
// Show captured image on photo canvas
/*private IEnumerator ShowCapturedImage(Texture2D texture)
{
byte[] pngData = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/../CapturedScreen.png", pngData);
Debug.Log("Saved screenshot to: " + Application.dataPath + "/../CapturedScreen.png");
if (photoSpriteRenderer == null)
{
Debug.LogWarning("photoSpriteRenderer is not assigned or photoCanvas is missing a SpriteRenderer.");
yield break;
}
Sprite capturedSprite = Sprite.Create(
texture,
new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f),
100f
);
photoSpriteRenderer.sprite = capturedSprite;
photoCanvas.transform.position = photoPosition;
photoCanvas.transform.localScale = photoScale;
yield return null;
}*/
2025-10-20 13:21:57 +02:00
}