Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

게임 프로그래밍

[유니티] 스크롤뷰 하나씩 넘기기 본문

프로그래밍/유니티

[유니티] 스크롤뷰 하나씩 넘기기

Junwe 2020. 1. 15. 00:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
/// <summary>
/// Snap a scroll rect to its children items. All self contained.
/// Note: Only supports 1 direction
/// </summary>
public class DragSnapper : UIBehaviour, IEndDragHandler, IBeginDragHandler
{
    public ScrollRect scrollRect; // the scroll rect to scroll
    public SnapDirection direction; // the direction we are scrolling
    public int itemCount; // how many items we have in our scroll rect
 
    public int CurrentitemCount;
 
    public AnimationCurve curve = AnimationCurve.Linear(0f, 0f, 1f, 1f); // a curve for transitioning in order to give it a little bit of extra polish
    public float speed; // the speed in which we snap ( normalized position per second? )
 
    public void OnBeginDrag(PointerEventData eventData)
    {
        StopCoroutine(SnapRect(direction == SnapDirection.Horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition)); // if we are snapping, stop for the next input
    }
 
    public void OnEndDrag(PointerEventData eventData)
    {
        StartCoroutine(SnapRect(direction == SnapDirection.Horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition)); // simply start our coroutine ( better than using update )
    }
 
    public void NextSnap()
    {
        float nextStartNormal = direction == SnapDirection.Horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition;
        nextStartNormal += (1f / (float)(itemCount - 1)) / 1.5f;
 
        StartCoroutine(SnapRect(nextStartNormal));
    }
 
    public int GetCurrentitemCount()
    {
        float delta = 1f / (float)(itemCount - 1);
        int count = Mathf.RoundToInt(scrollRect.horizontalNormalizedPosition / delta);
 
        if (count <= 0)
            count = 0;
        if (count >= itemCount - 1)
            count = itemCount - 1;
 
        return count;
    }
 
    public void PreviousSnap()
    {
        float nextStartNormal = direction == SnapDirection.Horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition;
        nextStartNormal -= (1f / (float)(itemCount - 1)) / 1.5f;
 
        StartCoroutine(SnapRect(nextStartNormal));
    }
 
    private IEnumerator SnapRect(float startNormal)
    {
        if (scrollRect == null)
            throw new System.Exception("Scroll Rect can not be null");
        if (itemCount == 0)
            throw new System.Exception("Item count can not be zero");
 
        //float startNormal = direction == SnapDirection.Horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition; // find our start position
        float delta = 1f / (float)(itemCount - 1); // percentage each item takes
        int target = Mathf.RoundToInt(startNormal / delta); // this finds us the closest target based on our starting point
        float endNormal = delta * target; // this finds the normalized value of our target
        float duration = Mathf.Abs((endNormal - startNormal) / speed); // this calculates the time it takes based on our speed to get to our target
 
        float timer = 0f; // timer value of course
        while (timer < 1f) // loop until we are done
        {
            timer = Mathf.Min(1f, timer + Time.deltaTime / duration); // calculate our timer based on our speed
            float value = Mathf.Lerp(startNormal, endNormal, curve.Evaluate(timer)); // our value based on our animation curve, cause linear is lame
 
            if (direction == SnapDirection.Horizontal) // depending on direction we set our horizontal or vertical position
                scrollRect.horizontalNormalizedPosition = value;
            else
                scrollRect.verticalNormalizedPosition = value;
 
            yield return new WaitForEndOfFrame(); // wait until next frame
        }
    }
}
 
// The direction we are snapping in
public enum SnapDirection
{
    Horizontal,
    Vertical,
}
cs

스크롤뷰에서 요소를 하나씩 넘기는 코드. 이동 할때 AnimationCurve를 넣어서 사용할수도 있다.

Comments