UnityView+iOS.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #if PLATFORM_IOS || PLATFORM_VISIONOS
  2. #import "UnityView.h"
  3. #import "UnityAppController+Rendering.h"
  4. #include "OrientationSupport.h"
  5. #import <GameController/GameController.h>
  6. @interface UnityView ()
  7. @property (nonatomic, readwrite) ScreenOrientation contentOrientation;
  8. @end
  9. // indirect mouse input on iOS: GCMouse.scroll gives real wheel deltas per notch (a pan recognizer
  10. // only gives an uneven pan), and UIHoverGestureRecognizer gives the cursor position uGUI routes on.
  11. @implementation UnityView (iOSScrollWheel)
  12. - (void)setupIndirectMouseInput
  13. {
  14. // wheel deltas: attach to the current mouse and any that becomes current
  15. [[NSNotificationCenter defaultCenter] addObserver: self
  16. selector: @selector(mouseDidBecomeCurrent:)
  17. name: GCMouseDidBecomeCurrentNotification
  18. object: nil];
  19. if (GCMouse.current != nil)
  20. [self attachScrollHandlerToMouse: GCMouse.current];
  21. // cursor position for routing
  22. UIHoverGestureRecognizer* hover = [[UIHoverGestureRecognizer alloc] initWithTarget: self action: @selector(handleHover:)];
  23. [self addGestureRecognizer: hover];
  24. }
  25. // both registrations outlive the view (observer is unretained, handler lives on the shared GCMouse),
  26. // so clear them or a stale view could call a torn down player in UaaL
  27. - (void)teardownIndirectMouseInput
  28. {
  29. [[NSNotificationCenter defaultCenter] removeObserver: self
  30. name: GCMouseDidBecomeCurrentNotification
  31. object: nil];
  32. // the mouse is shared: after a UaaL reload a newer view may own the slot, so only clear ours
  33. for (GCMouse* mouse in GCMouse.mice)
  34. {
  35. if (mouse.mouseInput.scroll.valueChangedHandler == self.indirectScrollHandler)
  36. mouse.mouseInput.scroll.valueChangedHandler = nil;
  37. }
  38. self.indirectScrollHandler = nil;
  39. }
  40. - (void)mouseDidBecomeCurrent:(NSNotification*)note
  41. {
  42. GCMouse* mouse = note.object;
  43. if (mouse != nil)
  44. [self attachScrollHandlerToMouse: mouse];
  45. }
  46. - (void)attachScrollHandlerToMouse:(GCMouse*)mouse
  47. {
  48. // one block per view, reused across mice so teardown can match ours by pointer. dispatch to the
  49. // main thread since GCMouse may fire on any queue. kScrollScale is a placeholder until measured.
  50. if (self.indirectScrollHandler == nil)
  51. {
  52. __weak UnityView* weakSelf = self;
  53. self.indirectScrollHandler = ^(GCControllerDirectionPad* dpad, float xValue, float yValue) {
  54. const float kScrollScale = 1.0f;
  55. const float dx = xValue * kScrollScale;
  56. const float dy = yValue * kScrollScale;
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. // only forward while the pointer is over this view, else a scroll over a sibling
  59. // view would still drive Unity in UaaL
  60. UnityView* strongSelf = weakSelf;
  61. if (strongSelf == nil || !strongSelf.indirectPointerInside)
  62. return;
  63. UnitySendScrollWheel(dx, dy);
  64. });
  65. };
  66. }
  67. mouse.mouseInput.scroll.valueChangedHandler = self.indirectScrollHandler;
  68. }
  69. - (void)handleHover:(UIHoverGestureRecognizer*)recognizer
  70. {
  71. // began/changed = inside, ended/cancelled/failed = left; the scroll handler gates on this
  72. self.indirectPointerInside = recognizer.state == UIGestureRecognizerStateBegan ||
  73. recognizer.state == UIGestureRecognizerStateChanged;
  74. if (!self.indirectPointerInside)
  75. return;
  76. // pass the raw UIKit point; UnitySendMousePosition applies the touch path's transform so it
  77. // matches ScreenManager extents
  78. CGPoint location = [recognizer locationInView: self];
  79. UnitySendMousePosition((float)location.x, (float)location.y);
  80. }
  81. @end
  82. @implementation UnityView (iOS)
  83. - (void)willRotateToOrientation:(UIInterfaceOrientation)toOrientation fromOrientation:(UIInterfaceOrientation)fromOrientation;
  84. {
  85. // to support the case of interface and unity content orientation being different
  86. // we will cheat a bit:
  87. // we will calculate transform between interface orientations and apply it to unity view orientation
  88. // you can still tweak unity view as you see fit in AppController, but this is what you want in 99% of cases
  89. ScreenOrientation to = ConvertToUnityScreenOrientation(toOrientation);
  90. ScreenOrientation from = ConvertToUnityScreenOrientation(fromOrientation);
  91. if (fromOrientation == UIInterfaceOrientationUnknown)
  92. _curOrientation = to;
  93. else
  94. _curOrientation = OrientationAfterTransform(_curOrientation, TransformBetweenOrientations(from, to));
  95. _viewIsRotating = YES;
  96. }
  97. - (void)didRotate
  98. {
  99. // if we are using metal display link we will delay actual unity-side resizing to happen before rendering
  100. if (_shouldRecreateView && !GetAppController().unityUsesMetalDisplayLink)
  101. {
  102. // recreateRenderingSurface expects layer's drawableSize to be set to proper value
  103. // and updateLayerDrawableSizeFromBounds does exactly that
  104. // note that normally we go through recreateRenderingSurfaceIfNeeded
  105. // which does call updateLayerDrawableSizeFromBounds
  106. [self updateLayerDrawableSizeFromBounds];
  107. [self updateUnityBackbufferSize];
  108. [self recreateRenderingSurface];
  109. }
  110. _viewIsRotating = NO;
  111. }
  112. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { UnitySendTouchesBegin(touches, event); }
  113. - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { UnitySendTouchesEnded(touches, event); }
  114. - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event { UnitySendTouchesCancelled(touches, event); }
  115. - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { UnitySendTouchesMoved(touches, event); }
  116. @end
  117. #endif // PLATFORM_IOS || PLATFORM_VISIONOS