Log in or register to post comments

Activate Individual Elements in New UI

February 27, 2015 - 2:26pm #1

How can I activate and deactivate individual canvas elements of the new UI (a slider, specifically) using the OnTrackableFound function? I currently have the slider set to "gameObject.SetActive (false)" when the app starts, but I can't figure out how to get it to show up when the ImageTarget is found. Thanks.

Activate Individual Elements in New UI

March 1, 2015 - 11:57pm #3

The approach you use here is generlly corretc; however, since you use GetComponentInChildren<Canvas>() to retrieve your Canvas, this will only retrieve Canvas components within the hierarchy of the Trackable on which you have attached your TrackableEventHandler.

If you wish to identify Canvas elements that may be located elsewhere in the Scene hierarchy, you may want to consider using something like:

Canvas[] canvas = (Canvas[]) FindObjectsOfType ( typeof (Canvas) );

This will return ALL canvas objects in your scene, for example.

 

Activate Individual Elements in New UI

March 1, 2015 - 12:53pm #2

I found that simply creating another Canvas and enabling/disabling in the TrackableEventHandler script works just fine. Not sure if this is the best way, though.

  private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
	Canvas [] canvasComponents = GetComponentsInChildren<Canvas> (true);

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

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

	foreach (Canvas component in canvasComponents)
	{
	     component.enabled = true;
	}

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
	Canvas [] canvasComponents = GetComponentsInChildren<Canvas> (true);

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

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

	foreach (Canvas component in canvasComponents)
	{
	    component.enabled = false;
	}

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

 

Log in or register to post comments