Setting ManipulationHandler.ConstraintOnRotation to None throws exception
Describe the bug
Setting the ManipulationHandler.ConstraintOnRotation = RotationConstraintType.None from script will throw an exception.
To reproduce
- Create a ManipulationHandler mh
- Note that mh.ConstraintOnRotation defaults to == RotationConstraintType.None
- Explicitly set mh.ConstraintOnRotation = RotationConstraintType.None
Exception is thrown in RotationConstraintHelper.ConvertToAxisFlags(), because the returned value is not of type AxisFlags.
Expected behavior
ConvertToAxisFlags(RotationConstraintType.None) should return something of type AxisFlags. Explicitly setting ManipulationHandler.ConstraintOnRotation = RotationConstraintType.None in script should not throw an exception.
Your setup (please complete the following information)
- Unity Version 2019.3.3f1
- MRTK Version v2.3.0 (from release)
Target platform (please complete the following information)
- Android
Additional context
I'm not sure if this is Android specific or not. It is interesting that the call to ConvertToAxisFlags(RotationConstraintType.None) succeeds in ManipulationHandler.Start(), but fails when being called from my script. This suggests a bit of sloppiness in the C# interpretation.
Hi @fast-slow-still, I cannot reproduce the issue using Unity 2019.4.3f1 and MRTK 2.4, could you try to update to the latest versions and see if the problem still persists?
Hi @MaxWang-MS , thanks for looking into this. I'll have a look and report back shortly.
Hi @MaxWang-MS , did you try on an Android build? It seems somewhat more strict, or at least strict in different areas. Regardless of whether an exception is thrown, I think the following code is still incorrect:
switch (type)
{
case RotationConstraintType.XAxisOnly:
return AxisFlags.YAxis | AxisFlags.ZAxis;
case RotationConstraintType.YAxisOnly:
return AxisFlags.XAxis | AxisFlags.ZAxis;
case RotationConstraintType.ZAxisOnly:
return AxisFlags.XAxis | AxisFlags.YAxis;
default:
return 0;
}
The default case returns 0, which is not of type AxisFlags. In fact, there is no AxisFlags enum with integer value 0. When setting to RotationConstraintType.None, it falls through to default, and then there is no conversion of the value zero to a valid AxisFlags enum. That is the exception which I saw. I'm surprised it shows up as a runtime exception, I would have thought this would be caught at compile time.
That code is currently in RotationConstraintType.cs (I think it was somewhere else when I originally filed this issue).
Hi @fast-slow-still, I switched the build target to android before testing in the editor (unfortunately I don't have an android device with me), and it worked for me. As for the code you posted, I think the reason why it is technically not wrong is because you actually can assign an undefined int value to an enum, as mentioned here.
Thanks @MaxWang-MS , I'm hoping to have an Android build running on device again later today or early next week. I can post back then. It would appear to be on Android device specific then. I will verify whether it is still happening and post back.
Again, as noted in the original report, it seems a flaky bug. At the time, ManipulationHandler.Start() was (and still does) call ConvertToAxisFlags(None) successfully, without generating an exception. Later when my code called it indirectly (via the ConstraintOnRotation property setter), the exception was generated. Also as noted in the original report, I have only seen this on Android (the code is mostly targeting HL).
BTW, legality aside, do you find it preferable to return something that's not an AxisFlags rather than adding an AxisFlags.None==0? Why or why not? Just wondering about the implications of changing this to mollify the Android runtime.
Hi @fast-slow-still, thanks for your comment. After some discussion with several members on the team I believe indeed adding AxisFlags.None==0 will be beneficial to MRTK. I will be opening a PR shortly.
Thanks @MaxWang-MS, I wish I could have been there for the discussions with your colleagues, I am sure I would have learned a great deal. I appreciate your thoughtfulness.
reopening - we had to revert this fix as it was breaking existing user data and mrtk example scenes
needs more investigation on android site to figure out why this exception is triggered. By adding the None we're causing a shift in the unity interpretation of the enum in the inspector which will result in +1 on all the values stored. This will cause existing values to point to the wrong enum value.
This issue has been marked as stale by an automated process because it has not had any recent activity. It will be automatically closed in 30 days if no further activity occurs. If this is still an issue please add a new comment with more recent details and repro steps.