E_ElecCompetition/Electrical_inspectionCompet.../Assets/Script/Camera/CameraControl1.cs

176 lines
5.7 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.UI;
using System;
using System.Collections;
using TMPro;
public class CameraControl1 : MonoBehaviour
{
public static CameraControl1 ins;
[Header("设置参数")]
[SerializeField] float minDis = 2;
[SerializeField] float maxDis ;
[SerializeField] float mouseRotSpeed = 8;
[SerializeField] float touchZoomSpeed = 0.1f;
[SerializeField] float touchRotateSpeed = 0.3f;
[SerializeField] float mouseMoveSpeed = 1;
[SerializeField] float mouseZoomSpeed = 10;
[SerializeField] float rSmoothTime = 0.3f;
[SerializeField] float sSmoothTime = 0.3f;
[SerializeField] float mSmoothTime = 0.3f;
[SerializeField] float minVerticalAngle = 0;
[SerializeField] float maxVerticalAngle = 60;
[Header("用于展示数据")]
[SerializeField] float horizontalAngle = 0;
[SerializeField] float verticalAngle = 0;
[SerializeField] float currentDis = 80;
[SerializeField] Vector3 originPosition;
[SerializeField] Vector3 targetPosition;
//[HideInInspector] public bool isrotateend;
bool isrotateable;
bool isscaleable;
float _targetDis;
private Touch oldTouch1;
private Touch oldTouch2;
//
public bool UPDOWN = false;
float targetDis
{
set
{
_targetDis = value > maxDis ? maxDis : value < minDis ? minDis : value;
}
get { return _targetDis; }
}
Vector3 rVelocity = Vector3.zero;
float sVelocity = 0;
Vector3 mVelocity = Vector3.zero;
Vector3 dampRotation;
Transform targetTrans;
bool m_IsSingleFinger;
private void Awake()
{
ins = this;
targetDis = currentDis;
targetPosition = originPosition;
}
[ContextMenu("设置相机位置")]
private void SetCameraTrans()
{
transform.eulerAngles = new Vector3(verticalAngle, horizontalAngle, 0);
var offset = transform.forward * -currentDis;
transform.position = originPosition + offset;
}
public void SetTarget(Transform target, float distance = 4)
{
targetDis = distance;
targetTrans = target;
}
public void SetCenterPoint(Vector3 target, float distance = 4)
{
targetDis = distance;
targetPosition = target;
}
public void Recover(float max, float min)
{
// targetPosition = originPosition = Vector3.zero;
horizontalAngle -= horizontalAngle % 360;
verticalAngle = 0;
maxDis = max;
minDis = min;
targetDis = maxDis;
// targetTrans = null;
}
public void RecoverTwo()
{
horizontalAngle -= horizontalAngle % 360;
verticalAngle = 0;
targetDis = maxDis;
}
private void Update()
{
//限制视角上下
if (UPDOWN)
{
if (verticalAngle < -15)
{
verticalAngle = -15;
}
}
if (Input.touchCount == 0)
{
// isscaleable = true;
isrotateable = true;
if (Input.GetMouseButton(0))
{
horizontalAngle += Input.GetAxis("Mouse X") * mouseRotSpeed;
verticalAngle -= Input.GetAxis("Mouse Y") * mouseRotSpeed;
verticalAngle = Mathf.Clamp(verticalAngle, minVerticalAngle, maxVerticalAngle);
}
targetDis = targetDis - Input.GetAxis("Mouse ScrollWheel") * mouseZoomSpeed;
}
if (Input.touchCount == 1)
{
if (isrotateable)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
oldTouch1 = Input.touches[0];
}
var deltaPos = Input.touches[0].position - oldTouch1.position;
horizontalAngle += deltaPos.x * touchRotateSpeed*3f ;
verticalAngle -= deltaPos.y * touchRotateSpeed;
verticalAngle = Mathf.Clamp(verticalAngle, minVerticalAngle, maxVerticalAngle);
oldTouch1 = Input.touches[0];
}
}
if (Input.touchCount == 2)
{
isrotateable = false ;
if (Input.touches[1].phase == TouchPhase.Began)
{
oldTouch1 = Input.touches[0];
oldTouch2 = Input.touches[1];
}
float currentTouchDistance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
float lastTouchDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
if (Input.touches[0].phase != TouchPhase.Ended && Input.touches[1].phase != TouchPhase.Ended)
{
//计算上次和这次双指触摸之间的距离差距
//然后去更改摄像机的距离
targetDis -= (currentTouchDistance - lastTouchDistance) * touchZoomSpeed;
oldTouch1 = Input.touches[0];
oldTouch2 = Input.touches[1];
}
}
currentDis = Mathf.SmoothDamp(currentDis, targetDis, ref sVelocity, rSmoothTime);
dampRotation = Vector3.SmoothDamp(dampRotation, new Vector3(verticalAngle, horizontalAngle), ref rVelocity, rSmoothTime);
transform.rotation = Quaternion.Euler(dampRotation);
if (targetTrans != null)
{
if (originPosition != targetTrans.position)
originPosition = Vector3.SmoothDamp(originPosition, targetTrans.position, ref mVelocity, mSmoothTime);
}
else
{
if (originPosition != targetPosition)
originPosition = Vector3.SmoothDamp(originPosition, targetPosition, ref mVelocity, mSmoothTime);
}
var offset = transform.forward * -currentDis;
transform.position = originPosition + offset;
}
}