110 lines
2.9 KiB
C#
110 lines
2.9 KiB
C#
|
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><>Сxֵ
|
|||
|
public float maxX = 500f; // <20><><EFBFBD><EFBFBD>xֵ
|
|||
|
|
|||
|
// 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);
|
|||
|
|
|||
|
mainSceneRectTransform.anchoredPosition = newPos; // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>ê<EFBFBD><C3AA>λ<EFBFBD><CEBB>
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
m_oneFingerDragStartPos = fingerPos;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// <20><>Ļ<EFBFBD><C4BB><EFBFBD>껻<EFBFBD><EABBBB><EFBFBD><EFBFBD>3D<33><44><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <param name="screenPos"><3E><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD></param>
|
|||
|
/// <returns></returns>
|
|||
|
Vector3 GetWorldPos(Vector2 screenPos)
|
|||
|
{
|
|||
|
return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(m_cam.transform.position.z)));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|