using UnityEngine; using System.IO; using System.Collections; using UnityEngine.Networking; public class ScreenShot : MonoBehaviour { [SerializeField] private string apiUrl = "YOUR_API_ENDPOINT_HERE"; // Replace with your API endpoint [SerializeField] private float screenshotCooldown = 5f; // Cooldown in seconds private bool canTakeScreenshot = true; public void TakeAndUploadScreenshot() { if (canTakeScreenshot) { StartCoroutine(CaptureAndUploadScreenshot()); } else { Debug.Log("Screenshot on cooldown. Please wait."); } } private IEnumerator CaptureAndUploadScreenshot() { canTakeScreenshot = false; // Wait for the end of the frame to ensure all rendering is complete yield return new WaitForEndOfFrame(); // Create a texture to hold the screenshot Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); // Read the screen pixels into the texture screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); screenshot.Apply(); // Convert the texture to a PNG byte array byte[] imageBytes = screenshot.EncodeToPNG(); // Clean up the texture Destroy(screenshot); // Create a form to send the image WWWForm form = new WWWForm(); form.AddBinaryData("image", imageBytes, "screenshot.png", "image/png"); // Create and send the request using (UnityWebRequest www = UnityWebRequest.Post(apiUrl, form)) { yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { Debug.Log("Screenshot uploaded successfully!"); } else { Debug.LogError("Error uploading screenshot: " + www.error); } } // Wait for cooldown before allowing another screenshot yield return new WaitForSeconds(screenshotCooldown); canTakeScreenshot = true; } }