Stylus buttons in unity?

0 votes
asked Mar 1, 2015 by T4D (163 points)
edited May 17, 2016 by ijonathanharper@gmail.com
what are the Stylus buttons in unity ? there not Mouse 0,1 or 2 or joystick 0,1,2 ???

how do I access the Stylus Buttons calls in unity ?

what are the setting for unity input manager ?

Maybe I look to simple are this but I want to press a button and fire off a command

normally i us a Keyboard, Mouse or Joystick input that is registered in the Input manager of unity.

after not having any Luck with mouse or joystick 0,1,2 is Zspace using something different  ? or is it Joystick button 12 or something in unity ?

1 Answer

+1 vote
answered Mar 3, 2015 by (335 points)

In Unity, the stylus button state can be retrieved using the ZSCore function IsTrackerTargetButtonPressed(TrackerTargetType, buttonId = [0 | 1 | 2 ]), eg

bool buttonIsPressed = zsCoreInstance.IsTrackerTargetButtonPressed(
                           ZSCore.TrackerTargetType.Primary,
                           0)

In your Unity application you can utilize this in a number of ways, let's look at three of them. 

Method 1: Poll button state

using UnityEngine;
using System.Collections;

public class StylusButtonM1 : MonoBehaviour
{
	
	private ZSCore zsCore;
	
	void Start()
	{
		zsCore = GameObject.Find ("ZSCore").GetComponent<ZSCore> ();
	}
	
	void Update ()
	{
		if (zsCore.IsTrackerTargetButtonPressed(ZSCore.TrackerTargetType.Primary, 0)){
			Debug.Log("Stylus button 0 is being pressed");					
		}
		else {
			//Debug.Log("Stylus button 0 released");					
		}
	}
}

So just attach this code to an active GameObject in a scene with the ZSCore prefab to try it out. Clicking the front stylus button will cause the message to print a number of times. It will print a number of times because the Update() function will likely get called several times while the button is clicked down. 

Method 2: Callback

using UnityEngine;
using System.Collections;

public class StylusButtonM2 : MonoBehaviour
{
	
	private ZSCore zsCore;
	private int count;
	
	void Start()
	{
		zsCore = GameObject.Find ("ZSCore").GetComponent<ZSCore> ();
		zsCore.Updated += new ZSCore.CoreEventHandler (OnCoreUpdated);
		count = 0;
	}
	
	/// called by ZSCore after each input update.
	private void OnCoreUpdated (ZSCore sender)
	{
		if (zsCore.IsTrackerTargetButtonPressed(ZSCore.TrackerTargetType.Primary, 0)){
			count = count + 1;
			Debug.Log("Stylus button 0 was pressed. (this message has been invoked " 
                      + count + " times)");
		}
	}		
}

Here we set the ZSCore Updated value (see General section of Unity docs, or better yet look at the ZSCore.cs script that comes with the Unity plugin). Updated is a CoreEventHandler, and can be assigned a Unity method accepting a single ZSCore parameter. The Unity method will then be invoked whenever any zSpace input event occurs. This means movements in head-tracking, movements in stylus, button presses etc. Hence if you run this code the message will print a number of times even for a single button press, since while the button is pressed down you are likely to move your head or stylus slightly and that will trigger another call. 

Thus if you want to call a function just once each time the front stylus button is pressed you would need to use something like this:

Method 3. Callback Once On Button Press

using UnityEngine;
using System.Collections;

public class StylusButtonM3 : MonoBehaviour
{
	
	private ZSCore zsCore;
	private int count;
	private bool button0JustPressed;
	private bool button0Pressed;
	
	void Start()
	{
		zsCore = GameObject.Find ("ZSCore").GetComponent<ZSCore> ();
		zsCore.Updated += new ZSCore.CoreEventHandler (OnCoreUpdated);
		count = 0;
		button0JustPressed = false;
		button0Pressed = false;
	}
	
	/// called by ZSCore after each input update.
	private void OnCoreUpdated (ZSCore sender)
	{
		if (zsCore.IsTrackerTargetButtonPressed(ZSCore.TrackerTargetType.Primary, 0)){
			if (!button0Pressed)
			{
				button0JustPressed = true;
			}
			else {
				button0JustPressed = false;				
			}
			button0Pressed = true;
		}
		else {
			button0JustPressed = false;
			button0Pressed = false;
		}
		if (button0JustPressed) Button0OnPress(); 
	}	
	private void Button0OnPress()
	{
		
		count = count + 1;
		Debug.Log("Stylus button 0 was pressed. (this message has been invoked " + count + " times)");
		
	}
}

 

 

 

 
commented Apr 6, 2015 by T4D (163 points)
you guys should do more work in this area not all zSpace developers are software engineers.. I moved to another project due to this slow down in not being able to get access to ZSpace buttons easily within unity,..  got back to it today so here's a playmaker action we created to do the above,.

this  gives button event access to playmaker users  just change the o to 1,2 etc to access other buttons

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("zSpace")]
    [Tooltip("Action generates event if zSpace button 0 pressed")]
    public class zSpaceButton0Input : FsmStateAction
    {
        [RequiredField]
        private ZSCore zsCore;
        public FsmEvent sendEvent;
        public FsmBool storeResult;

        // Code that runs on entering the state.
        public override void OnEnter ()
        {
            zsCore = GameObject.Find ("ZSCore").GetComponent<ZSCore> ();
        }

        // Code that runs every frame.
        public override void OnUpdate ()
        {
            // see if button 0 pressed
            if (zsCore.IsTrackerTargetButtonPressed (ZSCore.TrackerTargetType.Primary, 0)) {
                Fsm.Event(sendEvent);
                storeResult.Value = true;
            } else {
                storeResult.Value = false;
            }
        }
    }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
commented May 14, 2015 by treydavis76 (10 points)
I'm trying desperately to setup buttons in zSpace so they can be clicked and controlled through PlayMaker.  This script definitely looks promising, however, I have very limited knowledge on scripting and how to set it up.  Can you give any directions on exactly where you put this script, and how you set up the PlayMaker FSM to use it?
Thank you for any and all help you can give!
-T
...