I’m trying to create an input system that will catch all left mouse button events.
With the current code I’m missing about 1 in every 35 left mouse button events.
The target platform is a Windows Standalone created in Windows 5 Pro running on a machine with Windows 10. The mouse input is being driven by launching toy ammunition at a touchscreen, at a rate of about 2-ish per second. I’m testing on a scene with no real overhead, just a plane and instantiating a sphere locked at 60 frames per second with vsync. My code is using OnGUI to put mouse events into a queue and Update to process them. I’ve tried the popular TouchScript and Rewired plug-ins and the result is similar.
Any advice would be greatly appreciated.
import System.Collections.Generic;
var queue = new List.<Event>();
var currentEvent: Event;
var mouseNumber: int;
var mouseTextBox: GameObject;
var mouseSphere: GameObject;
var mouseVisibleNumber : int;
function OnGUI() {
currentEvent = Event.current;
//find left mouse button clicks
if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0){
//adds to back of queue
queue.Add(currentEvent);
}
}
function Update() {
var hit: RaycastHit;
//if the queue has anything in it
if (queue.Count > 0){
//references front of queue
var processingEvent = queue [0];
var flippingEvent = processingEvent.mousePosition;
//flips y coordinate, as putting it in the queue causes it to be inverted somehow
var flippingY = Screen.height-processingEvent.mousePosition.y;
var ray = Camera.main.ScreenPointToRay(Vector3(processingEvent.mousePosition.x, flippingY, 0));
//if the mouseclick hits something, add number to textbox
if (Physics.Raycast(ray, hit)){
mouseNumber ++;
Instantiate (mouseSphere, hit.point, transform.rotation);
mouseVisibleNumber ++;
var mouseInt = mouseVisibleNumber;
var mouseStr = mouseInt.ToString();
mouseTextBox.GetComponent(UI.Text).text = mouseStr;
for (var queuedEvents in queue){
Debug.Log (queue.Count + "," + queuedEvents);
}
queue.RemoveAt(0);
}
}
}