Log in or register to post comments

Virtual Button

December 16, 2011 - 2:35pm #1

Hi, I have a question about Virtual Button.

I want to add function for virtual Button that show 3D object when you hide it. Could you tell me what I should do or How I change the Virtual Button sample?

From the sample of Virtual Button, I kind of know how to change the teapod color when I touch Virtual Button.

But in my case, I want to show the object only when you touch the button. When you dont touch, there shouldnt be anything.

Could you tell me what I should do, or recommend me websites I can study. Since Im new, I dont know what i should start.

Thank you so much.

Virtual Button

September 23, 2018 - 11:30pm #8

Hi, i am working on hololens and I want to know can we use virtual buttons in hololens? if yes, can we interact with virtual buttons like in android?

Re: Virtual Button

December 20, 2011 - 1:51pm #7

Hi, Kim!
Thank you so much again, this was really helpful to understand.

Re: Virtual Button

December 19, 2011 - 7:22pm #6
m_Teapot = transform.FindChild("teapot").gameObject;

This is just another way to find the teapot object. You can search by name rather than dragging objects onto a public variable in the Inspector view.

m_TeapotObject.active = false;

This is the code that hid the teapot. Try calling it in the Start method.

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Start.html

Alternatively, you can select the teapot in your scene and uncheck the box in the upper left corner of the Inspector view. That's the same thing as toggling the "active" parameter.

- Kim

Re: Virtual Button

December 19, 2011 - 2:12pm #5

Thanks so much! I really appreciate your detailed explanation!
Now I can switch objects I want to show.

One problem is that all objects are shown when camera detect the target image, and then virtual button start working.
I want some objects hidden until l I touch button.

I tried to change the position of following code.
m_Teapot = transform.FindChild("teapot").gameObject;
but it doesnt solve.

do you have any solution??

Re: Virtual Button

December 19, 2011 - 12:54pm #4

First off, make m_TeapotObject public:

public GameObject m_TeapotObject;

Save the file. Click on ImageTargetWood in the Hierarchy view of the Unity editor. Look at the Inspector view. You should see a field named "Teapot Object" under the "Virtual Button Event Handler" component. Drag the teapot from the Hierarchy view onto this field.

Next, see the Unity Script Reference for the correct usage of the "active" property:

http://unity3d.com/support/documentation/ScriptReference/GameObject-active.html

So you would want to do this:

m_TeapotObject.active = true;

Then, in OnButtonReleased you can set it to false, using similar code. Here are the simplified OnButtonPressed and OnButtonReleased methods:

    // Called when the virtual button has just been pressed:
    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        // Apply the new texture:
        switch (vb.VirtualButtonName)
        {
            // Add additional cases if you want

            case "yellow":
                m_TeapotObject.active = true;
                break;
        }
    }


    // Called when the virtual button has just been released:
    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
        switch (vb.VirtualButtonName)
        {
            // Add additional cases if you want

            case "yellow":
                m_TeapotObject.active = false;
                break;
        }
    }

- Kim

Re: Virtual Button

December 19, 2011 - 10:37am #3

Thanks for reply. Since Im not familiar with coding, can I ask more detail?

In the "class VirtualButtonEventHandler",
I created "GameObject m_TeapotObject;"

In the "public void OnButtonPressed(VirtualButtonBehaviour vb)",
I wrote,
"case "yellow":
m_TeapotObject.active();
//mTeapot.renderer.material = m_TeapotMaterials[2];
break;"

And I don't understand how to deactivate the object in the "public void OnButtonReleased(VirtualButtonBehaviour vb)".
Should I destroy once like "Destroy(m_TeapotObject);"?

Thank you so much in advance.
Rest of code are all same as samples.

Re: Virtual Button

December 16, 2011 - 4:13pm #2

Start with the VirtualButtonEventHandler included with the sample. Instead of an array of Materials, create an array of GameObjects and add all your models via the Inspector view. Then, in the OnButtonPressed method you can select the correct model by its index and activate it via the GameObject.active property. In the OnButtonReleased method you can deactivate it.

- Kim

Log in or register to post comments