using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class NovelScrollViewScript : MonoBehaviour, IBeginDragHandler, IEndDragHandler { // Get Component ScrollRect rect; public int totalCount; public BookAudioPlayer m_bookAudioPlayerPrefabs; public Transform m_bookAudioPlayerParent; public List m_bookAudioList; private float[] posArray; private float targetPos; private int targetIndex = 0; private bool isDrag = false; private bool isStartDrag = false; public void OnBeginDrag(PointerEventData eventData) { isDrag = true; isStartDrag = true; m_bookAudioList[targetIndex].Pause(); } public void OnEndDrag(PointerEventData eventData) { isDrag = false; Vector2 pos = rect.normalizedPosition; float x = Mathf.Abs(pos.x - posArray[0]); targetIndex = 0; for (int i = 1; i < totalCount; ++i) { float tmp = Mathf.Abs(pos.x - posArray[i]); if (tmp < x) { x = tmp; targetIndex = i; } } targetPos = posArray[targetIndex]; } public void Init(List puzzleNovels) { for(int j=0;j(); totalCount = puzzleNovels.Count; posArray = new float[totalCount]; for (int i = 0; i < totalCount-1; i++) { posArray[i] = (float)i / (totalCount-1); } posArray[totalCount - 1] = 1.0f; for(int j=0;j(); currPlayer.Init(puzzleNovels[j]); m_bookAudioList.Add(currPlayer); } m_bookAudioPlayerParent.GetComponent().sizeDelta = new Vector2(Screen.width*puzzleNovels.Count,Screen.height); } // Update is called once per frame void Update() { if (!isDrag) { rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targetPos, Time.deltaTime * 8); if(rect.horizontalNormalizedPosition-targetPos<0.01f && isStartDrag) { m_bookAudioList[targetIndex].Resume(); isStartDrag = false; } } } }