Various 3D Models with an image target

0

I can not achieve this task, I have 6 different models that have the same target, but I want to see only one model at a time, say the one in position [i], since I am doing it with an array of GameObjects, then when selecting the next one with an arrow I have just seen the following model and the same thing happens with the back arrow, that I deployed the previous model.

Achieve visualize a model but as you go forward with the arrows the others are also on the screen, filling the 6 models. The code so far is the following:

using UnityEngine;
using Vuforia;
using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyDefaultTrackableEventHandler3: MonoBehaviour, ITrackableEventHandler {


  protected TrackableBehaviour mTrackableBehaviour;
  public int s_SelectedIndex;
  public GameObject[] players;

  protected virtual void Start() {

    players = GameObject.FindGameObjectsWithTag("Player");

    foreach(GameObject wi in players) {
      wi.SetActive(false);
    }
    mTrackableBehaviour = GetComponent < TrackableBehaviour > ();
    if (mTrackableBehaviour)
      mTrackableBehaviour.RegisterTrackableEventHandler(this);

  }

  protected virtual void OnDestroy() {
    if (mTrackableBehaviour)
      mTrackableBehaviour.UnregisterTrackableEventHandler(this);

  }


  public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) {

    if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) {
      Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
      OnTrackingFound();

    } else if (previousStatus == TrackableBehaviour.Status.TRACKED && newStatus == TrackableBehaviour.Status.NO_POSE) {
      Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
      OnTrackingLost();
    } else {
      OnTrackingLost();
    }
  }


  public void Suma() {
    players[s_SelectedIndex].SetActive(false);
    s_SelectedIndex++;
    players[s_SelectedIndex].SetActive(true);
  } 

  public void Resta() {
    players[s_SelectedIndex].SetActive(false);
    s_SelectedIndex--;
    players[s_SelectedIndex].SetActive(true);
  }


  protected virtual void OnTrackingFound() {



    if (s_SelectedIndex < players.Length) {
      Debug.Log("Player Number " + s_SelectedIndex + " is named " + players[s_SelectedIndex].name);

      var rendererComponents = players[s_SelectedIndex].GetComponentsInChildren < Renderer > (true);
      var colliderComponents = GetComponentsInChildren < Collider > (true);
      var canvasComponents = GetComponentsInChildren < Canvas > (true);


      foreach(var component in rendererComponents)
      component.enabled = true;


      foreach(var component in colliderComponents)
      component.enabled = true;


      foreach(var component in canvasComponents)
      component.enabled = true;

    }


  }


  protected virtual void OnTrackingLost() {

    var rendererComponents = GetComponentsInChildren < Renderer > (true);
    var colliderComponents = GetComponentsInChildren < Collider > (true);
    var canvasComponents = GetComponentsInChildren < Canvas > (true);


    foreach(var component in rendererComponents)
    component.enabled = false;


    foreach(var component in colliderComponents)
    component.enabled = false;


    foreach(var component in canvasComponents)
    component.enabled = false;

  }

}
    
asked by Jairzinho 29.09.2018 в 05:25
source

1 answer

0

Try deactivating the complete gameobjects (if you do not have them in front of you, it is better to create an Empty Object for each object and open the models there, and deactivate the Empty), deactivating the components sometimes does not work well in Unity. Also you avoid the overhead of having to find the components of each object in each change.

Replace this:

var rendererComponents = players[s_SelectedIndex].GetComponentsInChildren < Renderer > (true);
  var colliderComponents = GetComponentsInChildren < Collider > (true);
  var canvasComponents = GetComponentsInChildren < Canvas > (true);


  foreach(var component in rendererComponents)
  component.enabled = true;


  foreach(var component in colliderComponents)
  component.enabled = true;


  foreach(var component in canvasComponents)
  component.enabled = true;

Because of this:

players[s_SelectedIndex].SetActive(true);

And likewise for deactivation.

    
answered by 13.11.2018 в 15:16