Unity ScrollViewを左右にドラッグしてページをめくる
ScrollViewでは、左右にドラッグしてページをめくることができます.微信のように、左右にドラッグすると上下にドラッグできないし、上下にドラッグすると左右にドラッグできない.そして左右に引っ張ると弾力が出てくるので合わせます
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollPagaUtil : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
bool isPressing;
public ScrollRect sr;
Vector2 deltaPos;
public void OnBeginDrag(PointerEventData eventData)
{
//throw new System.NotImplementedException();
if (moving != null) StopCoroutine(moving);
}
public void OnEndDrag(PointerEventData eventData)
{
//throw new System.NotImplementedException();
isPressing = false;
if (moving != null) StopCoroutine(moving);
Move();
}
private void OnApplicationFocus(bool focus)
{
if (!focus)
{
isPressing = false;
MoveNow();
}
}
public void OnDrag(PointerEventData eventData)
{
Vector2 drag = eventData.delta;
if(Mathf.Abs(drag.y) > 1f && !isPressing)
{
sr.vertical = true;
sr.horizontal = false;
sr.movementType = ScrollRect.MovementType.Elastic;
sr.inertia = true;
isPressing = true;
}
else if (Mathf.Abs(drag.x) > 1f && !isPressing)
{
sr.horizontal = true;
sr.vertical = false;
sr.movementType = ScrollRect.MovementType.Clamped;
sr.inertia = false;
isPressing = true;
}
}
//-------------------------------------------------------------
public GameObject[] activeObject;
public GameObject[] disActiveObject;
public Transform[] contants;
public Transform center;
public Transform target;
Coroutine moving;
//
Transform GetNearContant()
{
float min = 9999;
Transform near = null;
foreach (Transform trans in contants)
{
float x = trans.position.x - center.transform.position.x;
if (Mathf.Abs(x) < min)
{
min = Mathf.Abs(x);
near = trans;
}
}
return near;
}
//
void SetButtonActive(int index)
{
for(int i =0;i< activeObject.Length; i++)
{
activeObject[i].SetActive(i == index);
disActiveObject[i].SetActive(i != index);
}
}
//
void SetButtonActive(Transform obj)
{
for(int i = 0;i< contants.Length; i++)
{
if(contants[i] == obj)
{
SetButtonActive(i);
return;
}
}
}
//
public void MoveToFirst()
{
if (moving != null) StopCoroutine(moving);
Transform near = contants[0];
SetButtonActive(0);
Vector3 pos = target.transform.localPosition;
pos.x = -near.localPosition.x;
target.transform.localPosition = pos;
}
//
public void MoveTo(int index)
{
if (moving != null) StopCoroutine(moving);
Transform near = contants[index];
SetButtonActive(index);
Vector3 pos = target.transform.localPosition;
pos.x = -near.localPosition.x;
target.transform.localPosition = pos;
}
//
void Move()
{
if (moving != null) StopCoroutine(moving);
Transform near = GetNearContant();
float xPos = -near.localPosition.x;
SetButtonActive(near);
moving = StartCoroutine(_MoveTo(xPos));
}
//
void MoveNow()
{
if (moving != null) StopCoroutine(moving);
Transform near = GetNearContant();
Vector3 pos = target.transform.localPosition;
pos.x = -near.localPosition.x;
SetButtonActive(near);
target.transform.localPosition = pos;
}
//
IEnumerator _MoveTo(float x)
{
float from = target.localPosition.x;
float to = x;
float timer = 0;
float time = 0.25f;
while (timer < time)
{
timer += Time.deltaTime;
float t = timer / time;
float curX = Mathf.Lerp(from, to, t);
Vector3 pos = target.localPosition;
pos.x = curX;
target.localPosition = pos;
yield return null;
}
moving = null;
}
}