Log in or register to post comments

Virtual Buttons ... in javascript

September 14, 2011 - 10:31am #1

Hello, I'm trying to implement some virtual buttons in my Android app. I've seen how to handle it with a .cs file, but am a bit more familiar with .js.

Can someone provide an example of how to script a virtual button press/release in javascript?

This is where i am at the moment:

var cubeMesh : GameObject; 

function Start(){
 	 cubeMesh.active = false;

}
// Called when the virtual button has just been pressed:
function OnButtonPressed(IVirtualButtonEventHandler)
    {
        Debug.Log("OnButtonPressed");
        cubeMesh.active = true;
    }

Re: Virtual Buttons ... in javascript

September 15, 2011 - 11:52am #5

The EventHandler should be attached to the ImageTarget object that contains the buttons. Take a look at the VirtualButtons sample to see the correct setup.

The javascript file doesn't need to be attached to anything in this case, it is instantiated in the Start method of the EventHandler.

- Kim

Re: Virtual Buttons ... in javascript

September 15, 2011 - 11:35am #4

How would this be set up in the scene hierarchy?

- Does the "VirtualButtonEventHandler.cs" file need to be attached on the stage?

- And the "JSVirtualButtonEventHandler.js", Does it get attached to the ImageTarget GameObject? or just sit in the root of the Plugins folder?

Thanks in advance,
CapnJ

Re: Virtual Buttons ... in javascript

September 15, 2011 - 7:21am #3

Awesome, I'll give it a try.

Thanks n' Yar,
CapnJ

Re: Virtual Buttons ... in javascript

September 14, 2011 - 10:38pm #2

So I don't think you'll be able to implement the IVirtualButtonEventHandler interface in javascript. You can create a bare bones handler in C# that calls your javascript functions, however.

VirtualButtonEventHandler.cs

using UnityEngine;

public class VirtualButtonEventHandler :    MonoBehaviour,
                                            IVirtualButtonEventHandler
{
    private JSVirtualButtonEventHandler jsHandler;

    void Start()
    {
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        for (int i = 0; i < vbs.Length; ++i)
        {
            vbs[i].RegisterEventHandler(this);
        }

        jsHandler = new JSVirtualButtonEventHandler();
    }

    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        Debug.Log("OnButtonPressed");
        jsHandler.OnButtonPressed(vb.VirtualButtonName);
    }

    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
        Debug.Log("OnButtonReleased");
        jsHandler.OnButtonReleased(vb.VirtualButtonName);
    }
}

JSVirtualButtonEventHandler.js

function OnButtonPressed (name : String)
{
    Debug.Log("js " + name + " pressed");
}

function OnButtonReleased (name : String)
{
    Debug.Log("js " + name + " released");
}

Be sure to put the javascript file in an earlier compilation pass by placing it in e.g. the Plugins folder.

- Kim

Log in or register to post comments