Log in or register to post comments

Send parameters to DefaultTrackableEventHandler.cs

December 26, 2012 - 6:11am #1

In kind of user-defined target sample i'd want to show just some 3d objects which are childs of UserDefinedTarget prefab by clicking some button. For example,suppose I have 3 objects childs of UserDefinedTarget prefab and 3 button on the screen. To do these i'd wand to send indexes  to DefaultTrackableEventHandler. Suppose the first child are made of objects that have indexes from 0 to 17. In the OnTrackingFound method i will need to render in this way: 

</p><p>beginIndex = 0;</p><p>endIndex = 18;</p><p>for(int i = beginIndex; i < endIndex; i++)</p><p>{</p><p>rendererComponents[i].enabled =true;</p><p>}</p><p>

 

So i'dd want to send this indexes from UserDefinedTargetEventHandler.cs after detecting which button has been clicked in ScanModeUILogic. How can i do this?

 

Or maybe i'm doing it in a bad place?

 

  There are a few ways to do

December 26, 2012 - 11:01am #2

 

There are a few ways to do this. You can set a public variable on the UDT Event Handler using GetComponent as such..

 

using UnityEngine;
using System.Collections;

public class UDTSelector : MonoBehaviour {
 
 UserDefinedTargetEventHandler eventHandler;
 // Use this for initialization
 void Start () {
 
  eventHandler = GetComponent<UserDefinedTargetEventHandler>();
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 
 void OnGUI(){
  
  if( GUILayout.Button("Select Model 1") ){
   eventHandler.modelIndex = 1;
  }
  
  if( GUILayout.Button("Select Model 2") ){
   eventHandler.modelIndex = 2;
  }
 }
}

 

Where the UserDefinedTargetEventHandler exposes this variable as..

public int modelIndex;

But IMO a cleaner implementation would be to have the event handler check a ModelSelection object (e.g. GameObject.Find("ModelSelection").GetComponent<UDTSelector>().selectedModel ) that is independent of the event handler. This will make it easier to support customization (e.g. multiple events handlers ) and behaviors across the models (e.g. multiple model rendering, and model combinations logic ).

Also you can use GetComponentsInChildren to obtain the renderers for these models, rather than needing to specify all of them explicitly..

        Renderer[] rendererComponents = selectedModel.GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = selectedModel.GetComponentsInChildren<Collider>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }

 

This approach would enable you to pass the model selection by name, as a string, and use GameObject.Find() to reference the selected model.

Log in or register to post comments