177 lines
6.6 KiB
C#
177 lines
6.6 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using XFrame.Core.UI;
|
|
using YHElectric;
|
|
/// <summary>
|
|
/// 摄像机控制类
|
|
/// </summary>
|
|
public class CameraControl : MonoBehaviour
|
|
{
|
|
public static CameraControl instance;
|
|
|
|
[Tooltip("是否可控")]
|
|
public bool controlable = true;//是否可控
|
|
[Tooltip("是否上下无限制旋转,勾选上则摄像机在竖直方向上下可以无限制旋转")]
|
|
public bool unlimitedRotUpDown = true;//是否上下无限制旋转
|
|
[Tooltip("是否限制位置,勾选上则摄像机只能在一定范围内移动,配合(minX,minY,minZ,maxX,maxY,maxZ等参数.)")]
|
|
public bool limitedPosition = true;//是否限制位置
|
|
[Tooltip("键盘移动速度")]
|
|
[Range(10, 100)] public float boardMoveSpeed = 50;//键盘移动速度
|
|
[Tooltip("鼠标平移速度")]
|
|
[Range(0.1f, 5)] public float mouseMoveSpeed = 2;//鼠标移动速度
|
|
[Tooltip("滚轮移动速度")]
|
|
[Range(0.1f, 5)] public float scrolMoveSpeed = 0.5f;//滚轮移动速度
|
|
[Tooltip("旋转速度")]
|
|
[Range(10, 200)] public float rotateSpeed = 100;//旋转速度
|
|
[Tooltip("上下旋转最小角度")]
|
|
[Range(-80, 0)] public float minimumX = -60f;
|
|
[Tooltip("上下旋转最大角度")]
|
|
[Range(0, 80)] public float maximumX = 60f;
|
|
[Tooltip("最小限制位置")]
|
|
public Vector3 minLimit = new Vector3(-250, 2, -250);
|
|
[Tooltip("最大限制位置")]
|
|
public Vector3 maxLimit = new Vector3(250, 250, 250);
|
|
|
|
float curBoardMoveSpd = 0;
|
|
float curMouseMoveSpd = 0;
|
|
float curScrolMoveSpd = 0;
|
|
float curRotateSpd = 0;
|
|
|
|
public static bool isFollowUAV = false;
|
|
public Transform uav;
|
|
public Vector3 offestUav = new Vector3(5, 5, 5);
|
|
|
|
public static bool isUAVFiew = false;
|
|
public Transform uavCamPos;
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
|
|
|
|
if (!controlable) return;
|
|
if (EventSystem.current.IsPointerOverGameObject()) return;
|
|
//键盘移动
|
|
float h = Input.GetAxis("Horizontal");
|
|
float v = Input.GetAxis("Vertical");
|
|
curBoardMoveSpd = Mathf.Lerp(curBoardMoveSpd, Input.GetKey(KeyCode.LeftShift) ? boardMoveSpeed * 2 : boardMoveSpeed, Time.deltaTime);
|
|
transform.Translate(new Vector3(h, 0, v) * curBoardMoveSpd * Time.deltaTime);
|
|
|
|
//鼠标移动
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
curMouseMoveSpd = transform.position.y * mouseMoveSpeed;//越靠近地面速度越慢
|
|
transform.Translate(-Input.GetAxis("Mouse X") * curMouseMoveSpd * Time.deltaTime, -Input.GetAxis("Mouse Y") * curMouseMoveSpd * Time.deltaTime, 0, Space.Self);
|
|
}
|
|
|
|
//滚轮移动
|
|
if (Input.GetAxis("Mouse ScrollWheel") != 0)//滑动鼠标滚轮移动
|
|
{
|
|
curScrolMoveSpd = transform.position.y * scrolMoveSpeed;//越靠近地面速度越慢
|
|
transform.Translate(Vector3.forward * Input.GetAxis("Mouse ScrollWheel") * curScrolMoveSpd, Space.Self);
|
|
}
|
|
|
|
//旋转
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
curRotateSpd = rotateSpeed;
|
|
if (unlimitedRotUpDown)
|
|
{
|
|
//上下360°无限制旋转
|
|
Vector3 ptCenter = transform.position + transform.forward * 1.0f;
|
|
float rotX = Input.GetAxis("Mouse X") * curRotateSpd * Time.deltaTime;
|
|
float rotY = Input.GetAxis("Mouse Y") * curRotateSpd * Time.deltaTime;
|
|
transform.RotateAround(ptCenter, Vector3.up, rotX);
|
|
transform.RotateAround(ptCenter, -transform.right, rotY);
|
|
}
|
|
else
|
|
{
|
|
//限制上下旋转角度
|
|
transform.Rotate(0, Input.GetAxis("Mouse X") * curRotateSpd * Time.deltaTime, 0, Space.World);
|
|
transform.Rotate(-Input.GetAxis("Mouse Y") * curRotateSpd * Time.deltaTime, 0, 0, Space.Self);
|
|
transform.localEulerAngles = new Vector3(Mathf.Clamp(CheckAngle(transform.localEulerAngles.x), minimumX, maximumX), transform.localEulerAngles.y, 0);
|
|
}
|
|
}
|
|
//摄像机位置限制
|
|
if (limitedPosition)
|
|
{
|
|
transform.position = new Vector3(
|
|
Mathf.Clamp(transform.position.x, minLimit.x, maxLimit.x),
|
|
Mathf.Clamp(transform.position.y, minLimit.y, maxLimit.y),
|
|
Mathf.Clamp(transform.position.z, minLimit.z, maxLimit.z));
|
|
}
|
|
}
|
|
private void LateUpdate()
|
|
{
|
|
if (isFollowUAV)
|
|
{
|
|
transform.position = Vector3.Lerp(transform.position, uav.transform.position + offestUav, Time.deltaTime);
|
|
transform.LookAt(uav.transform);
|
|
isUAVFiew = false;
|
|
}
|
|
if (isUAVFiew)
|
|
{
|
|
isFollowUAV = false;
|
|
//transform.position = Vector3.Lerp(transform.position, uavCamPos.position, Time.deltaTime*10);
|
|
transform.position = uavCamPos.position;
|
|
transform.localEulerAngles = uavCamPos.localEulerAngles;
|
|
}
|
|
}
|
|
public float CheckAngle(float value)
|
|
{
|
|
float angle = value - 180;
|
|
|
|
if (angle > 0)
|
|
return angle - 180;
|
|
|
|
return angle + 180;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 定位模型
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
public void LocationModel(string name)
|
|
{
|
|
if (name == "monitor_qj")
|
|
{
|
|
Camera.main.GetComponent<Animator>().enabled = true;
|
|
//XUIPanel.ShowPanel<PanoramicEnergyPanel>();
|
|
//IndexPanel.introduction.SetActive(true);
|
|
//return;
|
|
}
|
|
|
|
GameManager.instance.modelLocation.TryGetValue(name, out Transform trans);
|
|
if (trans != null)
|
|
{
|
|
float time = Vector3.Distance(transform.position, trans.position) / 30;
|
|
controlable = false;
|
|
transform.DOMove(trans.position, time).SetEase(Ease.InOutQuad);
|
|
transform.DORotate(trans.localEulerAngles, time).SetEase(Ease.InOutQuad).OnComplete(() =>
|
|
{
|
|
controlable = true;
|
|
WebCommunication.CallWGL(name);
|
|
if(name== "strategy_scx")
|
|
GameManager.instance.playableDirector.Play();
|
|
});
|
|
}
|
|
}
|
|
public void OffLight(){ //Application.ExternalCall("CallWGLFunc", "display");
|
|
StartCoroutine(ModelLocation.instance.SetLightState(false));
|
|
}
|
|
public void OpenLight()
|
|
{
|
|
StartCoroutine(ModelLocation.instance.SetLightState(true));
|
|
}
|
|
public void RoamFinish()
|
|
{
|
|
Camera.main.GetComponent<Animator>().enabled = false;
|
|
//IndexPanel.introduction.SetActive(false);
|
|
XUIPanel.ClosePanel<PanoramicEnergyPanel>();
|
|
}
|
|
}
|