-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX_CameraFocus.cs
70 lines (61 loc) · 1.71 KB
/
EX_CameraFocus.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
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 摄像机聚焦示例代码
/// </summary>
public class EX_CameraFocus : MonoBehaviour
{
#region 与示例代码不大相关的初始化过程
//摄像机
[SerializeField] private Camera mainCamera;
//聚焦立方体
[SerializeField] private Button btnCube;
//聚焦球体
[SerializeField] private Button btnSphere;
[SerializeField]
private Transform cube;
[SerializeField]
private Transform sphere;
private Plane _plane;
void Start()
{
btnCube.onClick.AddListener(OnFocusCubeClick);
btnSphere.onClick.AddListener(OnFocusSphereClick);
_plane = new Plane(Vector3.up, Vector3.zero);
}
private void OnFocusCubeClick()
{
CameraFocusAt(cube);
}
private void OnFocusSphereClick()
{
CameraFocusAt(sphere);
}
#endregion
private void CameraFocusAt(Transform target)
{
var cp = CalcScreenCenterPosOnPanel();
var tp = target.position;
//1.直接移动
// mainCamera.transform.Translate(tp - cp,Space.World);
//2.使用tween移动
mainCamera.transform.DOMove(mainCamera.transform.position + (tp - cp), 0.5f);
}
/// <summary>
/// 屏幕中心点到panel上的坐标
/// </summary>
/// <returns></returns>
private Vector3 CalcScreenCenterPosOnPanel()
{
var ray = mainCamera.ScreenPointToRay(new Vector3((float) Screen.width / 2, (float)Screen.height / 2, 0));
if (_plane.Raycast(ray, out var distance))
{
return ray.GetPoint(distance);
}
else
{
return Vector3.zero;
}
}
}