When i add OnScreenStick or OnScreenButton to a GameObject that is turned off and on, unity buttons for some reason needs to be double pressed (On PC) in order for it to execute.
The cause of this is this function : SetupInputControl() which executes OnEnable for this script, which is part of the unity new input system plugin.
Packages\InputSystem\Plugins\OnScreen\OnScreenControl.cs
I tried very hard to try to figure out what to modify in this function to make this problem go away, but i failed.
private void SetupInputControl()
{
Debug.Assert(m_Control == null, "InputControl already initialized");
Debug.Assert(m_NextControlOnDevice == null, "Previous InputControl has not been properly uninitialized (m_NextControlOnDevice still set)");
Debug.Assert(!m_InputEventPtr.valid, "Previous InputControl has not been properly uninitialized (m_InputEventPtr still set)");
// Nothing to do if we don't have a control path.
var path = controlPathInternal;
if (string.IsNullOrEmpty(path))
return;
// Determine what type of device to work with.
var layoutName = InputControlPath.TryGetDeviceLayout(path);
if (layoutName == null)
{
Debug.LogError(
$"Cannot determine device layout to use based on control path '{path}' used in {GetType().Name} component",
this);
return;
}
// Try to find existing on-screen device that matches.
var internedLayoutName = new InternedString(layoutName);
var deviceInfoIndex = -1;
for (var i = 0; i < s_OnScreenDevices.length; i)
{
////FIXME: this does not take things such as different device usages into account
if (s_OnScreenDevices[i].device.m_Layout == internedLayoutName)
{
deviceInfoIndex = i;
break;
}
}
// If we don't have a matching one, create a new one.
InputDevice device;
if (deviceInfoIndex == -1)
{
// Try to create device.
try
{
device = InputSystem.AddDevice(layoutName);
}
catch (Exception exception)
{
Debug.LogError(
$"Could not create device with layout '{layoutName}' used in '{GetType().Name}' component");
Debug.LogException(exception);
return;
}
InputSystem.AddDeviceUsage(device, "OnScreen");
// Create event buffer.
var buffer = StateEvent.From(device, out var eventPtr, Allocator.Persistent);
// Add to list.
deviceInfoIndex = s_OnScreenDevices.Append(new OnScreenDeviceInfo
{
eventPtr = eventPtr,
buffer = buffer,
device = device,
});
}
else
{
device = s_OnScreenDevices[deviceInfoIndex].device;
}
// Try to find control on device.
m_Control = InputControlPath.TryFindControl(device, path);
if (m_Control == null)
{
Debug.LogError(
$"Cannot find control with path '{path}' on device of type '{layoutName}' referenced by component '{GetType().Name}'",
this);
// Remove the device, if we just created one.
if (s_OnScreenDevices[deviceInfoIndex].firstControl == null)
{
s_OnScreenDevices[deviceInfoIndex].Destroy();
s_OnScreenDevices.RemoveAt(deviceInfoIndex);
}
return;
}
m_InputEventPtr = s_OnScreenDevices[deviceInfoIndex].eventPtr;
// We have all we need. Permanently add us.
s_OnScreenDevices[deviceInfoIndex] =
s_OnScreenDevices[deviceInfoIndex].AddControl(this);
}
CodePudding user response:
When i upgraded to 2022.1.13f, it was solved.