説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

UnityView.mm 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "UnityView.h"
  2. #include "UnityAppController.h"
  3. #include "UnityAppController+Rendering.h"
  4. #include "OrientationSupport.h"
  5. #include "Unity/DisplayManager.h"
  6. #include "Unity/ObjCRuntime.h"
  7. extern bool _skipPresent;
  8. @implementation UnityView
  9. {
  10. CGSize _surfaceSize;
  11. }
  12. @synthesize contentOrientation = _curOrientation;
  13. - (void)onUpdateSurfaceSize:(CGSize)size
  14. {
  15. _surfaceSize = size;
  16. CGSize systemRenderSize = CGSizeMake(size.width * self.contentScaleFactor, size.height * self.contentScaleFactor);
  17. _curOrientation = (ScreenOrientation)UnityReportResizeView((unsigned)systemRenderSize.width, (unsigned)systemRenderSize.height, _curOrientation);
  18. ReportSafeAreaChangeForView(self);
  19. }
  20. - (void)boundsUpdated
  21. {
  22. [self onUpdateSurfaceSize: self.bounds.size];
  23. }
  24. - (void)initImpl:(CGRect)frame scaleFactor:(CGFloat)scale
  25. {
  26. #if !PLATFORM_TVOS
  27. self.multipleTouchEnabled = YES;
  28. self.exclusiveTouch = YES;
  29. #endif
  30. self.contentScaleFactor = scale;
  31. self.skipRendering = NO;
  32. #if PLATFORM_TVOS
  33. _curOrientation = UNITY_TVOS_ORIENTATION;
  34. #elif UNITY_VISIONOS
  35. _curOrientation = UNITY_VISIONOS_ORIENTATION;
  36. #endif
  37. [self onUpdateSurfaceSize: frame.size];
  38. }
  39. - (id)initWithFrame:(CGRect)frame scaleFactor:(CGFloat)scale;
  40. {
  41. if ((self = [super initWithFrame: frame]))
  42. [self initImpl: frame scaleFactor: scale];
  43. return self;
  44. }
  45. - (id)initWithFrame:(CGRect)frame
  46. {
  47. if ((self = [super initWithFrame: frame]))
  48. [self initImpl: frame scaleFactor: 1.0f];
  49. return self;
  50. }
  51. - (id)initFromMainScreen
  52. {
  53. #if !PLATFORM_VISIONOS
  54. CGRect frame = [UIScreen mainScreen].bounds;
  55. CGFloat scale = UnityScreenScaleFactor([UIScreen mainScreen]);
  56. #else
  57. CGRect frame = CGRectMake(0.0f, 0.0f, 1920.0f, 1080.0f);
  58. CGFloat scale = 1.0f;
  59. #endif
  60. if ((self = [super initWithFrame: frame]))
  61. [self initImpl: frame scaleFactor: scale];
  62. return self;
  63. }
  64. - (void)resumeRendering
  65. {
  66. self.skipRendering = NO;
  67. }
  68. - (void)layoutSubviews
  69. {
  70. if (_surfaceSize.width != self.bounds.size.width || _surfaceSize.height != self.bounds.size.height)
  71. _shouldRecreateView = YES;
  72. [self onUpdateSurfaceSize: self.bounds.size];
  73. for (UIView* subView in self.subviews)
  74. {
  75. if ([subView respondsToSelector: @selector(onUnityUpdateViewLayout)])
  76. [subView performSelector: @selector(onUnityUpdateViewLayout)];
  77. }
  78. [super layoutSubviews];
  79. }
  80. - (void)safeAreaInsetsDidChange
  81. {
  82. ReportSafeAreaChangeForView(self);
  83. }
  84. - (void)recreateRenderingSurfaceIfNeeded
  85. {
  86. #if !PLATFORM_VISIONOS
  87. float requestedContentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  88. #else
  89. float requestedContentScaleFactor = 1.0f;
  90. #endif
  91. if (abs(requestedContentScaleFactor - self.contentScaleFactor) > FLT_EPSILON)
  92. {
  93. self.contentScaleFactor = requestedContentScaleFactor;
  94. [self onUpdateSurfaceSize: self.bounds.size];
  95. }
  96. unsigned requestedW, requestedH; UnityGetRenderingResolution(&requestedW, &requestedH);
  97. int requestedMSAA = UnityGetDesiredMSAASampleCount(1);
  98. int requestedSRGB = UnityGetSRGBRequested();
  99. int requestedWideColor = UnityGetWideColorRequested();
  100. int requestedHDR = UnityGetHDRModeRequested();
  101. int requestedMemorylessDepth = UnityMetalMemorylessDepth();
  102. UnityDisplaySurfaceBase* surf = GetMainDisplaySurface();
  103. if (_shouldRecreateView == YES
  104. || surf->targetW != requestedW || surf->targetH != requestedH
  105. || surf->disableDepthAndStencil != UnityDisableDepthAndStencilBuffers()
  106. || surf->msaaSamples != requestedMSAA
  107. || surf->srgb != requestedSRGB
  108. || surf->wideColor != requestedWideColor
  109. || surf->hdr != requestedHDR
  110. || surf->memorylessDepth != requestedMemorylessDepth
  111. )
  112. {
  113. [self recreateRenderingSurface];
  114. }
  115. }
  116. - (void)recreateRenderingSurface
  117. {
  118. auto controller = GetAppController();
  119. if (controller.engineLoadState >= kUnityEngineLoadStateRenderingInitialized)
  120. {
  121. unsigned requestedW, requestedH;
  122. UnityGetRenderingResolution(&requestedW, &requestedH);
  123. RenderingSurfaceParams params =
  124. {
  125. .msaaSampleCount = UnityGetDesiredMSAASampleCount(1),
  126. .renderW = (int)requestedW,
  127. .renderH = (int)requestedH,
  128. .srgb = UnityGetSRGBRequested(),
  129. .wideColor = UnityGetWideColorRequested(),
  130. .hdr = UnityGetHDRModeRequested(),
  131. .metalFramebufferOnly = UnityMetalFramebufferOnly(),
  132. .metalMemorylessDepth = UnityMetalMemorylessDepth(),
  133. .disableDepthAndStencil = UnityDisableDepthAndStencilBuffers(),
  134. .useCVTextureCache = 0,
  135. };
  136. APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(onBeforeMainDisplaySurfaceRecreate, &params);
  137. [GetMainDisplay() recreateSurface: params];
  138. // actually poke unity about updated back buffer and notify that extents were changed
  139. UnityReportBackbufferChange(GetMainDisplaySurface()->unityColorBuffer, GetMainDisplaySurface()->unityDepthBuffer);
  140. APP_CONTROLLER_RENDER_PLUGIN_METHOD(onAfterMainDisplaySurfaceRecreate);
  141. if (controller.engineLoadState >= kUnityEngineLoadStateAppReady)
  142. {
  143. // seems like ios sometimes got confused about abrupt swap chain destroy
  144. // draw 2 times to fill "both" buffers (we assume double buffering)
  145. // present only once to make sure correct image goes to CA
  146. // if we are calling this from inside repaint, second draw and present will be done automatically
  147. _skipPresent = true;
  148. // we may be asked to recreate surface while paused (in the background)
  149. // like changing device orientation while showing some system dialog
  150. // in this case we still want to redraw contents to avoid view stretching
  151. const bool wasPaused = UnityIsPaused();
  152. // please note that we still need to pretend we did come from displaylink to make sure vsync magic works
  153. // NOTE: unity does handle "draw frame with exact same timestamp" just fine
  154. UnityDisplayLinkCallback(controller.unityDisplayLink.timestamp);
  155. UnityRepaint();
  156. // if we are inside actual repaint: we are done (second draw and present will be done automatically)
  157. // otherwise we need the second repaint, actualy doing present this time
  158. _skipPresent = false;
  159. if (_viewIsRotating || wasPaused)
  160. {
  161. UnityDisplayLinkCallback(GetAppController().unityDisplayLink.timestamp);
  162. UnityRepaint();
  163. }
  164. }
  165. }
  166. _shouldRecreateView = NO;
  167. }
  168. @end
  169. @implementation UnityView (Deprecated)
  170. - (void)recreateGLESSurfaceIfNeeded { [self recreateRenderingSurfaceIfNeeded]; }
  171. - (void)recreateGLESSurface { [self recreateRenderingSurface]; }
  172. @end
  173. static Class UnityRenderingView_LayerClassMTL(id self_, SEL _cmd)
  174. {
  175. return NSClassFromString(@"CAMetalLayer");
  176. }
  177. static Class UnityRenderingView_LayerClassNULL(id self_, SEL _cmd)
  178. {
  179. return NSClassFromString(@"CALayer");
  180. }
  181. @implementation UnityRenderingView
  182. + (Class)layerClass
  183. {
  184. return nil;
  185. }
  186. + (void)InitializeForAPI:(UnityRenderingAPI)api
  187. {
  188. IMP layerClassImpl = api == apiMetal ? (IMP)UnityRenderingView_LayerClassMTL : (IMP)UnityRenderingView_LayerClassNULL;
  189. class_replaceMethod(object_getClass([UnityRenderingView class]), @selector(layerClass), layerClassImpl, UIView_LayerClass_Enc);
  190. }
  191. @end
  192. void ReportSafeAreaChangeForView(UIView* view)
  193. {
  194. CGRect safeArea = ComputeSafeArea(view);
  195. UnityReportSafeAreaChange(safeArea.origin.x, safeArea.origin.y,
  196. safeArea.size.width, safeArea.size.height);
  197. #if !PLATFORM_VISIONOS
  198. if (UnityDeviceHasCutout())
  199. {
  200. CGSize cutoutSizeRatio = GetCutoutToScreenRatio();
  201. if (!CGSizeEqualToSize(CGSizeZero, cutoutSizeRatio))
  202. {
  203. const float w = ([UIScreen mainScreen].nativeBounds.size.width * cutoutSizeRatio.width);
  204. const float h = ([UIScreen mainScreen].nativeBounds.size.height * cutoutSizeRatio.height);
  205. // Apple's cutouts are currently centred on the horizontal, and stuck to the top of the vertical, hence this positioning.
  206. const float x = (([UIScreen mainScreen].nativeBounds.size.width - w) / 2);
  207. const float y = ([UIScreen mainScreen].nativeBounds.size.height - h);
  208. UnityReportDisplayCutouts(&x, &y, &w, &h, 1);
  209. return;
  210. }
  211. }
  212. #endif
  213. UnityReportDisplayCutouts(nullptr, nullptr, nullptr, nullptr, 0);
  214. }
  215. CGRect ComputeSafeArea(UIView* view)
  216. {
  217. CGSize screenSize = view.bounds.size;
  218. CGRect screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
  219. UIEdgeInsets insets = [view safeAreaInsets];
  220. float insetLeft = insets.left, insetBottom = insets.bottom;
  221. float insetWidth = insetLeft + insets.right, insetHeight = insetBottom + insets.top;
  222. #if PLATFORM_IOS && !PLATFORM_VISIONOS
  223. // pre-iOS 15 there is a bug with safeAreaInsets when coupled with the way unity handles forced orientation
  224. // when we create/show new ViewController with fixed orientation, safeAreaInsets include status bar always
  225. // alas, we did not find a good way to work around that (this can be seen even in View Debugging: Safe Area would have status bar accounted for)
  226. // we know for sure that status bar height is 20 (at least on ios16 or older), so we can check if the safe area
  227. // includes inset of this size while status bar should be hidden, resetting vertical insets in this case
  228. if (@available(iOS 15, *))
  229. {
  230. // everything works as expected
  231. }
  232. else if (view.window.windowScene.statusBarManager.statusBarHidden && fabsf(insetHeight - 20) < 1e-6f)
  233. {
  234. insetHeight = insetBottom = 0.0f;
  235. }
  236. #endif
  237. // Unity uses bottom left as the origin
  238. screenRect = CGRectOffset(screenRect, insetLeft, insetBottom);
  239. screenRect.size.width -= insetWidth;
  240. screenRect.size.height -= insetHeight;
  241. const float scale = view.contentScaleFactor;
  242. // Truncate safe area size because in some cases (for example when Display zoom is turned on)
  243. // it might become larger than Screen.width/height which are returned as ints.
  244. screenRect.origin.x = (unsigned)(screenRect.origin.x * scale);
  245. screenRect.origin.y = (unsigned)(screenRect.origin.y * scale);
  246. screenRect.size.width = (unsigned)(screenRect.size.width * scale);
  247. screenRect.size.height = (unsigned)(screenRect.size.height * scale);
  248. return screenRect;
  249. }
  250. // Apple does not provide the cutout width and height in points/pixels. They *do* however list the
  251. // size of the cutout and screen in mm for accessory makers. We can use this information to calculate the percentage of the screen is cutout.
  252. // This information can be found here - https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf
  253. CGSize GetCutoutToScreenRatio()
  254. {
  255. switch (UnityDeviceGeneration())
  256. {
  257. case deviceiPhone14:
  258. return CGSizeMake(0.415, 0.04);
  259. case deviceiPhone14Plus:
  260. return CGSizeMake(0.377, 0.036);
  261. case deviceiPhone14Pro:
  262. case deviceiPhone15:
  263. case deviceiPhone15Pro:
  264. return CGSizeMake(0.318, 0.057);
  265. case deviceiPhone14ProMax:
  266. case deviceiPhone15ProMax:
  267. case deviceiPhone15Plus:
  268. return CGSizeMake(0.292, 0.052);
  269. case deviceiPhone13ProMax:
  270. return CGSizeMake(0.373, 0.036);
  271. case deviceiPhone13Pro:
  272. case deviceiPhone13:
  273. return CGSizeMake(0.4148, 0.0399);
  274. case deviceiPhone13Mini:
  275. return CGSizeMake(0.4644, 0.0462);
  276. case deviceiPhone12ProMax:
  277. return CGSizeMake(0.4897, 0.0346);
  278. case deviceiPhone12Pro:
  279. case deviceiPhone12:
  280. return CGSizeMake(0.5393, 0.0379);
  281. case deviceiPhone12Mini:
  282. return CGSizeMake(0.604, 0.0424);
  283. case deviceiPhone11ProMax:
  284. return CGSizeMake(0.5057, 0.0335);
  285. case deviceiPhone11Pro:
  286. return CGSizeMake(0.5583, 0.037);
  287. case deviceiPhone11:
  288. case deviceiPhoneXR:
  289. return CGSizeMake(0.5568, 0.0398);
  290. case deviceiPhoneXSMax:
  291. return CGSizeMake(0.4884, 0.0333);
  292. case deviceiPhoneX:
  293. case deviceiPhoneXS:
  294. return CGSizeMake(0.5391, 0.0368);
  295. default:
  296. NSCAssert(!UnityDeviceHasCutout(), @"Device has a cutout, but no ratio has been added for it.");
  297. return CGSizeZero;
  298. }
  299. }