blob: c30949268739b4f2433aa4c4305f021b4bdfc080 (
plain)
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
|
using UnityEngine;
public class UICameraController : MonoBehaviour
{
[SerializeField]
private float cameraSpeed = 10f;
[SerializeField]
private float cameraZoom = 1f;
private Vector3 oldPos;
private void Start()
{
}
private void Update()
{
UpdateMovement();
UpdateZoom();
}
public void ResetPosition()
{
base.transform.localPosition = Vector3.zero;
cameraZoom = 1f;
base.transform.localScale = Vector3.one * cameraZoom;
}
private Vector3 ViewPointToPixels(Vector3 view)
{
return new Vector3(view.x * (float)Camera.main.scaledPixelWidth, view.y * (float)Camera.main.scaledPixelHeight, 0f);
}
private void UpdateMovement()
{
if (Input.GetMouseButtonDown(1))
{
oldPos = ViewPointToPixels(Camera.main.ScreenToViewportPoint(Input.mousePosition));
}
if (Input.GetMouseButton(1))
{
Vector3 vector = ViewPointToPixels(Camera.main.ScreenToViewportPoint(Input.mousePosition));
Vector3 translation = vector - oldPos;
translation.z = 0f;
base.transform.Translate(translation);
oldPos = vector;
}
else
{
Vector3 vector2 = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0f);
if (vector2.sqrMagnitude > 0.1f)
{
base.transform.Translate(-vector2.normalized * Time.deltaTime * cameraSpeed * (6f / (5f + cameraZoom)));
}
}
}
private void UpdateZoom()
{
float num = cameraZoom;
cameraZoom = Mathf.Clamp(cameraZoom + Input.mouseScrollDelta.y / 10f, 0.5f, 2f);
base.transform.localScale = Vector3.one * cameraZoom;
base.transform.localPosition *= cameraZoom / num;
base.transform.localPosition += new Vector3(0f, 0.5f * (float)Camera.main.scaledPixelHeight * (cameraZoom - num), 0f);
}
}
|