using System.Collections.Generic; using System.Collections; using UnityEngine; using UnityEngine.UI; public class ContentManager : MonoBehaviour { [Header("Content Vieport")] public Image contentDisplay; public List contentPanels; [Header("Navigation Dots")] public GameObject dotsContainer; public GameObject dotPrefab; [Header("Pagination Buttons")] public Button nextButton; public Button prevButton; [Header("Page Settings")] public bool useTimer = false; public bool isLimitedSwipe = false; public float autoMoveTime = 5f; private float timer; public int currentIndex = 0; public float swipeThreshold = 50f; private Vector2 touchStartPos; // Reference to the RectTransform of the content area public RectTransform contentArea; void Start() { nextButton.onClick.AddListener(NextContent); prevButton.onClick.AddListener(PreviousContent); // Initialize dots InitializeDots(); // Display initial content ShowContent(); // Start auto-move timer if enabled if (useTimer) { timer = autoMoveTime; InvokeRepeating("AutoMoveContent", 1f, 1f); // Invoke every second to update the timer } } void InitializeDots() { // Create dots based on the number of content panels for (int i = 0; i < contentPanels.Count; i++) { GameObject dot = Instantiate(dotPrefab, dotsContainer.transform); Image dotImage = dot.GetComponent(); dotImage.color = (i == currentIndex) ? Color.white : Color.gray; dotImage.fillAmount = 0f; // Initial fill amount // You may want to customize the dot appearance and layout here } } void UpdateDots() { // Update the appearance of dots based on the current index for (int i = 0; i < dotsContainer.transform.childCount; i++) { Image dotImage = dotsContainer.transform.GetChild(i).GetComponent(); dotImage.color = (i == currentIndex) ? Color.white : Color.gray; float targetFillAmount = timer / autoMoveTime; StartCoroutine(SmoothFill(dotImage, targetFillAmount, 0.5f)); } } IEnumerator SmoothFill(Image image, float targetFillAmount, float duration) { float startFillAmount = image.fillAmount; float elapsedTime = 0f; while (elapsedTime < duration) { image.fillAmount = Mathf.Lerp(startFillAmount, targetFillAmount, elapsedTime / duration); elapsedTime += Time.deltaTime; yield return null; } image.fillAmount = targetFillAmount; // Ensure it reaches the exact target } void Update() { // Detect swipe input only within the content area DetectSwipe(); } void DetectSwipe() { if (Input.GetMouseButtonDown(0)) { touchStartPos = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { Vector2 touchEndPos = Input.mousePosition; float swipeDistance = touchEndPos.x - touchStartPos.x; // Check if the swipe is within the content area bounds if (Mathf.Abs(swipeDistance) > swipeThreshold && IsTouchInContentArea(touchStartPos)) { if (isLimitedSwipe && ((currentIndex == 0 && swipeDistance > 0) || (currentIndex == contentPanels.Count - 1 && swipeDistance < 0))) { // Limited swipe is enabled, and at the edge of content return; } if (swipeDistance > 0) { PreviousContent(); } else { NextContent(); } } } } // Check if the touch position is within the content area bounds bool IsTouchInContentArea(Vector2 touchPosition) { return RectTransformUtility.RectangleContainsScreenPoint(contentArea, touchPosition); } void AutoMoveContent() { timer -= 1f; // Decrease timer every second if (timer = 0 && newIndex < contentPanels.Count) { currentIndex = newIndex; ShowContent(); UpdateDots(); } } }
@Someone-fu7wz
5 ай бұрын
Awesome tutorial, thank you. But I'm not gonna lie but from 1:35 on your voice has this 😏😏😏 touch. Even pressing boring buttons sounds like an intense thing is going on😂
@UnityVista
5 ай бұрын
I don't have any issues, thanks for asking 😅. By the way, I highly recommend my second video because it has clean coding and amazing transitions, and it's also easy to understand. Please comment on what topic I should make the next video on. Thanks for watching! ❣️
@volpe768
8 ай бұрын
Great video, really helpfull! I have a question about the last sentence that you say "it worked also in a mobile app" but how it works if all the input are by mouse? Thanks for the answer
@UnityVista
8 ай бұрын
Right, but it has a swipe threshold that can also work on mobile devices, However i suggest using the new one because it has been coded cleanly, I had checked the code in this one, and there were many errors, but in the new one, there are no errors. kzitem.info/news/bejne/s6mwvJucq5OFlno
@CapeYa-jv4kz
4 ай бұрын
I can't swipe content with the cursor, I have to use a button, is there a solution?
@UnityVista
4 ай бұрын
Sorry for this not working, as it was just a prototype. If you want a polished version, please use this one kzitem.info/news/bejne/s6mwvJucq5OFlno. It is more polished, flexible, and everything works properly. If you encounter any issues after this, please let me know, but I hope there won't be any problems.
@CapeYa-jv4kz
4 ай бұрын
@@UnityVista thankyou😁
@sarkioricha1217
3 ай бұрын
Thanks for sharing, 🙏.
@UnityVista
3 ай бұрын
Watch my another carousel video for more polished version ❤ comment what next
@alexeysmirnov7874
2 ай бұрын
Was the text for this tutorial written by an LLM?
@UnityVista
2 ай бұрын
Yes,
@NatureRiders8107
8 ай бұрын
amazing tutorial but why the auto move function is not working
@UnityVista
8 ай бұрын
"Apologies, but the auto scroller isn't working. However, a new video is coming soon with all the errors fixed, and it's very good. Please wait until tomorrow; you'll get the new video with the best features. Thanks."
@UnityVista
8 ай бұрын
Check "Use Timer" bool is true
@n_mckean
10 ай бұрын
Why people are still making UI outside of UI Toolkit is beyond me. Devs need to move forward.
@UnityVista
10 ай бұрын
Sir, can you tell me what kind of tutorial you want so that I can help you?
@UnityVista
10 ай бұрын
I admit that my tutorial is not that good but I made it because it is a paid asset and I just tried.
@MrHwizard
Ай бұрын
Well, No World space (so no VR), No custom shaders, and elements are addressed by strings instead of object reference in code.
Пікірлер: 19