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);*/ private Color squareColor; private Coroutine flashCoroutine; private SpriteRenderer flashSpriteRenderer; void Start() { if (square == null) square = transform.Find("Square")?.gameObject; if (square != null) flashSpriteRenderer = square.GetComponent(); if (flashSpriteRenderer != null) { squareColor = flashSpriteRenderer.color; squareColor.a = 0; } // Initialize photoSpriteRenderer if photoCanvas is assigned /*if (photoCanvas != null) { photoSpriteRenderer = photoCanvas.GetComponent(); }*/ } public void TriggerFlash() { // Use coroutine to ensure capture happens after rendering //StartCoroutine(CaptureAndShow()); // Flash Effect: 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)); }*/ 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) { squareColor.a = alpha; flashSpriteRenderer.color = squareColor; } } // 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; }*/ }