通过切换来激活和禁用GameObjects - 增强现实

问题描述:

我正在使用ArToolkit进行增强现实的应用程序。通过切换来激活和禁用GameObjects - 增强现实

这很简单。

我显示了一些元素符合切换阶段。例如,我3个元素

3 Elements

当运行时,显示一个元素。如果我先点击toggle这个元素隐藏并显示下一个元素。当我点击第二个toggle前一个元素隐藏,下一个是显示。

换句话说,当我运行时只显示第一个元素(Cube)。当我在第一肘单击,每个元件(立方体,esphere,cilinder)是显示:

步骤1: Step1

步骤2: step2

步骤3: step3

这是我的等级:

hierarchy

里面ArControlador是标记。内部Canvas是切换。

但是,实际上我用粗鲁的方式建立这个项目,得到每个元素GameObject.Find。我希望以优雅的方式获得GameObjects,干净的代码和可扩展性。

我想以自动方式获取GameObjects,因此如果我有3,5,10或20个元素,代码将完美运行。

我觉得在一些可能像GameObjectsArrayGameObjectsList,创建一个GameObject父亲,并得到像FindGameObjectsWithTag所有孩子的,但我没有全成。

这是我的代码,它已经工作,但在粗暴的方式,歌厅GameObjectGameObject并启用/禁用与aux变量GameObjects,请参阅:

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; 
     }  
    } 
} 

所以,我怎样才能在智能化GameObjects像上面的例子(arraylist,tag)或其他的方式。

遍历变换层次结构。您已经将您的step1,step2,step3 ...对象分配给父级。

List<GameObject> steps = new List<GameObject>(); 

void Awake() { 
    GameObject parentObject = GameObject.Find("Steps"); 
    for(int i = 0; i < parentObject.transform.childCount; i++) { 
     steps.Add(parentObject.transform.GetChild(i)); 
    } 
}