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

132 lines
3.7 KiB
C#
Raw Normal View History

2024-11-25 23:46:35 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sceneContorl : MonoBehaviour
{
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>();
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
// <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EAB0B4>ʱ
if (Input.GetMouseButtonDown(0))
{
m_fingerDown = true;
m_oneFingerDragStartPos = Input.mousePosition; // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʼλ<CABC><CEBB>
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD>ʱ
if (Input.GetMouseButtonUp(0))
{
m_fingerDown = false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD>
if (m_fingerDown)
{
HandleFingerDragMove(Input.mousePosition);
}
#else
// <20>ƶ<EFBFBD><C6B6>ˣ<EFBFBD><CBA3><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
if (Input.touchCount == 1)
{
Touch touch = Input.touches[0];
// <20><>ָ<EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD>Ļʱ
if (touch.phase == TouchPhase.Began)
{
m_fingerDown = true;
m_oneFingerDragStartPos = touch.position; // <20><>ȡ<EFBFBD><C8A1>ָ<EFBFBD><D6B8><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʼλ<CABC><CEBB>
}
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ļ<EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD>ʱ
else if (touch.phase == TouchPhase.Moved)
{
HandleFingerDragMove(touch.position); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>϶<EFBFBD>
}
}
else
{
m_fingerDown = false;
}
#endif
}
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
// <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>
private float GetClosestTargetValue(float currentX)
2024-11-25 23:46:35 +08:00
{
2024-11-26 00:39:25 +08:00
float closestValue = targetPositions[0]; // Ĭ<><C4AC>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB>ֵ
float minDistance = Mathf.Abs(currentX - targetPositions[0]); // <20><>ʼ<EFBFBD><CABC>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB>Ŀ<EFBFBD><C4BF><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
2024-11-25 23:46:35 +08:00
2024-11-26 00:39:25 +08:00
// <20><><EFBFBD><EFBFBD> targetPositions <20><><EFBFBD><EFBFBD><E9A3AC><EFBFBD><EFBFBD><EFBFBD>뵱ǰλ<C7B0>õľ<C3B5><C4BE><EFBFBD>
for (int i = 1; i < targetPositions.Length; i++)
{
float distance = Mathf.Abs(currentX - targetPositions[i]); // <20><><EFBFBD>㵱ǰλ<C7B0><CEBB><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ľ<EFBFBD><C4BE>Ծ<EFBFBD><D4BE><EFBFBD>
if (distance < minDistance) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>С
{
closestValue = targetPositions[i]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
minDistance = distance; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
}
}
2024-11-25 23:46:35 +08:00
2024-11-26 00:39:25 +08:00
return closestValue; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ֵ
}
2024-11-25 23:46:35 +08:00
}