using Org.BouncyCastle.Crypto.Macs;
using System;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class CameraControl : MonoBehaviour
{
public static CameraControl instance;
public List<string> list_RayName = new List<string>();
public Camera m_cam;
private Transform m_selfTrans;
private bool m_fingerDown = false;
public RaycastHit hit;
//当场景显示之前出现UI界面就关闭拖拽
public bool Boxcoller_bol = true;//判断碰撞框是否激活
/// <summary>
/// 单指滑动的手指位置
/// </summary>
private Vector3 m_oneFingerDragStartPos;
/// <summary>
/// 双指缩放的上一帧的双指距离
/// </summary>
private float m_twoFingerLastDistance = -1;
private void Awake()
{
instance = this;
m_cam = GetComponent<Camera>();
m_selfTrans = transform;
list_RayName.Add("Hospital_panel");
list_RayName.Add("Hospital_panel_1");
list_RayName.Add("Hoof_repair_rooms");
list_RayName.Add("Hoof_repair_room_2");
list_RayName.Add("Pancake_tudio_2");
list_RayName.Add("Wall");
list_RayName.Add("Caltha");
list_RayName.Add("Bar");
list_RayName.Add("Gaming");
list_RayName.Add("Guidance_table");
//导诊台的位置 Vector3(-3.74715376,-2.97996163,-45)
//画饼室的位置 Vector3(-0.677244067,-11.3491144,-45)
//修驴蹄的位置 Vector3(6.04842091,-8.08742237,-45)
//舔狗室的位置 Vector3(-16,-4.30645037,-45)
//厕所的位置 Vector3(-11.575222,2.41921329,-45)
//休息区的位置 Vector3(-3.88875127,5.90846729,-45)
//施工区的位置 Vector3(13.5474091,-3.76378059,-45)
}
public void MovePos(Vector3 pos)
{
transform.position = pos;
m_cam.orthographicSize = 10;
Boxcoller_bol = true;
}
private void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
if (Boxcoller_bol)
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = m_cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo; //射线对象(结构体),存储了相关信息
//发出射线检测碰撞,返回一个布尔值(是否发生碰撞),这里的out下面会详解
//碰到了标签是123的碰撞盒物体
if (Physics.Raycast(ray, out hitInfo))
{
hit = hitInfo;
for (int i = 0; i < list_RayName.Count; i++)
{
//Debug.Log(hitInfo.transform.gameObject.layer);
if (list_RayName.Contains(hitInfo.transform.name))
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition);
}
}
}
}
if (Input.GetMouseButtonUp(0))
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
if (m_fingerDown)
{
HandleFingerDragMove(Input.mousePosition);
}
var distance = Input.GetAxis("Mouse ScrollWheel");
HandleMouseScrollWheel(distance * 10);
}
#else
if(Boxcoller_bol)
{
if (2 == Input.touchCount)
{
// 双指缩放
HandleTwoFingerScale();
}
else if (1 == Input.touchCount)
{
if (TouchPhase.Began == Input.touches[0].phase)
{
Ray ray = m_cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (list_RayName.Contains(hitInfo.transform.name))
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition);
}
}
}
else if (TouchPhase.Moved == Input.touches[0].phase)
{
// 单指滑动
HandleFingerDragMove(Input.touches[0].position);
}
m_twoFingerLastDistance = -1;
}
else
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
}
#endif
}
//场景大小缩放时限制位置也会一起改变
float pos_y = 23.4f;
float pos_x = 16f;
/// <summary>
/// 单指滑动
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
//滑动差
Vector3 cha = m_oneFingerDragStartPos - GetWorldPos(fingerPos);
Vector3 newP = m_cam.transform.position;
newP.x = newP.x + cha.x;
if (newP.x > pos_x) { newP.x = pos_x; }
if (newP.x < -pos_x) { newP.x = -pos_x; }
newP.y = newP.y + cha.y;
if (newP.y > pos_y) { newP.y = pos_y; }
if (newP.y < -pos_y) { newP.y = -pos_y; }
m_selfTrans.position = newP;
}
/// <summary>
/// 双指缩放
/// </summary>
private void HandleTwoFingerScale()
{
float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
if (-1 == m_twoFingerLastDistance) m_twoFingerLastDistance = distance;
// 与上一帧比较变化
float scale = 0.1f * (distance - m_twoFingerLastDistance);
ScaleCamere(scale);
m_twoFingerLastDistance = distance;
}
/// <summary>
/// 鼠标滚轮缩放
/// </summary>
/// <param name="distance"></param>
private void HandleMouseScrollWheel(float distance)
{
if (0 == distance) return;
ScaleCamere(distance);
}
/// <summary>
/// 缩放摄像机的视口
/// </summary>
/// <param name="scale"></param>
private void ScaleCamere(float scale)
{
m_cam.orthographicSize -= scale * 0.5f;//缩小放大速度
//根据缩放大小实现相机移动的边界
CameraSize(m_cam.orthographicSize);
if (m_cam.orthographicSize < 10)//最小可视距离
{
m_cam.orthographicSize = 10;
}
if (m_cam.orthographicSize > 20)//最大可视距离
{
m_cam.orthographicSize = 20;
}
}
private void CameraSize(float orthographicSize)
{
if(orthographicSize>=10||15>= orthographicSize)
{
pos_y = 23.4f;
}
if (orthographicSize > 15 || 16 >= orthographicSize)
{
pos_y = 22.4f;
}
if (orthographicSize > 16 || 17 >= orthographicSize)
{
pos_y = 21.4f;
}
if (orthographicSize > 17 || 18 >= orthographicSize)
{
pos_y = 20.4f;
pos_x = 16f;
}
if (orthographicSize > 18 || 19 >= orthographicSize)
{
pos_y = 19.4f;
pos_x = 15.8f;
}
if (orthographicSize > 19 || 20 >= orthographicSize)
{
pos_y = 18.4f;
pos_x = 15.24f;
}
}
/// <summary>
/// 屏幕坐标换算成3D坐标
/// </summary>
/// <param name="screenPos">屏幕坐标</param>
/// <returns></returns>
Vector3 GetWorldPos(Vector2 screenPos)
{
return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 0));
}
}