Chụp ảnh màn hình trong game
C# Code
using UnityEngine;
using System.IO;
public class ScreenshotHandler : MonoBehaviour
{
public KeyCode screenshotKey = KeyCode.F12;
public string screenshotFolderName = "Screenshots";
void Update()
{
if (Input.GetKeyDown(screenshotKey))
{
TakeScreenshot();
}
}
public void TakeScreenshot()
{
// Tạo thư mục nếu nó chưa tồn tại
string folderPath = Path.Combine(Application.persistentDataPath, screenshotFolderName);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// Tạo tên file duy nhất dựa trên ngày và giờ
string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string fileName = $"Screenshot_{timestamp}.png";
string filePath = Path.Combine(folderPath, fileName);
// Chụp và lưu ảnh
ScreenCapture.CaptureScreenshot(filePath);
Debug.Log($"Screenshot saved to: {filePath}");
// Tùy chọn: Hiển thị thông báo cho người chơi
}
}Cung cấp chức năng cho người chơi chụp lại khoảnh khắc trong game và lưu thành tệp PNG vào một thư mục được chỉ định.