Checking the equality/inequality of a system-defined enum with equality/inequality operators (
==
/
!=
) will fail.
public override void OnInputMethodChanged(VRCInputMethod inputMethod)
{
if (inputMethod == VRCInputMethod.Touch)
{
// Run code for touch input
}
else
{
// Run code for non-touch input
}
}
(The UdonGraph version (attached image) also has an identical issue.)
Workaround
Cast to underlying values and compare them.
if ((int)inputMethod == (int)VRCInputMethod.Touch)
Analysis
UdonSharp compiles the expression
inputMethod == VRCInputMethod.Touch
into
EXTERN, "SystemObject.__Equals__SystemObject__SystemBoolean"
Although
Object.Equals(Object)
is overridden by
Enum.Equals(Object)
, which the
inputMethod
may have, with comparing underlying value, this
EXTERN
seems to call the
Object.Equals
directly (maybe via reflection API), and it returns the unexpected result (by only comparing the referencing instances).
The
EXTERN, "SystemObject.__Equals__
comes from here https://github.com/vrchat-community/UdonSharp/blob/22307065bd408dfd163fe46b0b8b701a4efcbc00/Packages/com.vrchat.UdonSharp/Editor/Compiler/Binder/BoundNodes/BoundInvocationExpression.cs#L420 And replacing the
System_Object
of this line with
System_Enum
doesn't work because the
Enum.Equals
is not exposed.
Suggestions
  • Temporary, rewrite the example codes using cast operator
  • Expose
    System.Enum.Equals
  • And replace the
    EXTERN
    with
    System.Enum.Equals