using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //Handle Objects in Queue public class ObjectQ : MonoBehaviour { //all objects in the Q(ueue) are stored here [SerializeField] private List queue = new List(); [SerializeField] private Material material; private QableObject currentObject = null; private int currentIndex = 0; private State currentState = State.Changing; //switch to next or previous object in Q public void Change(bool previous = false) { if(queue.Count < 2 || currentState != State.ViewMode) return; currentIndex = previous ? currentIndex - 1 : currentIndex + 1; currentIndex = (currentIndex + queue.Count) % queue.Count; StartCoroutine(ShowNext()); currentState = State.Changing; } //add Object to Q public void Add(GameObject gameObject) { var o = gameObject.AddComponent(); gameObject.transform.SetParent(transform); gameObject.transform.position = transform.position; o.renderer.material = material; queue.Add(o); //if no object is currently visible then show the first object in Q if(currentObject == null) StartCoroutine(ShowNext()); } //Show next object from currentIndex private IEnumerator ShowNext() { if(currentObject != null) yield return StartCoroutine(currentObject.Dissolve()); currentObject = queue[currentIndex]; StartCoroutine(currentObject.Dissolve(true)); currentState = State.ViewMode; } //States of ObjectQ public enum State { ViewMode, Changing } }