存档功能

This commit is contained in:
2025-01-08 11:41:20 +08:00
parent c67366664c
commit 43b99f98e7
49 changed files with 2498 additions and 5762 deletions
@@ -15,7 +15,9 @@ public class MouseFollowAndZoom : MonoBehaviour
public float zoomDuration = 1.0f; // 缩放持续时间(减慢)
public Vector2 sceneBoundsMin; // 场景边界的左下角
public Vector2 sceneBoundsMax; // 场景边界的右上角
public Camera viewCamera;
//private Camera MainCamera;
private Vector3 initialPosition;
private Vector3 zoomedInPosition; // 保存缩放时的相机位置
@@ -23,11 +25,11 @@ public class MouseFollowAndZoom : MonoBehaviour
private EyeSystem eyemanager;
private EyeTarget currentTarget; // 当前目标
private float viewOffset=40.3f;
private float viewOffset = 40.3f;
private bool isActive=false;
private bool isActive = false;
private bool isLocked=false;
private bool isLocked = false;
// 只读访问
public bool GetMyPrivateVariable()
@@ -40,6 +42,7 @@ public class MouseFollowAndZoom : MonoBehaviour
{
isActive = value;
}
public void SetIsLock(bool value)
{
isLocked = value;
@@ -48,10 +51,9 @@ public class MouseFollowAndZoom : MonoBehaviour
void Start()
{
//MainCamera = Camera.main;
initialPosition = transform.position;
targetIndicator.SetActive(false); // 初始时隐藏目标框
eyemanager=FindObjectOfType<EyeSystem>();
eyemanager = FindObjectOfType<EyeSystem>();
if (eyemanager == null)
{
Debug.LogError("EyeManager instance not found in the scene.");
@@ -62,9 +64,9 @@ public class MouseFollowAndZoom : MonoBehaviour
{
if (!isActive)
{
targetIndicator.SetActive(false);
targetIndicator.SetActive(false);
return;
}
}
HandleTargetIndicator(); // 显示指示器
if (isLocked) return;
@@ -72,70 +74,72 @@ public class MouseFollowAndZoom : MonoBehaviour
UpdateCurrentTarget(); // 更新当前目标
}
private Vector3 currentOffset; // 当前的偏移量
private Vector3 currentOffset; // 当前的偏移量
void HandleMouseMovement()
{
// 获取鼠标位置
Vector3 mousePosition = Input.mousePosition;
Vector3 worldMousePosition = viewCamera.ScreenToWorldPoint(mousePosition);
worldMousePosition.z = 0; // 确保 z 为 0
// 计算目标偏移量并反向
Vector3 targetOffset = (viewCamera.transform.position - worldMousePosition) * parallaxEffectMultiplier;
targetOffset.x = Mathf.Clamp(targetOffset.x, -maxOffset.x, maxOffset.x);
targetOffset.y = Mathf.Clamp(targetOffset.y, -maxOffset.y, maxOffset.y);
// 平滑处理偏移量
float smoothFactor = 0.01f; // 平滑系数,值越小平滑效果越强,响应越慢
currentOffset = Vector3.Lerp(currentOffset, targetOffset, smoothFactor);
// 计算目标位置
Vector3 targetPosition = initialPosition + currentOffset;
// 限制目标位置在场景边界范围内
targetPosition.x = Mathf.Clamp(targetPosition.x, sceneBoundsMin.x, sceneBoundsMax.x);
targetPosition.y = Mathf.Clamp(targetPosition.y, sceneBoundsMin.y, sceneBoundsMax.y);
// 使用插值(Lerp)平滑更新位置
float followSpeed = 0.1f; // 跟随速度,值越小延迟越大
Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, followSpeed);
// 应用平滑后的位置
transform.position = smoothedPosition;
}
void UpdateCurrentTarget()
{
// 获取鼠标位置
Vector3 mousePosition = Input.mousePosition;
mousePosition.x += viewOffset; // 添加偏移量
Vector3 worldPosition = viewCamera.ScreenToWorldPoint(mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero);
// 检查是否有目标
if (hit.collider != null && hit.collider.CompareTag("EyeTarget"))
void HandleMouseMovement()
{
currentTarget = hit.collider.GetComponent<EyeTarget>(); // 更新当前目标
// 获取鼠标位置
Vector3 mousePosition = Input.mousePosition;
Vector3 worldMousePosition = viewCamera.ScreenToWorldPoint(mousePosition);
worldMousePosition.z = 0; // 确保 z 为 0
// 处理鼠标点击事件
if (Input.GetMouseButtonDown(0))
// 计算目标偏移量并反向
Vector3 targetOffset = (viewCamera.transform.position - worldMousePosition) * parallaxEffectMultiplier;
targetOffset.x = Mathf.Clamp(targetOffset.x, -maxOffset.x, maxOffset.x);
targetOffset.y = Mathf.Clamp(targetOffset.y, -maxOffset.y, maxOffset.y);
// 平滑处理偏移量
float smoothFactor = 0.01f; // 平滑系数,值越小平滑效果越强,响应越慢
currentOffset = Vector3.Lerp(currentOffset, targetOffset, smoothFactor);
// 计算目标位置
Vector3 targetPosition = initialPosition + currentOffset;
// 限制目标位置在场景边界范围内
targetPosition.x = Mathf.Clamp(targetPosition.x, sceneBoundsMin.x, sceneBoundsMax.x);
targetPosition.y = Mathf.Clamp(targetPosition.y, sceneBoundsMin.y, sceneBoundsMax.y);
// 使用插值(Lerp)平滑更新位置
float followSpeed = 0.1f; // 跟随速度,值越小延迟越大
Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, followSpeed);
// 应用平滑后的位置
transform.position = smoothedPosition;
}
void UpdateCurrentTarget()
{
// 获取鼠标位置
Vector3 mousePosition = Input.mousePosition;
mousePosition.x += viewOffset; // 添加偏移量
Vector3 worldPosition = viewCamera.ScreenToWorldPoint(mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPosition, Vector2.zero);
// 检查是否有目标
if (hit.collider != null && hit.collider.CompareTag("EyeTarget"))
{
Debug.Log("点击目标触发了");
if (currentTarget != null)
currentTarget = hit.collider.GetComponent<EyeTarget>(); // 更新当前目标
// 处理鼠标点击事件
if (Input.GetMouseButtonDown(0))
{
eyemanager.SetTarget(currentTarget); // 设置目标
}
else
{
Debug.LogWarning("The collider does not have an EyeTarget component.");
Debug.Log("点击目标触发了");
if (currentTarget != null)
{
eyemanager.SetTarget(currentTarget); // 设置目标
}
else
{
Debug.LogWarning("The collider does not have an EyeTarget component.");
}
}
}
else
{
currentTarget = null; // 如果没有目标,设置为 null
}
}
else
{
currentTarget = null; // 如果没有目标,设置为 null
}
}
void HandleTargetIndicator()
{
if (currentTarget == null)
@@ -153,7 +157,8 @@ void UpdateCurrentTarget()
// 转换为屏幕坐标
Vector3 screenCenter = viewCamera.WorldToScreenPoint(boxCenter);
Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenter + boxSize) - viewCamera.WorldToScreenPoint(boxCenter);
Vector2 screenSize = viewCamera.WorldToScreenPoint(boxCenter + boxSize) -
viewCamera.WorldToScreenPoint(boxCenter);
// 设置目标框位置和大小
RectTransform indicatorRectTransform = targetIndicator.GetComponent<RectTransform>();
@@ -167,10 +172,10 @@ void UpdateCurrentTarget()
targetIndicator.SetActive(true);
}
}
public void SetIndicatorTarget(EyeTarget target)
{
currentTarget=target;
currentTarget = target;
}
@@ -187,7 +192,7 @@ void UpdateCurrentTarget()
// {
// Debug.Log(hit.collider.gameObject.name);
// }
// if (hit.collider != null && hit.collider.CompareTag("EyeTarget")) // 检查是否是目标 Sprite
// {
@@ -253,11 +258,11 @@ void UpdateCurrentTarget()
// 绘制场景边界
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));
}
}
}