UnityAdsUnityWrapper.mm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // UnityAdsUnityWrapper.m
  3. // Copyright(c) 2015 Unity Technologies. All rights reserved.
  4. //
  5. #import "UnityAdsConfig.h"
  6. extern "C" {
  7. typedef void (*UnityAdsReadyCallback)(const char * placementId);
  8. typedef void (*UnityAdsDidErrorCallback)(long rawError, const char * message);
  9. typedef void (*UnityAdsDidStartCallback)(const char * placementId);
  10. typedef void (*UnityAdsDidFinishCallback)(const char * placementId, long rawFinishState);
  11. }
  12. #define DONTSTRIP __attribute__((used))
  13. #define EXPORT __attribute__((visibility("default")))
  14. #if USE_UNITYADS
  15. #import "UnityAppController.h"
  16. #import "Unity/UnityInterface.h"
  17. #import "UnityAds/UnityAds.h"
  18. #import "UnityAds/UADSMetaData.h"
  19. extern "C" {
  20. const char * UnityAdsCopyString(const char * string)
  21. {
  22. char * copy = (char*)malloc(strlen(string) + 1);
  23. strcpy(copy, string);
  24. return copy;
  25. }
  26. static UnityAdsReadyCallback readyCallback = NULL;
  27. static UnityAdsDidErrorCallback errorCallback = NULL;
  28. static UnityAdsDidStartCallback startCallback = NULL;
  29. static UnityAdsDidFinishCallback finishCallback = NULL;
  30. }
  31. @interface UnityAdsUnityWrapperDelegate : NSObject<UnityAdsDelegate>
  32. @end
  33. @implementation UnityAdsUnityWrapperDelegate
  34. - (void)unityAdsReady:(NSString *)placementId
  35. {
  36. if (readyCallback != NULL)
  37. {
  38. const char * rawPlacementId = UnityAdsCopyString([placementId UTF8String]);
  39. readyCallback(rawPlacementId);
  40. free((void*)rawPlacementId);
  41. }
  42. }
  43. - (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message
  44. {
  45. if (errorCallback != NULL)
  46. {
  47. const char * rawMessage = UnityAdsCopyString([message UTF8String]);
  48. errorCallback(error, rawMessage);
  49. free((void*)rawMessage);
  50. }
  51. }
  52. - (void)unityAdsDidStart:(NSString *)placementId
  53. {
  54. UnityPause(1);
  55. if (startCallback != NULL)
  56. {
  57. const char * rawPlacementId = UnityAdsCopyString([placementId UTF8String]);
  58. startCallback(rawPlacementId);
  59. free((void*)rawPlacementId);
  60. }
  61. }
  62. - (void)unityAdsDidFinish:(NSString *)placementId withFinishState:(UnityAdsFinishState)state
  63. {
  64. UnityPause(0);
  65. if (finishCallback != NULL)
  66. {
  67. const char * rawPlacementId = UnityAdsCopyString([placementId UTF8String]);
  68. finishCallback(rawPlacementId, state);
  69. free((void*)rawPlacementId);
  70. }
  71. }
  72. @end
  73. extern "C" {
  74. EXPORT DONTSTRIP void UnityAdsEngineInitialize(const char * gameId, bool testMode)
  75. {
  76. static UnityAdsUnityWrapperDelegate * unityAdsUnityWrapperDelegate = NULL;
  77. if (unityAdsUnityWrapperDelegate == NULL)
  78. {
  79. unityAdsUnityWrapperDelegate = [[UnityAdsUnityWrapperDelegate alloc] init];
  80. }
  81. [UnityAds initialize: [NSString stringWithUTF8String: gameId] delegate: unityAdsUnityWrapperDelegate testMode: testMode];
  82. }
  83. EXPORT DONTSTRIP void UnityAdsEngineShow(const char * placementId)
  84. {
  85. if (placementId == NULL)
  86. {
  87. [UnityAds show: UnityGetGLViewController()];
  88. }
  89. else
  90. {
  91. [UnityAds show: UnityGetGLViewController() placementId: [NSString stringWithUTF8String: placementId]];
  92. }
  93. }
  94. EXPORT DONTSTRIP bool UnityAdsEngineGetDebugMode()
  95. {
  96. return [UnityAds getDebugMode];
  97. }
  98. EXPORT DONTSTRIP void UnityAdsEngineSetDebugMode(bool debugMode)
  99. {
  100. [UnityAds setDebugMode: debugMode];
  101. }
  102. EXPORT DONTSTRIP bool UnityAdsEngineIsSupported()
  103. {
  104. return [UnityAds isSupported];
  105. }
  106. EXPORT DONTSTRIP bool UnityAdsEngineIsReady(const char * placementId)
  107. {
  108. if (placementId == NULL)
  109. {
  110. return [UnityAds isReady];
  111. }
  112. else
  113. {
  114. return [UnityAds isReady: [NSString stringWithUTF8String: placementId]];
  115. }
  116. }
  117. EXPORT DONTSTRIP long UnityAdsEngineGetPlacementState(const char * placementId)
  118. {
  119. if (placementId == NULL)
  120. {
  121. return [UnityAds getPlacementState];
  122. }
  123. else
  124. {
  125. return [UnityAds getPlacementState: [NSString stringWithUTF8String: placementId]];
  126. }
  127. }
  128. EXPORT DONTSTRIP const char * UnityAdsEngineGetVersion()
  129. {
  130. static const char * unityAdsVersion = NULL;
  131. if (unityAdsVersion == NULL)
  132. {
  133. unityAdsVersion = UnityAdsCopyString([[UnityAds getVersion] UTF8String]);
  134. }
  135. return unityAdsVersion;
  136. }
  137. EXPORT DONTSTRIP bool UnityAdsEngineIsInitialized()
  138. {
  139. return [UnityAds isInitialized];
  140. }
  141. EXPORT DONTSTRIP void UnityAdsEngineSetMetaData(const char * category, const char * data)
  142. {
  143. if (category != NULL && data != NULL)
  144. {
  145. UADSMetaData* metaData = [[UADSMetaData alloc] initWithCategory: [NSString stringWithUTF8String: category]];
  146. NSDictionary* json = [NSJSONSerialization JSONObjectWithData: [[NSString stringWithUTF8String: data] dataUsingEncoding: NSUTF8StringEncoding] options: 0 error: nil];
  147. for (id key in json)
  148. {
  149. [metaData set: key value: [json objectForKey: key]];
  150. }
  151. [metaData commit];
  152. }
  153. }
  154. EXPORT DONTSTRIP void UnityAdsEngineSetReadyCallback(UnityAdsReadyCallback callback)
  155. {
  156. readyCallback = callback;
  157. }
  158. EXPORT DONTSTRIP void UnityAdsEngineSetDidErrorCallback(UnityAdsDidErrorCallback callback)
  159. {
  160. errorCallback = callback;
  161. }
  162. EXPORT DONTSTRIP void UnityAdsEngineSetDidStartCallback(UnityAdsDidStartCallback callback)
  163. {
  164. startCallback = callback;
  165. }
  166. EXPORT DONTSTRIP void UnityAdsEngineSetDidFinishCallback(UnityAdsDidFinishCallback callback)
  167. {
  168. finishCallback = callback;
  169. }
  170. }
  171. #else
  172. extern "C" {
  173. EXPORT DONTSTRIP void UnityAdsEngineInitialize(const char * gameId, bool testMode) {}
  174. EXPORT DONTSTRIP void UnityAdsEngineShow(const char * placementId) {}
  175. EXPORT DONTSTRIP bool UnityAdsEngineGetDebugMode() { return false; }
  176. EXPORT DONTSTRIP void UnityAdsEngineSetDebugMode(bool debugMode) {}
  177. EXPORT DONTSTRIP bool UnityAdsEngineIsSupported() { return false; }
  178. EXPORT DONTSTRIP bool UnityAdsEngineIsReady(const char * placementId) { return false; }
  179. EXPORT DONTSTRIP long UnityAdsEngineGetPlacementState(const char * placementId) { return -1; }
  180. EXPORT DONTSTRIP const char * UnityAdsEngineGetVersion() { return NULL; }
  181. EXPORT DONTSTRIP bool UnityAdsEngineIsInitialized() { return false; }
  182. EXPORT DONTSTRIP void UnityAdsEngineSetMetaData(const char * category, const char * data) {}
  183. EXPORT DONTSTRIP void UnityAdsEngineSetReadyCallback(UnityAdsReadyCallback callback) {}
  184. EXPORT DONTSTRIP void UnityAdsEngineSetDidErrorCallback(UnityAdsDidErrorCallback callback) {}
  185. EXPORT DONTSTRIP void UnityAdsEngineSetDidStartCallback(UnityAdsDidStartCallback callback) {}
  186. EXPORT DONTSTRIP void UnityAdsEngineSetDidFinishCallback(UnityAdsDidFinishCallback callback) {}
  187. }
  188. #endif