You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.6 KiB
167 lines
4.6 KiB
4 years ago
|
using AX.InputSystem;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using AX.MessageSystem;
|
||
|
using System;
|
||
|
|
||
|
public class FireDeployCameraManager : MonoBehaviour
|
||
|
{
|
||
|
public Transform target;
|
||
|
public float mSpeed = 50;
|
||
|
public float distance = 100, minDistance = 15, maxDistance = 200;
|
||
|
public bool needDamping = true;
|
||
|
public float damping = 5.0f;
|
||
|
|
||
|
private Vector3 position = new Vector3();
|
||
|
|
||
|
private float mouseScroll = 0.0f;
|
||
|
|
||
|
private Vector3 prevPos = Vector3.zero;//进入拖动过程前(开始拖动)时鼠标在屏幕坐标系中的位置
|
||
|
private Vector3 mousePostion = Vector3.zero;
|
||
|
private bool dragStart;
|
||
|
|
||
|
private float distanceResult;
|
||
|
|
||
|
// Use this for initialization
|
||
|
void Awake()
|
||
|
{
|
||
|
target = GameObject.Find("FireDeployCameraTarget").transform;
|
||
|
}
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
transform.gameObject.SetActive(false);
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void LateUpdate()
|
||
|
{
|
||
|
if (target)
|
||
|
{
|
||
|
if (dragStart)
|
||
|
UpdateCameraTargetHorizontalPos();
|
||
|
|
||
|
UpdateCameraScale();
|
||
|
|
||
|
UpdateCameraState();
|
||
|
}
|
||
|
}
|
||
|
float a = 100;
|
||
|
/// <summary>
|
||
|
/// 更新鼠标滚动操作传人的数据
|
||
|
/// </summary>
|
||
|
public void UpdateCameraScaleData(float mouseScrollData, float distanceRslt)
|
||
|
{
|
||
|
//Debug.Log("++++++");
|
||
|
|
||
|
mouseScroll = mouseScrollData;
|
||
|
distanceResult = distanceRslt;
|
||
|
|
||
|
//a -= mouseScroll * mSpeed;
|
||
|
//a = Mathf.Clamp(a, minDistance, maxDistance);
|
||
|
//if (mouseScroll == 0 && distanceResult != 0)
|
||
|
//{
|
||
|
// a = distanceResult;
|
||
|
//}
|
||
|
|
||
|
//Debug.Log(transform.GetComponent<Camera>().orthographicSize + " / " + a + " = " + transform.GetComponent<Camera>().orthographicSize / a);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新鼠标左键按住状态下,鼠标移动操作开始传入的数据
|
||
|
/// </summary>
|
||
|
public void UpdateDragEnterData(Vector3 mousePos, bool draging)
|
||
|
{
|
||
|
mousePostion = mousePos;
|
||
|
dragStart = draging;
|
||
|
|
||
|
var screenSpace = GetComponent<Camera>().WorldToScreenPoint(target.position);
|
||
|
prevPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新鼠标左键按住状态下,鼠标移动操作过程中传入的数据
|
||
|
/// </summary>
|
||
|
/// <param name="mousePos"></param>
|
||
|
public void UpdateDragingData(Vector3 mousePos, bool draging)
|
||
|
{
|
||
|
mousePostion = mousePos;
|
||
|
|
||
|
dragStart = draging;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新鼠标左键按住状态下,鼠标移动操作结束传入的数据
|
||
|
/// </summary>
|
||
|
/// <param name="mousePos"></param>
|
||
|
public void UpdateDragExitData(Vector3 mousePos, bool draging)
|
||
|
{
|
||
|
prevPos = mousePos;
|
||
|
|
||
|
dragStart = draging;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 缩放,平移相机后,更新其位置,旋转状态
|
||
|
/// </summary>
|
||
|
private void UpdateCameraState()
|
||
|
{
|
||
|
Vector3 disVector = new Vector3(0.0f, distance, 0.0f);
|
||
|
position = disVector + target.position;
|
||
|
|
||
|
if (needDamping)
|
||
|
{
|
||
|
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
|
||
|
|
||
|
transform.GetComponent<Camera>().orthographicSize = distance;
|
||
|
//Debug.Log(transform.GetComponent<Camera>().orthographicSize);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
transform.position = position;
|
||
|
transform.GetComponent<Camera>().orthographicSize = distance;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 相机缩放
|
||
|
/// </summary>
|
||
|
private void UpdateCameraScale()
|
||
|
{
|
||
|
distance -= mouseScroll * mSpeed;
|
||
|
distance = Mathf.Clamp(distance, minDistance, maxDistance);
|
||
|
|
||
|
if (mouseScroll == 0 && distanceResult != 0)
|
||
|
{
|
||
|
distance = distanceResult;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 相机水平移动
|
||
|
/// </summary>
|
||
|
private void UpdateCameraTargetHorizontalPos()
|
||
|
{
|
||
|
if (mousePostion.x > Screen.width || mousePostion.y > Screen.height)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
var screenSpace = GetComponent<Camera>().WorldToScreenPoint(target.position);
|
||
|
|
||
|
var currPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z);
|
||
|
|
||
|
var prev = GetComponent<Camera>().ScreenToWorldPoint(prevPos);
|
||
|
var curr = GetComponent<Camera>().ScreenToWorldPoint(currPos);
|
||
|
|
||
|
var offset = (curr - prev);
|
||
|
|
||
|
offset.y = 0;
|
||
|
|
||
|
target.position += -offset;
|
||
|
|
||
|
//MessageDispatcher.SendMessage("UPDATE_FIREDEPOLY_OBJ_POS", new KeyValuePair<Vector3, Vector3>(prevPos, currPos));
|
||
|
prevPos = currPos;
|
||
|
}
|
||
|
}
|