Topic locked

Unity 4.6 UI canvas with Vuforia

November 26, 2014 - 6:20am #1

Hello,

I didn't knew where to post this, so if this is the wrong place, i am sorry.
im also posting this on Unity forum but wanted to post it here as well for more answers.
so if its wrong i am deeply sorry and it can be removed or placed somewhere where it belongs.
anyway..

i have a setup within unity with vuforia with the new 4.6 ui canvas.
i have made a canvas with several buttons, all nice.. 
then i have a movie run on the target from vuforia with universal video texture plugin.

when all things are set up with camera and stuff, when i play the button. everything disappears like it should and when the camera sees the target then the movie is shown.
but the canvas and its ui elements don't disappear.. so when i have multiple targets next to each other, all with there own ui. i see all the ui elements of all the targets flying there.
i have set my canvas to world mode because it needs to be set around the movie.
i also made all the items a child of the image target.

can anyone help how to hide the canvas and only show it when the target is visible?
is there some option i missed?

thanks in advance.

Nick.

Unity 4.6 UI canvas with Vuforia

December 9, 2014 - 6:59am #9

Glad to hear it is solved.

Of course you could re-use the same approach to enable / disable other components;

note, however, they must be Unity Components; the "Cube" class you mention in your example does not exist in the Unity API, as far as I can tell. Rather, a cube is just a GameObject with primitiveType.Cube; see also:

http://docs.unity3d.com/ScriptReference/PrimitiveType.Cube.html

What you could do is to get a GameObject, then get the Renderer components attached to the Object and enable / disable those.

But, again, this is more like a Unity programming question, not Vuforia ;-)

 

Unity 4.6 UI canvas with Vuforia

December 9, 2014 - 2:58am #8

Great thank you very much.

this worked for me.

 

can i use this for everything?

lets say i have a cube replacing the canvas. would then the line be:

 

Cube cubeObject = (Cube) FindObjectOfType( typeof(Cube) );

cubeObject.enabled = true / false;

 

thanks so far. the canvas made hidden worked as a charm now!!!!

Unity 4.6 UI canvas with Vuforia

December 4, 2014 - 6:29am #7

You should be able to get it working by replacing 

Canvas.enabled = true / false;

with

canvasObject.enabled = true / false;

Unity 4.6 UI canvas with Vuforia

December 4, 2014 - 12:28am #6

I am getting an error when i imported the code.

an object reference is required to access non-static member

with the line 

canvas.enabled = true / false;

 

 

i also want to have multiple imagetargets with multiple canvasses. 

when i rename them all canvas1, canvas2,canvas3 etc etc.. how do i implement this into the code?

i have put this within the OnTrackingFound

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

and this within OnTrackingLost

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

 

here's my code so far.

    #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);

        //enable canvas
	Canvas canvasObject = (Canvas) FindObjectOfType( typeof(Canvas) );
	{
	Canvas.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);


         //disable canvas
	Canvas canvasObject = (Canvas) FindObjectOfType( typeof(Canvas) );
	{
	Canvas.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
}
        

Unity 4.6 UI canvas with Vuforia

December 3, 2014 - 4:27am #5

Cool. i will try that. thanx!!

Unity 4.6 UI canvas with Vuforia

December 3, 2014 - 3:29am #4

You could try with something like:

Canvas canvasObject = (Canvas) FindObjectOfType( typeof(Canvas) );

canvas.enabled = true / false;

 

Unity 4.6 UI canvas with Vuforia

December 3, 2014 - 12:08am #3

Hi AlessandroB, thank you for your reply.

Could you please make an example code of how to disable the canvas?

the only ontracking found i was able to get was with an animation within unity.

the code in orange was the code i used so the animation only plays when the cam sees the target, and that works.

so how do i change this so it works with the canvas, or for instance a movie or other things?

thank you very much in advance!!

 

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

        // *** Additional animation code
        Animation[] animationComponents = GetComponentsInChildren<Animation>();
        
        foreach (Animation component in animationComponents)
        {
            component.Play();
        }
        // *** end of animation code

        // 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);

        // *** Additional animation code
        Animation[] animationComponents = GetComponentsInChildren<Animation>();
        
        foreach (Animation component in animationComponents)
        {
            component.Stop();
        }
        // *** end of animation code

Unity 4.6 UI canvas with Vuforia

November 28, 2014 - 8:28am #2

You can detect when a Target becomes visible (detected) or not visible (lost) using the ITrackableEventHandler interface;  see the DefaultTrackableEventHandler used in the Vuforia Samples for an example of it.

Based on that, you could for example get a reference to the Canvas game object in your scene, retrieve the Canvas component programmatically (using cnavas.GetComponent<Canvas>()) and then disable it;   there might also be other techniques to disable Canvas elements that you may want to search in the Unity forum.

 

Topic locked