using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.UI;
public class PhotoCapture : MonoBehaviour
{
public Camera photoCamera;
public KeyCode photoKey = KeyCode.P;
public int photoWidth = 1920;
public int photoHeight = 1080;
public string photoDirectory = "Photos";
public RawImage photoDisplay;
public RawImage collectionsImage;
public Image borderImage;
public float displayDuration;
[SerializeField] private PlayerSwapScript playerSwapScript;
[SerializeField] private float raycastDistance;
[SerializeField] private GameObject tick5;
private bool photoIsTaken = false;
public bool photoOfHogTaken = false;
public AudioSource src;
public AudioClip AudioClip;
private void Start()
{
// Create photo directory if it doesn't exist
Directory.CreateDirectory(Path.Combine(Application.dataPath, photoDirectory));
// Hide photo UI elements at start
if (photoDisplay != null)
{
borderImage.gameObject.SetActive(false);
photoDisplay.gameObject.SetActive(false);
}
}
private void Update()
{
// Perform a forward raycast to detect hedgehogs
var rayDirection = photoCamera.transform.forward;
rayDirection = new Vector3(rayDirection.x, 0, rayDirection.z); // Flatten Y-axis for precision
var hits = Physics.RaycastAll(transform.position, rayDirection, raycastDistance);
Debug.DrawRay(transform.position, rayDirection * raycastDistance);
if (hits.Length > 0)
{
foreach (var hit in hits)
{
if (hit.collider.CompareTag("Hedgehog") && photoIsTaken)
{
photoOfHogTaken = true;
tick5.SetActive(true);
Debug.Log("Photo of hog taken");
break;
}
}
}
else
{
photoOfHogTaken = false;
}
// Take photo only when transformed (non-human)
if (Input.GetKeyDown(photoKey) && !PlayerSwapScript.isHumanShape)
{
TakePhoto();
}
}
void TakePhoto()
{
StartCoroutine(CapturePhoto());
}
private IEnumerator CapturePhoto()
{
if (photoCamera == null)
{
Debug.LogError("Photo Camera is not assigned. :( ");
yield break;
}
borderImage.gameObject.SetActive(true);
photoCamera.gameObject.SetActive(true);
yield return new WaitForEndOfFrame();
// Render camera to texture
RenderTexture rt = new RenderTexture(photoWidth, photoHeight, 24);
photoCamera.targetTexture = rt;
RenderTexture.active = rt;
photoCamera.Render();
Texture2D photo = new Texture2D(photoWidth, photoHeight, TextureFormat.RGB24, false);
photo.ReadPixels(new Rect(0, 0, photoWidth, photoHeight), 0, 0);
photo.Apply();
// Save photo to disk
byte[] bytes = photo.EncodeToPNG();
string filename = Path.Combine(Application.dataPath, photoDirectory, $"photo_{System.DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png");
File.WriteAllBytes(filename, bytes);
DisplayPhoto(photo);
photoIsTaken = true;
// Cleanup
photoCamera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
photoCamera.gameObject.SetActive(false);
Debug.Log($"Photo saved to: {filename}");
}
void DisplayPhoto(Texture2D photo)
{
StartCoroutine(ShowPhotoForDuration(photo));
}
private IEnumerator ShowPhotoForDuration(Texture2D photo)
{
if (photoDisplay != null)
{
// Play sound if available
if (AudioClip != null)
{
Debug.Log("found sound");
src.clip = AudioClip;
src.Play();
}
Debug.Log("Displaying photo");
photoDisplay.texture = photo;
borderImage.gameObject.SetActive(true);
photoDisplay.gameObject.SetActive(true);
yield return new WaitForSeconds(displayDuration);
// Hide after time expires
borderImage.gameObject.SetActive(false);
photoDisplay.gameObject.SetActive(false);
// Update gallery if hog was captured
if (collectionsImage != null && photoOfHogTaken)
{
collectionsImage.texture = photo;
}
}
}
}