EDIT: I figured out how to script this with a little tinkering. Anyone who wants to use the script below is welcome to it.
Basically, this creates an object "slideshow," where you can have a number of objects placed on the same target and set them to switch out at given intervals.
To explain how this works:
I created a timer (whose length is a product of the total number of objects and a user-determined interval between switches). The timer starts counting down when the target is first tracked. When the timer falls below this predetermined time, a different object will be set as active, and so on. When the timer hits 0, it resets at the first object.
To use this:
- first edit the script to contain whatever number of objects and intervals you need (right now it is set to use 2 objects, but I have added functionality for a 3rd and commented it out to show you how it would be done. You should be able to add as many as you want).
- In your Unity scene, add your objects as children of your target
- Drag this script onto your target
- In the Inspector window under "Object Slideshow," add your objects in the order you want them to appear. Then, total up the "Number of Objects" you are using and define the "Time Between Switches" in number of seconds.
This took me a while to figure out, so hopefully some of you might benefit from my struggle. Let me know if any of you have suggestions for how to make the code more efficient -- I'm still new at this.
Enjoy!
using System.Collections; using System.Collections.Generic; using UnityEngine; using Vuforia; /// <summary> /// Creates a timer which triggers a slideshow of objects on a single tracker /// Add to target and then define variables in the inspector /// </summary> public class ObjectSlideshow : MonoBehaviour, ITrackableEventHandler { private TrackableBehaviour mTrackableBehaviour; /// Define these in the inspector. Add more objects and switches here as necesarry. [SerializeField] private float numberOfObjects; [SerializeField] private float timeBetweenSwitches; [SerializeField] private GameObject object1; [SerializeField] private GameObject object2; //[SerializeField] private GameObject object3; private float timer; private float switch1; private float switch2; //private float switch3; private float timerReset; private bool tracking; void Start() { /// Hides objects at startup object1.SetActive(false); object2.SetActive(false); //object3.SetActive(false); /// Sets the length of the timers as a product of input variables float maxTime = timeBetweenSwitches * numberOfObjects; timer = maxTime; timerReset = maxTime; switch1 = (maxTime - (timeBetweenSwitches)); switch2 = (maxTime - (timeBetweenSwitches * 2)); //switch3 = (maxTime - (timeBetweenSwitches * 3)); mTrackableBehaviour = GetComponent<TrackableBehaviour>(); if (mTrackableBehaviour) { mTrackableBehaviour.RegisterTrackableEventHandler(this); } } public void OnTrackableStateChanged( TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) { if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) { tracking = true; } } void Update() { if (tracking == true) { /// Timer starts counting down when tracking begins, /// switching objects as it goes timer -= Time.deltaTime; /// First Switch if (timer > switch1) { object1.SetActive(true); } else { object1.SetActive(false); } /// Second Switch if (timer > switch2 && timer < switch1) { object2.SetActive(true); } else { object2.SetActive(false); } /// Third Switch //if (timer > switch3 && timer < switch2) //{ // object3.SetActive(true); //} //else //{ // object3.SetActive(false); //} /// Resets timer at 0 if (timer <= 0) { timer = timerReset; } } } }
Modified your Code to add n
Modified your Code to add n no. of objects