Log in or register to post comments

Multiple Image Target & Canvas

December 28, 2014 - 5:17pm #1

I found this custom .cs code of "DefaultTrackableEventHandler.cs" to enabled/disable a canvas with an Image Target, it works very well !

 

		//enable canvas
		Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
		{
			canvasObject.enabled = true;
		}
		//disable canvas
		Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
		{
			canvasObject.enabled = false;
		}

 

Now I would like to work with Multiple Image Target, but the problem is : on my android only the first canvas ( "Canvas1" ) react to the code, and this for all Image Target :

 

ANDROID SCREENSHOTS

Default View : Button1 disable / Button2 enabled

ImageTarget1 : Button1 enabled / Button2 enabled

Image Target2 : Button1 enabled / Button2 enabled

 

I think the code use only the first canvas he finds in the hierarchy...

 

HIERARCHY PROJECT

AR Camera

►Image Target 1
     ►My 3D model
     ►GameObject ( empty )
          ►Canvas1
              ►Button1

►Image Target 2
     ►My 3D model
     ►GameObject ( empty )
          ►Canvas2
              ►Button2

►Directional light
►EventSystem

I would like to find a solution so that each canvas react according to their Image Target ?

Thank a lot ;)

 

CUSTOM "DefaultTrackableEventHandler.cs" by Jerry Tube

using UnityEngine;

/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
    
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS
    
    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


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

		//enable canvas
		Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
		{
			canvasObject.enabled = true;
		}

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

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

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


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

		//disable canvas
		Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas));
		{
			canvasObject.enabled = false;
		}

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

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

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

    #endregion // PRIVATE_METHODS
}

 

AttachmentSize
Image icon Default View83.53 KB
Image icon ImageTarget1 39.73 KB
Image icon ImageTarget285.33 KB

Multiple Image Target & Canvas

May 1, 2016 - 1:54pm #4

This help to find canvas and his child objects

AttachmentSize
Image icon fd.jpg78.74 KB

Multiple Image Target & Canvas

May 1, 2016 - 1:50pm #3

add the following to the TrackableEentHandler.cs -> OnTrackingFound() function:

and add the following to the TrackableEentHandler.cs -> OnTrackingLost() function:

The standard TrackableEventHandler.cs only enables renderers and colliders. This help to find canvas and his child objects

Multiple Image Target & Canvas

January 5, 2015 - 6:12am #2

It sounds like you will need to use the GetComponentsInChildren instead of the FindObjectOfType method :

//disable canvas
Canvas canvasObject = (Canvas) FindObjectOfType(typeof(Canvas)); 
{
    canvasObject.enabled = false; 
}
 
 
with this code:
 
//disable canvas under this Target
Canvas[] canvasObjectsForThisTarget = GetComponentsInChildren<Canvas>(); 
foreach (var c in canvasObjectsForThisTarget)
{
    c.enabled = false; 
}
 
 
Do the same for both the OnTrackingFound (enabling canvas) and the OnTrackingLost (disabling canvas).
 
See also:
Log in or register to post comments