UnityViewControllerListener.mm 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "UnityViewControllerListener.h"
  2. #include <UIKit/UIApplication.h>
  3. #define DEFINE_NOTIFICATION(name) extern "C" __attribute__((visibility ("default"))) NSString* const name = @#name;
  4. DEFINE_NOTIFICATION(kUnityViewWillLayoutSubviews);
  5. DEFINE_NOTIFICATION(kUnityViewDidLayoutSubviews);
  6. DEFINE_NOTIFICATION(kUnityViewWillDisappear);
  7. DEFINE_NOTIFICATION(kUnityViewDidDisappear);
  8. DEFINE_NOTIFICATION(kUnityViewWillAppear);
  9. DEFINE_NOTIFICATION(kUnityViewDidAppear);
  10. DEFINE_NOTIFICATION(kUnityInterfaceWillChangeOrientation);
  11. DEFINE_NOTIFICATION(kUnityInterfaceDidChangeOrientation);
  12. #undef DEFINE_NOTIFICATION
  13. void UnityRegisterViewControllerListener(id<UnityViewControllerListener> obj)
  14. {
  15. #define REGISTER_SELECTOR(sel, notif_name) \
  16. if([obj respondsToSelector:sel]) \
  17. [[NSNotificationCenter defaultCenter] addObserver:obj selector:sel name:notif_name object:nil]; \
  18. REGISTER_SELECTOR(@selector(viewWillLayoutSubviews:), kUnityViewWillLayoutSubviews);
  19. REGISTER_SELECTOR(@selector(viewDidLayoutSubviews:), kUnityViewDidLayoutSubviews);
  20. REGISTER_SELECTOR(@selector(viewWillDisappear:), kUnityViewWillDisappear);
  21. REGISTER_SELECTOR(@selector(viewDidDisappear:), kUnityViewDidDisappear);
  22. REGISTER_SELECTOR(@selector(viewWillAppear:), kUnityViewWillAppear);
  23. REGISTER_SELECTOR(@selector(viewDidAppear:), kUnityViewDidAppear);
  24. REGISTER_SELECTOR(@selector(interfaceWillChangeOrientation:), kUnityInterfaceWillChangeOrientation);
  25. REGISTER_SELECTOR(@selector(interfaceDidChangeOrientation:), kUnityInterfaceDidChangeOrientation);
  26. #undef REGISTER_SELECTOR
  27. }
  28. void UnityUnregisterViewControllerListener(id<UnityViewControllerListener> obj)
  29. {
  30. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewWillLayoutSubviews object: nil];
  31. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewDidLayoutSubviews object: nil];
  32. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewWillDisappear object: nil];
  33. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewDidDisappear object: nil];
  34. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewWillAppear object: nil];
  35. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityViewDidAppear object: nil];
  36. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityInterfaceWillChangeOrientation object: nil];
  37. [[NSNotificationCenter defaultCenter] removeObserver: obj name: kUnityInterfaceDidChangeOrientation object: nil];
  38. }
  39. extern void AppController_SendUnityViewControllerNotification(NSString* name);
  40. @implementation UnityViewControllerNotificationsDefaultSender
  41. - (void)onViewWillLayoutSubviews
  42. {
  43. AppController_SendUnityViewControllerNotification(kUnityViewWillLayoutSubviews);
  44. }
  45. - (void)onViewDidLayoutSubviews
  46. {
  47. AppController_SendUnityViewControllerNotification(kUnityViewDidLayoutSubviews);
  48. }
  49. - (void)onViewDidDisappear:(BOOL)animated
  50. {
  51. AppController_SendUnityViewControllerNotification(kUnityViewDidDisappear);
  52. }
  53. - (void)onViewWillDisappear:(BOOL)animated
  54. {
  55. AppController_SendUnityViewControllerNotification(kUnityViewWillDisappear);
  56. }
  57. - (void)onViewDidAppear:(BOOL)animated
  58. {
  59. AppController_SendUnityViewControllerNotification(kUnityViewDidAppear);
  60. }
  61. - (void)onViewWillAppear:(BOOL)animated
  62. {
  63. AppController_SendUnityViewControllerNotification(kUnityViewWillAppear);
  64. }
  65. @end