【问题标题】:Activate and Desactivate GameObjects by toggles - Augmented Reality通过切换激活和停用游戏对象 - 增强现实
【发布时间】:2017-06-28 18:28:53
【问题描述】:

我正在使用 ArToolkit 制作一个带有增强现实的应用程序。

这很简单。

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

当我运行时,会显示一个元素。如果我首先单击toggle,则隐藏此元素并显示下一个元素。当我点击第二个toggle 时,前一个元素隐藏,下一个元素显示。

换句话说,当我运行时,只显示第一个元素(Cube)。当我点击第一次切换时,会显示每个元素(立方体、esphere、cilinder):

第 1 步:

第 2 步:

第 3 步:

这是我的层次结构:

ArControlador 内部是标记。 Canvas 内部是切换开关。

但是,实际上我以粗鲁的方式构建了这个项目,使用GameObject.Find 获取每个元素。我希望以优雅的方式、简洁的代码和可扩展的方式获得游戏对象。

我想以自动方式获取游戏对象,所以如果我有 3、5、10 或 20 个元素,代码将完美运行。

我认为在 ArrayGameObjectsListGameObjects 等一些可能性中,创建一个 GameObject 父亲并获得所有像 FindGameObjectsWithTag 这样的孩子,但我没有成功。

这是我的代码,它已经工作了,但是以粗鲁的方式,通过 GameObject 获取 GameObject 并使用 aux 变量启用/禁用游戏对象,请参阅:

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

那么,我怎样才能像上面的例子(arraylisttag)或其他东西一样以智能方式获得GameObjects

【问题讨论】:

    标签: c# unity3d augmented-reality artoolkit


    【解决方案1】:

    遍历转换层次结构。您已经将 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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 2016-04-10
      相关资源
      最近更新 更多