- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class PhotoCapture : MonoBehaviour
- {
- [Header("Photo Taker")]
- [SerializeField] private Image photoDisplayArea;
- [SerializeField] private GameObject photoFrame;
- [SerializeField] private GameObject cameraUI;
- [Header("Flash Effect")]
- [SerializeField] private GameObject cameraFlash;
- [SerializeField] private float flashTime;
- [Header("Photo Fader Effect")]
- [SerializeField] private Animator fadingAnimation;
- [Header("Audio")]
- [SerializeField] private AudioSource cameraAudio;
- private Texture2D screenCapture;
- private bool viewingPhoto;
- private bool isCaptureingPhoto;
- private void Start()
- {
- screenCapture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
- }
- public void OnClick(bool isPhotoMode)
- {
- if (isPhotoMode && viewingPhoto)
- {
- RemovePhoto();
- }
- if (!isPhotoMode)
- {
- RemovePhoto();
- }
- if (!viewingPhoto)
- {
- StartCoroutine(CapturePhoto());
- }
- }
- IEnumerator CapturePhoto()
- {//alt enter
- if (isCaptureingPhoto) yield break; //cantspam
- cameraUI.SetActive(false);
- isCaptureingPhoto = true;
- viewingPhoto = true;
- yield return new WaitForEndOfFrame();
- Rect regionToRead = new Rect(0, 0, Screen.width, Screen.height);
- screenCapture.ReadPixels(regionToRead, 0, 0, false);
- screenCapture.Apply();
- ShowPhoto();
- }
- void ShowPhoto()
- {
- Sprite photoSprite = Sprite.Create(screenCapture, new Rect(0.0f, 0.0f, screenCapture.width, screenCapture.height), new Vector2(0.5f, 0.5f), 100.0f);
- photoDisplayArea.sprite = photoSprite;
- photoFrame.SetActive(true);
- StartCoroutine(CameraFlashEffect());
- fadingAnimation.Play("CameraAphla");
- }
- IEnumerator CameraFlashEffect()
- {
- cameraAudio.Play();
- cameraFlash.SetActive(true);
- yield return new WaitForSeconds(flashTime);
- cameraFlash.SetActive(false);
- isCaptureingPhoto = false;
- }
- public void RemovePhoto()
- {
- viewingPhoto = false;
- photoFrame.SetActive(false);
- }
- }
[text] Capture
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- Capture
- Paste Code
- 01 Jul-2022
- 2.36 Kb
You can Share it: