86 lines
2.6 KiB
Plaintext
86 lines
2.6 KiB
Plaintext
|
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<BookAudioPlayer> 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<PuzzleNovel> puzzleNovels)
|
||
|
{
|
||
|
for(int j=0;j<m_bookAudioList.Count;j++)
|
||
|
{
|
||
|
Destroy(m_bookAudioList[j].gameObject);
|
||
|
}
|
||
|
m_bookAudioList.Clear();
|
||
|
rect = GetComponent<ScrollRect>();
|
||
|
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<puzzleNovels.Count;j++)
|
||
|
{
|
||
|
GameObject go = GameObject.Instantiate(m_bookAudioPlayerPrefabs.gameObject,m_bookAudioPlayerParent);
|
||
|
BookAudioPlayer currPlayer = go.GetComponent<BookAudioPlayer>();
|
||
|
currPlayer.Init(puzzleNovels[j]);
|
||
|
m_bookAudioList.Add(currPlayer);
|
||
|
}
|
||
|
m_bookAudioPlayerParent.GetComponent<RectTransform>().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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|