Question by
Radical_boy · Sep 22, 2021 at 12:50 PM ·
c#inputdragscrollview
Unity Scroll View blocks IDragHandler input?
I use a script for my mobile app to manage some pages and them being scrolled with touch input from IDragHandler and IEndDragHandler
But on one of the pages i have a scroll view that blocks input from the Script, and i can only scroll in the places where the scroll view Raycast Target doesn't hit.
Is there a way to make this Script work even withmy scroll view?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class PageSwipe : MonoBehaviour, IDragHandler, IEndDragHandler
{
public Vector3 panelLocation;
public float percentThreshold = 0.2f;
public float easing = 0.1f;
public int totalPages = 2;
public int currentPage = 1;
public float P;
public float Y;
public GameObject Slide;
public Vector3 newLocation;
// Start is called before the first frame update
void Start()
{
panelLocation = transform.GetComponent<RectTransform>().localPosition;
}
void Update()
{
if(PlayerPrefs.GetInt("SildeTutorial")==1)
{
Slide.gameObject.SetActive(false);
}
}
public void OnDrag(PointerEventData data)
{
float difference = data.pressPosition.x - data.position.x;
Y = difference;
if (difference < 0 && currentPage == 1)
{
return;
}
if (difference < -1100 && currentPage == 2)
{
return;
}
if (difference > 0 && currentPage == 3)
{
return;
}
if (difference > 1100 && currentPage == 2)
{
return;
}
transform.GetComponent<RectTransform>().localPosition = panelLocation - new Vector3(difference, 0, 0);
}
public void OnEndDrag(PointerEventData data)
{
float percentage = (data.pressPosition.x - data.position.x) / Screen.width;
P = percentage;
if (Mathf.Abs(percentage) >= percentThreshold)
{
newLocation = panelLocation;
if (percentage > 0 && currentPage < totalPages)
{
currentPage++;
newLocation += new Vector3(-1920, 0, 0);
}
else if (percentage < 0 && currentPage > 1)
{
currentPage--;
newLocation += new Vector3(1920, 0, 0);
}
StartCoroutine(SmoothMove(transform.GetComponent<RectTransform>().localPosition, newLocation, easing));
panelLocation = newLocation;
}
else
{
StartCoroutine(SmoothMove(transform.GetComponent<RectTransform>().localPosition, panelLocation, easing));
}
PlayerPrefs.SetInt("SildeTutorial", 1);
}
public void SwipeToPRofile()
{
currentPage = 1;
LeanTween.moveLocalX(gameObject, 1920f, 0.3f);
panelLocation.x = 1920f;
}
public void SwipeToChat()
{
currentPage = 3;
LeanTween.moveLocalX(gameObject, -1920f, 0.3f);
panelLocation.x = -1920f;
}
IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds)
{
float t = 0f;
while (t <= 1.0)
{
t += Time.deltaTime / seconds;
transform.GetComponent<RectTransform>().localPosition = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t));
yield return null;
}
}
public void MovePageOnButtonPress()
{
Vector3 newLocation = panelLocation;
newLocation += new Vector3(Screen.width, 0, 0);
StartCoroutine(SmoothMove(transform.position, newLocation, easing));
currentPage = 1;
}
}
Comment