I’m making an application with Augmented Reality using ArToolkit.
It’s very simple.
I show some elements accord the toggle stage. Example, I’ve 3 elements
When I run, one element is displayed. If I click in first toggle this element hide and the next element show. When I click in second toggle the previous element hide and the next is show.
In another words, when I run only the first element (Cube) is displayed. When I click in first toggle, each element (cube, esphere, cilinder) is show:
Step 1:
Step 2:
Step 3:
This is my hierarchy:
Inside “ArControlador” are the markers. Inside “Canvas” are the toggles.
But, actually I build this project in rude way, getting each element with **GameObject.Find**
. I want get the GameObjects in in elegance way, clean code and it can be scaleable.
I want get the GameObjects in an automated way, so if I have 3, 5, 10 or 20 elements the code will run perfectly.
I think in some possibilities like Array of GameObjects, List of GameObjects, create a GameObject father and get all childs like FindGameObjectsWithTag but I don’t have successfull.
This is my code, it already works but in rude way, geting GameObject per GameObject and enable/disable the GameObjects with aux variable, see:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Linq;
using System.Collections.Generic;
using System;
using UnityEngine.SceneManagement;
public class AlternaEntreOsPassos : MonoBehaviour
{
private UnityEngine.UI.Toggle[] toggles;
public int aux = 0;
public GameObject marker1, marker2, marker3;
void Awake()
{
//Find manually the objects
marker1 = GameObject.Find("Marcador1");
marker2 = GameObject.Find("Marcador2");
marker3 = GameObject.Find("Marcador3");
}
void Start()
{
toggles = GetComponentsInChildren<UnityEngine.UI.Toggle>();
if (toggles.Length > 0)
{
//2nd and 3rd false for not be displayed yet...
marker2.SetActive(false);
marker3.SetActive(false);
for (int i = 0; i < toggles.Length; i++)
{
int closureIndex = i;
toggles[closureIndex].interactable = false;
toggles[closureIndex].onValueChanged.AddListener((isOn) =>
{
if (isOn == true)
{
aux++;
//Disabling the toggle that was clicked
toggles[closureIndex].interactable = false;
if (closureIndex < toggles.Length - 1)
{
//Activatin the next toggle
toggles[closureIndex + 1].interactable = true;
}
if (aux == 1)
{
//Desactivating the actual element and activating the next element
marker1.SetActive(false);
marker2.SetActive(true);
}
if (aux == 2)
{
//Desactivating the actual element and activating the next element
marker2.SetActive(false);
marker3.SetActive(true);
}
if (aux == 3)
{
marker3.SetActive(false);
}
}
}
);
}
toggles[0].interactable = true;
}
}
}
So, how I can get the GameObjects in intelligent way like the examples above (array, list, tag) or something else.