_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Scene_main/sceneContorl.cs

100 lines
2.9 KiB
C#
Raw Normal View History

2024-11-25 23:46:35 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-11-26 02:59:54 +08:00
using UnityEngine.EventSystems;
2024-11-25 23:46:35 +08:00
2024-11-26 02:59:54 +08:00
public class sceneContorl : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
2024-11-25 23:46:35 +08:00
{
public Camera m_cam;
public GameObject mainScene;
private bool m_fingerDown = false;
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ʼλ<CABC><CEBB>
private Vector2 m_oneFingerDragStartPos;
// UIԪ<49>ص<EFBFBD> RectTransform
private RectTransform mainSceneRectTransform;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
private float sensitivity = 0.5f; // <20><><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>Сֵ<D0A1><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
public float minX = -500f; // <20><>С
public float maxX = 500f; // <20><><EFBFBD><EFBFBD>
2024-11-26 00:39:25 +08:00
[Header("<22><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>")]
public float[] targetPositions = new float[] { 0f, -2100f, -3760f, -5350f };
// <20><><EFBFBD><EFBFBD> mainBTN <20>ű<EFBFBD>
public mainBTN mainBtnScript;
2024-11-25 23:46:35 +08:00
// Start is called before the first frame update
void Start()
{
mainSceneRectTransform = mainScene.GetComponent<RectTransform>();
}
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
2024-11-26 02:59:54 +08:00
2024-11-25 23:46:35 +08:00
// <20><><EFBFBD>㵱ǰ֡<C7B0>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>򣬱<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD>
Vector3 moveDelta = fingerPos - m_oneFingerDragStartPos; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD><CEBB> - <20><>ʼλ<CABC><CEBB>
Vector3 newPos = mainSceneRectTransform.anchoredPosition;
// <20><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>x<EFBFBD><78>λ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Χ<EFBFBD><CEA7>
newPos.x = newPos.x + (moveDelta.x * sensitivity);
// <20><><EFBFBD><EFBFBD>x<EFBFBD><78>λ<EFBFBD><CEBB><EFBFBD><EFBFBD> minX <20><> maxX ֮<><D6AE>
newPos.x = Mathf.Clamp(newPos.x, minX, maxX);
2024-11-26 00:39:25 +08:00
mainSceneRectTransform.anchoredPosition = newPos; // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>ê<EFBFBD><C3AA>λ<EFBFBD><CEBB>
2024-11-25 23:46:35 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_oneFingerDragStartPos = fingerPos;
}
2024-11-26 00:39:25 +08:00
2024-11-25 23:46:35 +08:00
/// <summary>
2024-11-26 00:39:25 +08:00
/// <20><><EFBFBD>㵱ǰλ<C7B0><CEBB><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD><D3A6>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>ֵ.
2024-11-25 23:46:35 +08:00
/// </summary>
2024-11-26 00:39:25 +08:00
/// <param name="currentX"><3E><>ǰX<C7B0><58><EFBFBD><EFBFBD></param>
/// <returns><3E><><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>ֵ</returns>
2024-11-26 02:59:54 +08:00
float FindClosestNumber(float target)
2024-11-25 23:46:35 +08:00
{
2024-11-26 02:59:54 +08:00
float closestNumber = targetPositions[0]; // Ĭ<>ϵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float minDifference = Mathf.Abs(target - closestNumber); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD>ֵ
2024-11-25 23:46:35 +08:00
2024-11-26 02:59:54 +08:00
foreach (float number in targetPositions)
2024-11-26 00:39:25 +08:00
{
2024-11-26 02:59:54 +08:00
float difference = Mathf.Abs(target - number); // <20><><EFBFBD>㵱ǰ<E3B5B1><C7B0><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD>ֵ
if (difference < minDifference) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ֵ<EFBFBD><D6B5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-26 00:39:25 +08:00
{
2024-11-26 02:59:54 +08:00
closestNumber = number;
minDifference = difference;
2024-11-26 00:39:25 +08:00
}
}
2024-11-26 02:59:54 +08:00
return closestNumber;
}
public void OnDrag(PointerEventData eventData)
{
HandleFingerDragMove(eventData.position);
}
public void OnBeginDrag(PointerEventData eventData)
{
m_oneFingerDragStartPos = Input.mousePosition; // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʼλ<CABC><CEBB>
}
public void OnEndDrag(PointerEventData eventData)
{
mainBtnScript.MovePanel(FindClosestNumber(mainSceneRectTransform.anchoredPosition.x));
2024-11-26 00:39:25 +08:00
}
2024-11-25 23:46:35 +08:00
}