UnityViewControllerListener.mm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. }