OneWaySDK.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. public class OneWaySDK : MonoBehaviour
  8. {
  9. public static AndroidJavaObject _plugin;
  10. // Use this for initialization
  11. void Start () {
  12. }
  13. // Update is called once per frame
  14. void Update () {
  15. }
  16. public enum OneWaySDKPlacementState
  17. {
  18. //A state that indicates that the placement is ready to show an ad. The `show:` selector can be called.
  19. kOneWaySDKPlacementStateReady = 0,
  20. // A state that indicates that no state is information is available.
  21. // @warning This state can that OneWaySDK is not initialized or that the placement is not correctly configured in the oneway ads admin tool.
  22. kOneWaySDKPlacementStateNotAvailable,
  23. // A state that indicates that the placement is currently disabled. The placement can be enabled in the oneway ads admin tools.
  24. kOneWaySDKPlacementStateDisabled,
  25. // A state that indicates that the placement is not currently ready, but will be in the future.
  26. // @note This state most likely indicates that the ad content is currently caching.
  27. kOneWaySDKPlacementStateWaiting,
  28. // A state that indicates that the placement is properly configured, but there are currently no ads available for the placement.
  29. kOneWaySDKPlacementStateNoFill
  30. };
  31. public enum OneWaySDKFinishState
  32. {
  33. kOneWaySDKFinishStateError = 0,
  34. kOneWaySDKFinishStateSkipped ,
  35. kOneWaySDKFinishStateCompleted
  36. };
  37. public enum OneWaySDKError
  38. {
  39. // An error that indicates failure due to `OneWaySDK` currently being uninitialized.
  40. kOneWaySDKErrorNotInitialized = 0,
  41. // An error that indicates failure due to a failure in the initialization process.
  42. kOneWaySDKErrorInitializedFailed,
  43. // An error that indicates failure due to attempting to initialize `OneWaySDK` with invalid parameters.
  44. kOneWaySDKErrorInvalidArgument,
  45. // An error that indicates failure of the video player.
  46. kOneWaySDKErrorVideoPlayerError,
  47. // An error that indicates failure due to having attempted to initialize the `OneWaySDK` class in an invalid environment.
  48. kOneWaySDKErrorInitSanityCheckFail,
  49. // An error that indicates failure due to the presence of an ad blocker.
  50. kOneWaySDKErrorAdBlockerDetected,
  51. // An error that indicates failure due to inability to read or write a file.
  52. kOneWaySDKErrorFileIoError,
  53. // An error that indicates failure due to a bad device identifier.
  54. kOneWaySDKErrorDeviceIdError,
  55. // An error that indicates a failure when attempting to show an ad.
  56. kOneWaySDKErrorShowError,
  57. // An error that indicates an internal failure in `OneWaySDK`.
  58. kOneWaySDKErrorInternalError,
  59. // A state that indicates that the SDK is properly configured, but there are currently no ads available.
  60. kOneWaySDKCampaignNoFill
  61. }
  62. #region Constructor and Lifecycle
  63. static OneWaySDK()
  64. {
  65. // try/catch this so that we can warn users if they try to stick this script on a GO manually
  66. try
  67. {
  68. // create a new GO for our manager
  69. var go = new GameObject( "OneWaySDK" );
  70. go.AddComponent<OneWaySDK>();
  71. DontDestroyOnLoad( go );
  72. }
  73. catch( UnityException ) {
  74. Debug.LogWarning ("It looks like you have the OneWaySDK on a GameObject in your scene. Please remove the script from your scene.");
  75. }
  76. #if UNITY_ANDROID
  77. using( var pluginClass = new AndroidJavaClass( "mobi.oneway.OneWaySdkPlugin" ) )
  78. _plugin = pluginClass.CallStatic<AndroidJavaObject>( "instance" );
  79. #endif
  80. }
  81. // used to ensure the VungleManager will always be in the scene to avoid SendMessage logs if the user isn't using any events
  82. public static void noop(){}
  83. #endregion
  84. #region Events
  85. // Fired when a Vungle ad starts
  86. public static event Action <string> onOneWaySDKReadyEvent;
  87. public static event Action <string> onOneWaySDKDidStartEvent;
  88. public static event Action <string,string> onOneWaySDKDidFinishEvent;
  89. public static event Action <string,string> onOneWaySDKDidErrorEvent;
  90. #endregion
  91. void onOneWaySDKReady (string placementID) {
  92. try{
  93. onOneWaySDKReadyEvent (placementID);
  94. }catch(Exception e){
  95. print("-------------- OneWaySDK Warning : Method 'onOneWaySDKReadyEvent ' not implemented --------------"+e.Message);
  96. }
  97. }
  98. void onOneWaySDKDidStart (string placementID) {
  99. try{
  100. onOneWaySDKDidStartEvent (placementID);
  101. }catch(Exception e){
  102. Debug.LogWarning ("-------------- OneWaySDK Warning : Method 'onOneWaySDKDidStartEvent ' not implemented --------------"+e.Message);
  103. }
  104. }
  105. void onOneWaySDKDidFinish (string msg) {
  106. Dictionary<string,object> attrs = (Dictionary<string,object>) MiniJSONV.Json.Deserialize( msg );
  107. try{
  108. onOneWaySDKDidFinishEvent (attrs["placementId"].ToString(),attrs["state"].ToString());
  109. }catch(Exception e){
  110. Debug.LogWarning ("-------------- OneWaySDK Warning : Method 'onOneWaySDKDidFinishEvent ' not implemented --------------"+e.Message);
  111. }
  112. }
  113. void onOneWaySDKDidError (string msg) {
  114. Dictionary<string,object> attrs = (Dictionary<string,object>) MiniJSONV.Json.Deserialize( msg );
  115. try{
  116. onOneWaySDKDidErrorEvent (attrs["error"].ToString(),attrs["message"].ToString());
  117. }catch(Exception e){
  118. Debug.LogWarning ("-------------- OneWaySDK Warning : Method 'onOneWaySDKDidErrorEvent ' not implemented --------------"+e.Message);
  119. }
  120. }
  121. // init
  122. [DllImport ("__Internal")]
  123. private static extern void _OneWaySDKInit(string PId,bool debugMode);
  124. public static void init(string iOSPId , string androidPId, bool debugMode)
  125. {
  126. #if UNITY_IPHONE
  127. _OneWaySDKInit( iOSPId, debugMode);
  128. #elif UNITY_ANDROID
  129. _plugin.Call("init",androidPId,debugMode);
  130. #endif
  131. }
  132. // show
  133. [DllImport ("__Internal")]
  134. private static extern void _OneWaySDKShow();
  135. public static void show()
  136. {
  137. #if UNITY_IPHONE
  138. _OneWaySDKShow();
  139. #elif UNITY_ANDROID
  140. _plugin.Call("showOneWayAd");
  141. #endif
  142. }
  143. //ShowPlacementID
  144. [DllImport ("__Internal")]
  145. private static extern void _OneWaySDKShowPlacementID(string placementID);
  146. public static void showPlacementID(string placementID)
  147. {
  148. #if UNITY_IPHONE
  149. _OneWaySDKShowPlacementID(placementID);
  150. #elif UNITY_ANDROID
  151. _plugin.Call("showPlacementID",placementID);
  152. #endif
  153. }
  154. //isReady
  155. [DllImport ("__Internal")]
  156. private static extern bool _OneWaySDKIsReady();
  157. public static bool IsReady()
  158. {
  159. #if UNITY_IPHONE
  160. return _OneWaySDKIsReady();
  161. #elif UNITY_ANDROID
  162. return _plugin.Call<bool>("isVideoAvailable");
  163. #else
  164. return false;
  165. #endif
  166. }
  167. }