AR_Celement/Assets/Scripts/SwipeScript.cs
2023-01-13 11:48:42 +01:00

79 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwipeScript : MonoBehaviour
{
public Image imageText;
public List<Sprite> lengths;
public int numberImage;
public int lengthTexts;
public Button arrowLeft;
public Button arrowRight;
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
// Start is called before the first frame update
void Start()
{
Button btnLeft = arrowLeft.GetComponent<Button>();
Button btnRight = arrowRight.GetComponent<Button>();
arrowLeft.gameObject.SetActive(false);
btnLeft.onClick.AddListener(PreviousText);
btnRight.onClick.AddListener(NextText);
imageText.sprite = lengths[0];
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
startTouchPosition = Input.GetTouch(0).position;
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){
endTouchPosition = Input.GetTouch(0).position;
if(endTouchPosition.x < startTouchPosition.x){
NextText();
}
if(endTouchPosition.x > startTouchPosition.x){
PreviousText();
}
}
}
private void NextText(){
if(numberImage < lengthTexts)
{
numberImage ++;
if(numberImage == lengthTexts){
arrowRight.gameObject.SetActive(false);
}else{
arrowRight.gameObject.SetActive(true);
}
if(numberImage != 0){
arrowLeft.gameObject.SetActive(true);
}
imageText.sprite = lengths[numberImage];
}
}
private void PreviousText(){
if(numberImage > 0) {
numberImage --;
if(numberImage == 0){
arrowLeft.gameObject.SetActive(false);
}else{
arrowLeft.gameObject.SetActive(true);
}
if(numberImage != lengthTexts){
arrowRight.gameObject.SetActive(true);
}
imageText.sprite = lengths[numberImage];
}
}
}