-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCameraController.cs
145 lines (118 loc) · 5 KB
/
CameraController.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TouchSimulatorForUnity
{
public class CameraController : MonoBehaviour
{
[Header("Zoom")]
[SerializeField]
float orthographicSize = 12;
[SerializeField]
float orthographicSizeMin = 6;
[SerializeField]
float orthographicSizeMax = 20;
[Header("Move")]
[SerializeField]
Transform borderRight;
[SerializeField]
Transform borderLeft;
[SerializeField]
Transform borderTop;
[SerializeField]
Transform borderBottom;
Vector2 currentPointerPos, lastPointerPos;
bool isTapping, isTappingSecond;
void Awake()
{
Camera.main.orthographicSize = orthographicSize;
#if UNITY_EDITOR
var touchSimulator = FindFirstObjectByType<TouchSimulator>();
touchSimulator.OnPositionInput += OnPositionInput;
touchSimulator.OnTapInput += OnTapInput;
touchSimulator.OnTapSecondInput += OnTapSecondInput;
touchSimulator.OnPinchInput += OnPinchInput;
#endif
void OnPositionInput(Vector2 pos)
{
lastPointerPos = currentPointerPos;
currentPointerPos = pos;
if (isTapping && !isTappingSecond)
{
var currentPointerPosWorld = Camera.main.ScreenToWorldPoint(currentPointerPos);
var lastPointerPosWorld = Camera.main.ScreenToWorldPoint(lastPointerPos);
var direction = currentPointerPosWorld - lastPointerPosWorld;
MoveBy(-direction);
}
}
void OnTapInput(bool isTapping)
{
this.isTapping = isTapping;
if (isTapping)
{
}
else
{
lastPointerPos = Vector2.zero;
currentPointerPos = Vector2.zero;
}
}
void OnTapSecondInput(bool isTapping)
{
this.isTappingSecond = isTapping;
}
void OnPinchInput(Vector2 primaryCurrent, Vector2 secondaryCurrent, Vector2 primaryLast, Vector2 secondaryLast)
{
if (!isTapping || !isTappingSecond)
return;
var lastPinchPos = new Vector2(
(primaryLast.x + secondaryLast.x) / 2,
(primaryLast.y + secondaryLast.y) / 2);
var lastPosWorld = Camera.main.ScreenToWorldPoint(lastPinchPos);
var currentPinchDistance = Vector2.Distance(primaryCurrent, secondaryCurrent);
var lastPinchDistance = Vector2.Distance(primaryLast, secondaryLast);
var newOrthographicSize = lastPinchDistance * Camera.main.orthographicSize / currentPinchDistance;
var lastOrthographicSize = Camera.main.orthographicSize;
SetOrthographicSize(newOrthographicSize);
var currentPinchPos = new Vector2(
(primaryCurrent.x + secondaryCurrent.x) / 2,
(primaryCurrent.y + secondaryCurrent.y) / 2);
var currentPosWorld = Camera.main.ScreenToWorldPoint(currentPinchPos);
var scaleMarginDirection = lastPosWorld - currentPosWorld;
MoveBy(scaleMarginDirection * newOrthographicSize / lastOrthographicSize);
}
}
public void SetOrthographicSize(float newOrthographicSize)
{
Camera.main.orthographicSize = newOrthographicSize;
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, orthographicSizeMin, orthographicSizeMax);
ConstrainPosition();
}
public void MoveBy(Vector2 direction)
{
Camera.main.transform.position = new(
Camera.main.transform.position.x + direction.x,
Camera.main.transform.position.y + direction.y,
Camera.main.transform.position.z);
ConstrainPosition();
}
void ConstrainPosition()
{
var aspectRatio = (float)Screen.width / Screen.height;
var maxPos = new Vector2(
borderRight.position.x - Camera.main.orthographicSize * aspectRatio,
borderTop.position.y - Camera.main.orthographicSize
);
var minPos = new Vector2(
borderLeft.position.x + Camera.main.orthographicSize * aspectRatio,
borderBottom.position.y + Camera.main.orthographicSize
);
Camera.main.transform.position = new(
Mathf.Clamp(Camera.main.transform.position.x, minPos.x, maxPos.x),
Mathf.Clamp(Camera.main.transform.position.y, minPos.y, maxPos.y),
Camera.main.transform.position.z
);
}
}
}