티스토리 뷰

반응형

-dargSpeed를 통하여 드래그 속도를 조절가능

-loop 가능

-isplay를통한 제어

 

#이미지시퀀스 드래그 구현

 

#드래그 설정

 

 

 

#이미지 객체 드래그 영역 구현

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class dragArea : MonoBehaviour, IDragHandler
{
    public int dragSpeed = 100;
    public gofogoImageSequencer manager;

    int dragValue = 1;

    private void Start()
    {
        dragValue = dragSpeed;
    }

    public void OnDrag(PointerEventData eventData)
    {

        int currentPos = manager.currentPos;
        int index = manager.index;

        int position = (int)eventData.position.x;

        int pos = position - currentPos;
        dragValue--;
        if (dragValue <= 0)
        {
            if (pos > 0)
            {

                manager.index = index + 1;
                manager.PlayBack_Seq();

            }



            else
            {
                manager.index = index - 1;
                manager.PlayBack_Seq();
            }
            dragValue = dragSpeed;
        }
        manager.currentPos = position;

    }
}

 

 

 

 

#라이브러리 설정

#라이브러리 구현부

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class gofogoImageSequencer : MonoBehaviour
{
    public string resourceFolder;
    public string sequenceFolder;
    public float FPS = 25;
    public bool loadOnAwake = true;
    public bool playOnAwake = true;
    public bool loop;
    public int currentPos = 0;

    public RawImage rawImage;
    public float delta = 0;

    Texture2D[] sequence;

    public int index = 0;
    public bool isPlaying;

    bool isPaused;

    [SerializeField]
    float frame = 0;
    [SerializeField]
    bool isDrag = false;


    private void Update()
    {

        if (isPlaying)
        {
            delta += Time.deltaTime;
            frame = delta / (1f / FPS);
            index = Mathf.Clamp(Mathf.FloorToInt(frame), 0, sequence.Length);
            PlayBack();
        }
    }

    void Awake()
    {
        if (!loadOnAwake)
            return;
        LoadSequence(playOnAwake);
    }

    void PlayBack()
    {
        if (isPlaying)
        {
            if (sequence != null)
            {
                if (sequence.Length > 0)
                {




                    if (index >= sequence.Length)
                    {
                        if (loop)
                        {
                            delta = 0;
                            index = 0;
                            return;
                        }
                        else
                        {
                            index = 0;
                            delta = 0;
                            isPlaying = false;
                            return;
                        }
                    }



                    UpdateFrame();
                }
            }
        }
    }

    public void PlayBack_Seq()
    {
        if (isDrag)
        {
            if (sequence != null)
            {
                if (sequence.Length > 0)
                {
                    if (index >= sequence.Length)
                    {

                        index = 0;
                        delta = 0;
                        return;
                    }


                    if (index < 0)
                    {
                        index = 0;
                    }
                    UpdateFrame();
                }
            }
        }

    }

    void UpdateFrame()
    {
        rawImage.texture = sequence[index];


    }

    public void LoadSequence(bool play = false)
    {
        Object[] obj = Resources.LoadAll(sequenceFolder, typeof(Texture2D));
        Debug.Log(obj.Length);
        if (obj.Length > 0)
        {
            sequence = new Texture2D[obj.Length];
            for (int i = 0; i < obj.Length; i++)
            {
                sequence[i] = (Texture2D)obj[i];
            }
            index = 0;
            UpdateFrame();
            if (play) Play();
        }
    }

    #region Play Stop Pause Clear

    void Play()
    {
        if (!isPlaying)
        {
            index = 0;
            isPlaying = true;
        }
        else if (isPaused)
        {
            isPaused = false;
            isPlaying = true;
        }
    }

    void Pause(bool value)
    {
        isPaused = value;
        isPlaying = !value;
    }

    public void Stop()
    {
        isPlaying = false;
        isPaused = false;
        index = 0;
    }

    public void Stop(bool clear)
    {
        Stop();
        rawImage.texture = null;
    }
    public void Clear()
    {
        Stop();
        sequence = new Texture2D[0];
        rawImage.texture = null;
    }

    #endregion

}

 

#이미지시퀀스 #이미지드래그 #유니티 #unity #image sequence #image drag

반응형
댓글
반응형