summaryrefslogtreecommitdiff
path: root/Assets/Mountools/Tools/ToolFreeCamera.cs
blob: 8f7fce8905de1c1d843c8483ecf1c5ff7177509b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEngine;

namespace Mountools.Tools
{
    public class ToolFreeCamera : MonoBehaviour
    {
        [HideInInspector] public string cameraRotationX = "Mouse Y";
        [HideInInspector] public string cameraRotationY = "Mouse X";
        [HideInInspector] public string cameraRotationZ = "Horizontal";
        [HideInInspector] public string cameraForwardAxisName = "Vertical";
        [HideInInspector] public string cameraFastMoveButton = "Fire3";
        [HideInInspector] public string enableRotationZ = "Fire1";
        [HideInInspector] public string cameraRotationZRecover = "Jump";

        public float cameraSensitivity = 150f;
        public float cameraMovingSpeed = 20f;
        public float cameraFastMovingSpeed = 50f;
        public bool canCameraRotationZ;
        
        // 场景内相机状态
        private readonly Dictionary<Camera, bool> _cameraStatus = new();

        private float _currentSpeed;
        private Vector3 _currentRotation;

        // 相机状态
        public bool active => FindObjectOfType<ToolFreeCamera>() != null;

        public static void FreeCameraEnter()
        {
            if (! Application.isPlaying) return;
            if (FindObjectOfType<ToolFreeCamera>() != null) return;

            GameObject gameObject = new GameObject("Free Camera");
            Camera selfCamera = gameObject.AddComponent<Camera>();
            ToolFreeCamera freeCamera = gameObject.AddComponent<ToolFreeCamera>();
            Camera[] cameras = FindObjectsOfType<Camera>(true);

            freeCamera._cameraStatus.Clear();
            foreach (var cam in cameras)
            {
                if (cam == selfCamera) continue;
                
                freeCamera._cameraStatus.Add(cam, cam.gameObject.activeSelf);

                if (cam.gameObject.activeSelf)
                {
                    var camTransform = cam.transform;
                    var selfCameraTransform = selfCamera.transform;
                    
                    selfCameraTransform.position = camTransform.position;
                    freeCamera._currentRotation = camTransform.rotation.eulerAngles;
                    
                    selfCamera.fieldOfView = cam.fieldOfView;
                    selfCamera.orthographic = cam.orthographic;
                    selfCamera.depth = cam.depth;
                    selfCamera.aspect = cam.aspect;
                    selfCamera.clearFlags = cam.clearFlags;
                    selfCamera.backgroundColor = cam.backgroundColor;
                    
                    cam.gameObject.SetActive(false);
                    continue;
                }
                
                cam.gameObject.SetActive(false);
            }
            
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
        
        public static void FreeCameraExit()
        {
            if (! Application.isPlaying) return;
            var freeCamera = FindObjectOfType<ToolFreeCamera>();
            if (freeCamera == null) return;
            
            freeCamera.gameObject.SetActive(false);

            foreach (var cameras in freeCamera._cameraStatus)
            {
                cameras.Key.gameObject.SetActive(cameras.Value);
            }
            
            Destroy(freeCamera.gameObject);
        }
        
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
        
            if (Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.Escape))
            {
                if (Cursor.lockState == CursorLockMode.None)
                {
                    FreeCameraExit();
                }
                else
                {
                    Cursor.lockState = CursorLockMode.None;
                    Cursor.visible = true;
                }
            }
            
            UpdateCameraRotation();
        }

        public void UpdateCameraRotation()
        {
            if (Cursor.lockState == CursorLockMode.None) return;

            var camRotationZ = Input.GetAxis(cameraRotationZ);

            var canRotationZ = canCameraRotationZ && Input.GetButton(enableRotationZ);
        
            // Rotation Control
            var rotationVector = new Vector3
            {
                x = Input.GetAxis(cameraRotationX), 
                y = Input.GetAxis(cameraRotationY), 
                z = canRotationZ ? camRotationZ : 0
            };

            // Change Rotation
            var t = transform;
            _currentRotation.x -= cameraSensitivity * Time.fixedDeltaTime * rotationVector.x;
            _currentRotation.y += cameraSensitivity * Time.fixedDeltaTime * rotationVector.y;
            _currentRotation.z -= cameraSensitivity * Time.fixedDeltaTime * rotationVector.z;
            _currentRotation.x = ClampRotation(_currentRotation.x, -90, 90);
            _currentRotation.z = ClampRotation(_currentRotation.z, -90, 90);

            if (Input.GetButton(cameraRotationZRecover))
                _currentRotation.z = Mathf.Lerp(_currentRotation.z, 0, 0.2f);

            t.rotation = Quaternion.Euler(_currentRotation);
        
            // Movement Control
            var movementAxisForward = Input.GetAxis(cameraForwardAxisName);
            var movementAxisRight = canRotationZ ? 0 : camRotationZ;

            var speed = Input.GetButton(cameraFastMoveButton) ? cameraFastMovingSpeed : cameraMovingSpeed;
            _currentSpeed = Mathf.Lerp(_currentSpeed, speed, 0.3f);

            t.position += t.forward * (Time.deltaTime * _currentSpeed * movementAxisForward) +
                          t.right * (Time.deltaTime * _currentSpeed * movementAxisRight);
        }

        private static float ClampRotation(float n, float min, float max)
        {
            if (n < -360)
                n += 360;
        
            if (n > 360)
                n-= 360;
        
            return Mathf.Clamp(n, min, max);
        }
    }
}

#endif