150 lines
4.6 KiB
C#
150 lines
4.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using AibisDream;
|
|
using Cinemachine;
|
|
|
|
public class MouseFollowAndZoom : MonoBehaviour
|
|
{
|
|
public CinemachineVirtualCamera virtualCamera;
|
|
public GameObject targetIndicator; // UI 目标框
|
|
public TMP_Text targetNameText; // 显示目标名称的文本
|
|
public float zoomInScale = 1.5f; // 缩放倍数
|
|
public float zoomDuration = 1.0f; // 缩放持续时间(减慢)
|
|
public Vector2 sceneBoundsMin; // 场景边界的左下角
|
|
public Vector2 sceneBoundsMax; // 场景边界的右上角
|
|
private EyeSystem eyemanager;
|
|
private EyeTarget currentTarget; // 当前目标
|
|
private bool isActive = false;
|
|
private bool isLocked = false;
|
|
|
|
// 读写访问
|
|
public void SetIsActive(bool value)
|
|
{
|
|
isActive = value;
|
|
}
|
|
|
|
public void SetIsLock(bool value)
|
|
{
|
|
isLocked = value;
|
|
}
|
|
|
|
|
|
void Start()
|
|
{
|
|
targetIndicator.SetActive(false); // 初始时隐藏目标框
|
|
eyemanager = FindObjectOfType<EyeSystem>();
|
|
if (eyemanager == null)
|
|
{
|
|
Debug.LogError("EyeManager instance not found in the scene.");
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!isActive)
|
|
{
|
|
targetIndicator.SetActive(false);
|
|
return;
|
|
}
|
|
|
|
HandleTargetIndicator(); // 显示指示器
|
|
if (isLocked) return;
|
|
HandleMouseMovement();
|
|
UpdateCurrentTarget(); // 更新当前目标
|
|
}
|
|
|
|
private Vector3 currentOffset; // 当前的偏移量
|
|
|
|
void HandleMouseMovement()
|
|
{
|
|
|
|
}
|
|
|
|
void UpdateCurrentTarget()
|
|
{
|
|
// 获取鼠标位置
|
|
Vector3 mousePosition = Input.mousePosition;
|
|
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
|
|
RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero);
|
|
|
|
// 检查是否有目标
|
|
if (hit.collider != null && hit.collider.CompareTag("EyeTarget"))
|
|
{
|
|
currentTarget = hit.collider.GetComponent<EyeTarget>(); // 更新当前目标
|
|
|
|
// 处理鼠标点击事件
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
Debug.Log("点击目标触发了");
|
|
if (currentTarget != null)
|
|
{
|
|
eyemanager.SetTarget(currentTarget); // 设置目标
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("The collider does not have an EyeTarget component.");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
currentTarget = null; // 如果没有目标,设置为 null
|
|
}
|
|
}
|
|
|
|
void HandleTargetIndicator()
|
|
{
|
|
// if (currentTarget == null)
|
|
// {
|
|
// targetIndicator.SetActive(false); // 隐藏指示器
|
|
// return;
|
|
// }
|
|
|
|
// // 获取目标的 BoxCollider2D
|
|
// BoxCollider2D boxCollider = currentTarget.GetComponent<BoxCollider2D>();
|
|
// if (boxCollider != null)
|
|
// {
|
|
// Vector3 boxCenter = boxCollider.bounds.center;
|
|
// Vector3 boxSize = boxCollider.bounds.size;
|
|
|
|
// // 转换为屏幕坐标
|
|
// Vector3 screenCenter = viewCamera.WorldToScreenPoint(boxCenter);
|
|
// Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenter + boxSize) -
|
|
// viewCamera.WorldToScreenPoint(boxCenter);
|
|
|
|
// // 设置目标框位置和大小
|
|
// RectTransform indicatorRectTransform = targetIndicator.GetComponent<RectTransform>();
|
|
// indicatorRectTransform.position = screenCenter;
|
|
// indicatorRectTransform.sizeDelta = screenSize;
|
|
|
|
// // 设置目标名称
|
|
// targetNameText.text = currentTarget.name;
|
|
|
|
// // 显示指示器
|
|
// targetIndicator.SetActive(true);
|
|
// }
|
|
}
|
|
|
|
public void SetIndicatorTarget(EyeTarget target)
|
|
{
|
|
currentTarget = target;
|
|
}
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
// 设置 Gizmos 颜色
|
|
Gizmos.color = Color.red;
|
|
|
|
// 绘制场景边界
|
|
Vector3 bottomLeft = new Vector3(sceneBoundsMin.x, sceneBoundsMin.y, 0);
|
|
Vector3 topRight = new Vector3(sceneBoundsMax.x, sceneBoundsMax.y, 0);
|
|
|
|
// 绘制边界框
|
|
Gizmos.DrawLine(bottomLeft, new Vector3(bottomLeft.x, topRight.y, 0));
|
|
Gizmos.DrawLine(bottomLeft, new Vector3(topRight.x, bottomLeft.y, 0));
|
|
Gizmos.DrawLine(topRight, new Vector3(bottomLeft.x, topRight.y, 0));
|
|
Gizmos.DrawLine(topRight, new Vector3(topRight.x, bottomLeft.y, 0));
|
|
}
|
|
} |