Tuesday, April 16, 2024
HomeSample Page

Sample Page Title


I wish to make a slide of pictures that change routinely by a timer with out urgent any button for instance each 5 seconds the picture adjustments to a different one. I’ve a script but it surely solely adjustments them when urgent a button I appeared in all places however I discovered no data about tips on how to do it with time as an alternative of sliding buttons.

that is the script I am utilizing now:

utilizing UnityEngine;
utilizing UnityEngine.UI;
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing System.Linq;

public class Slidescript : MonoBehaviour
{
    //All _slides within the venture, minus the _fadeSlide
    [SerializeField]
    personal Checklist<Rework> _slides = default;

    //The panel that overlays all _slides and adjustments from clear to black
    [SerializeField]
    personal Picture _fadeSlide = default;

    [Header("Config Values")]
    [SerializeField, Tooltip("The duration (in seconds) over which the fade slide will fade in / out")]
    personal float _fadeDuration = 0.75f;

    [SerializeField, Tooltip("All key codes that will move to the next slide if pressed")]
    personal KeyCode[] _nextSlideKeyCodes = {
        KeyCode.D,
        KeyCode.RightArrow,
        KeyCode.Area
    };

    [SerializeField, Tooltip("All key codes that will move to the previous slide if pressed")]
    personal KeyCode[] _previousSlideKeyCodes = {
        KeyCode.A,
        KeyCode.LeftArrow,
    };

    //The slide we're at present viewing
    personal int _currentSlide = -1;

    // Whether or not the fade slide is at present fading
    personal bool _isTransitioning = false;

    personal void Begin()
    {
        // Set our fade to black slide to black in order that the viewers cannot see the primary slide
        _fadeSlide.coloration = Colour.black;
    }

    public void Replace()
    {
        //Examine for arrow keys/house bar down so we are able to fade the fadePanel accordingly
        ListenForInput();
    }

    personal void ListenForInput()
    {
        // Ignore enter if we're in the midst of a transition
        if (_isTransitioning)
        {
            return;
        }

        //If we strike the house bar or proper arrow key, proceed to the subsequent slide
        if (_nextSlideKeyCodes.Any(Enter.GetKeyDown))
        {
            NextSlide();
        }

        //If we strike the left arrow key, return to the earlier slide
        if (_previousSlideKeyCodes.Any(Enter.GetKeyDown))
        {
            PreviousSlide();
        }

        //If we strike the Esc key, give up the applying
        if (Enter.GetKeyDown(KeyCode.Escape))
        {
            Utility.Give up();
        }
    }

    personal void NextSlide()
    {
        //If we're on the final slide already..
        if (_currentSlide == _slides.Rely - 1)
        {
            // Exit early
            return;
        }

        //Increment the slide rely
        _currentSlide++;

        // Transition to the subsequent slide
        StartCoroutine(SlideTransition());
    }


    personal IEnumerator SlideTransition()
    {
        // Mark our fading slide as at present fading
        _isTransitioning = true;

        // Fade to black
        yield return StartCoroutine(FadeToTargetColor(targetColor: Colour.black));

        // Set solely our present slide lively - and all others inactive
        _slides.ForEach(slide => slide.gameObject.SetActive(_slides.IndexOf(slide) == _currentSlide));

        // Fade to clear
        yield return StartCoroutine(FadeToTargetColor(targetColor: Colour.clear));

        // Mark our fading slide as not fading
        _isTransitioning = false;
    }

    personal void PreviousSlide()
    {
        //If the present slide is the very first slide, ignore the remainder of this methodology
        if (_currentSlide == 0)
        {
            return;
        }

        //Decrement the present slide
        _currentSlide--;

        // Transition to the earlier slide
        StartCoroutine(SlideTransition());
    }

    personal IEnumerator FadeToTargetColor(Colour targetColor)
    {
        // The overall quantity of seconds that has elapsed because the begin of our lerp sequence
        float elapsedTime = 0.0f;

        // The colour of our fade panel in the beginning of the lerp sequence
        Colour startColor = _fadeSlide.coloration;

        // Whereas we've not reached the top of the lerp sequence..
        whereas (elapsedTime < _fadeDuration)
        {
            // Enhance our elapsed time
            elapsedTime += Time.deltaTime;

            // Carry out a lerp to our goal coloration
            _fadeSlide.coloration = Colour.Lerp(startColor, targetColor, elapsedTime / _fadeDuration);

            // Await the subsequent body
            yield return null;
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments