Log in or register to post comments

Hide / show 3d object on trackables

November 25, 2011 - 5:39am #1

Hello,

I use unity in combination with SDK for a android AR model. With this model I made a menu that allows a person to hide and show the model.

I used the basic Unity script
if .. == true then i load hide model else i load show model.
With that script I am able to hide and show the model, but the problem is. When i press the button twice I can activate the gameobject without my camera pointing to the tracable

My code:

function hideModel ()
{
gameObject.SetActiveRecursively(false);
}

function showModel ()

{
gameObject.SetActiveRecursively(true);
}

Is there a way to hide and show my models ONLY when my camera points to the trackable.

You're welcome.

December 14, 2012 - 4:43am #7

You're welcome.

Hide / show 3d object on trackables

December 13, 2012 - 7:44am #6

Thanks a lot alessandro thats really helped to me :)

Hide / show 3d object on trackables

December 10, 2012 - 2:15am #5

Hi, accessing the members of a C# script from Javascript is not something that you can do that way;

you need first to retrieve the C# script (DefaultTrackableEventHandler) using GetComponent;

also, the boolean variable isVisible should be public.

Have a look at this Unity tutorial explaining how to access C# variables from JS;

http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access

Another possibility could be to use the GameObject.sendMessage() function, which allows to invoke a method belonging to another script; in this case you will need to introduce a public method called "isVisible()" in the C# script, so that you can call GameObject.sendMessage("isVisible"); from JS. The isVisible method in C# will simply return the value of your "isVisible" private variable.

I hope this helps.

 

Hide / show 3d object on trackables

December 9, 2012 - 8:22am #4

I did  all of them.

in DefaultTrackableEventHandler i made an variable as

 private bool isVisible = false;

 

when I check in my javascript code 

if (DefaultTrackableEventHandler.isVisible == true){

}

getting this error "Unknown identifier: 'DefaultTrackableEventHandler'."

 

seems i couldnt accessing C# variables in JavaScript  :(

any1 help me ?

Re: Hide / show 3d object on trackables

December 2, 2011 - 4:36am #3

Thank you Kim,

It worked. I did what you said, I made a new var for isVisible and checked that value in my other JS file:

if (TrackableEventHandler.isVisible == true){
	[[[ your action here ]]]	}

Re: Hide / show 3d object on trackables

November 28, 2011 - 5:51pm #2

Take a look at the TrackableEventHandler that comes with the samples. You can use the OnTrackingFound and OnTrackingLost methods to assign a global variable that keeps track of whether or not a trackable is visible. Something like this:

    private bool isVisible = false;

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

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

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

        isVisible = true;
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

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

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

        isVisible = false;
    }

Your showModel method should check if this variable is true.

- Kim

Log in or register to post comments