200 lines
5.6 KiB
C#

using System.IO;
using UnityEngine;
using static UnityEngine.Debug;
public class FlyCamera : MonoBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 10f;
public float fastMoveSpeed = 50f;
public float mouseSensitivity = 2f;
[Header("FOV Settings")]
public float minFOV = 10f;
public float maxFOV = 120f;
public float fovScrollSpeed = 10f;
[Header("Screenshot Settings")]
public KeyCode screenshotKey = KeyCode.F2;
private Camera _cam;
private float _rotationX;
private bool _isFlyCamEnabled = true;
private float _screenshotFlashTime;
private bool _showScreenshotFlash;
private void Start()
{
_cam = GetComponent<Camera>();
if (_cam == null)
{
_cam = Camera.main;
}
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
CreateScreenshotDirectory();
}
private void Update()
{
if (!_isFlyCamEnabled) return;
HandleMouseLook();
HandleMovement();
HandleFOVControl();
HandleScreenshot();
HandleScreenshotFlash();
if (Input.GetKeyDown(KeyCode.Escape))
{
ToggleCursor();
}
if (Cursor.lockState != CursorLockMode.Locked && Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
private void HandleMouseLook()
{
if (Cursor.lockState != CursorLockMode.Locked) return;
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
_rotationX -= mouseY;
_rotationX = Mathf.Clamp(_rotationX, -90f, 90f);
transform.rotation = Quaternion.Euler(_rotationX, transform.eulerAngles.y + mouseX, 0f);
}
private void HandleMovement()
{
Vector3 movement = Vector3.zero;
float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? fastMoveSpeed : moveSpeed;
if (Input.GetKey(KeyCode.W))
movement += transform.forward;
if (Input.GetKey(KeyCode.S))
movement -= transform.forward;
if (Input.GetKey(KeyCode.A))
movement -= transform.right;
if (Input.GetKey(KeyCode.D))
movement += transform.right;
if (Input.GetKey(KeyCode.Q))
movement -= transform.up;
if (Input.GetKey(KeyCode.E))
movement += transform.up;
transform.position += movement * (currentSpeed * Time.deltaTime);
}
private void HandleFOVControl()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0f)
{
_cam.fieldOfView = Mathf.Clamp(_cam.fieldOfView - scroll * fovScrollSpeed, minFOV, maxFOV);
}
}
private void HandleScreenshot()
{
if (Input.GetKeyDown(screenshotKey))
{
TakeScreenshot();
}
}
private void CreateScreenshotDirectory()
{
string screenshotDir = Path.Combine(Application.dataPath, "..", "Screenshots");
if (!Directory.Exists(screenshotDir))
{
Directory.CreateDirectory(screenshotDir);
}
}
private void TakeScreenshot()
{
StartCoroutine(CaptureScreenshotDelayed());
}
// ReSharper disable Unity.PerformanceAnalysis
private System.Collections.IEnumerator CaptureScreenshotDelayed()
{
yield return new WaitForEndOfFrame();
string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string filename = $"Screenshot_{timestamp}.png";
string screenshotDir = Path.Combine(Application.dataPath, "..", "Screenshots");
string fullPath = Path.Combine(screenshotDir, filename);
ScreenCapture.CaptureScreenshot(fullPath);
Log($"Screenshot saved: {fullPath}");
_showScreenshotFlash = true;
_screenshotFlashTime = 0.2f;
}
private void HandleScreenshotFlash()
{
if (_showScreenshotFlash)
{
_screenshotFlashTime -= Time.deltaTime;
if (_screenshotFlashTime <= 0f)
{
_showScreenshotFlash = false;
}
}
}
private void OnGUI()
{
if (_showScreenshotFlash)
{
GUI.color = new Color(1f, 1f, 1f, 0.8f);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Texture2D.whiteTexture);
GUIStyle style = new GUIStyle();
style.fontSize = 36;
style.normal.textColor = Color.black;
style.alignment = TextAnchor.MiddleCenter;
GUI.Label(new Rect(0, Screen.height / 2 - 50, Screen.width, 100), "Screenshot Captured!", style);
}
}
private void ToggleCursor()
{
if (Cursor.lockState == CursorLockMode.Locked)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
else
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
public void EnableFlyCamera(bool enable)
{
_isFlyCamEnabled = enable;
if (enable)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
}