Example.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using Godot;
  2. using System;
  3. using System.Collections;
  4. public class Example : Control
  5. {
  6. private Node MobileAds;
  7. IDictionary config;
  8. private Button EnableBanner;
  9. private Button DisableBanner;
  10. private Button ShowBanner;
  11. private Button HideBanner;
  12. private Button Interstitial;
  13. private Button Rewarded;
  14. private Button RewardedInterstitial;
  15. private Button RequestUserConsent;
  16. private Button ResetConsentState;
  17. private RichTextLabel Advice;
  18. private AudioStreamPlayer Music;
  19. private CheckBox BannerPosition;
  20. private ItemList BannerSizes;
  21. private void GetAllNodes()
  22. {
  23. MobileAds = (Node) GetNode("/root/MobileAds");
  24. config = (IDictionary) MobileAds.Get("config");
  25. EnableBanner = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Banner/EnableBanner");
  26. DisableBanner = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Banner/DisableBanner");
  27. ShowBanner = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Banner2/ShowBanner");
  28. HideBanner = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Banner2/HideBanner");
  29. Interstitial = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Interstitial");
  30. Rewarded = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/Rewarded");
  31. RewardedInterstitial = (Button) GetNode("Background/TabContainer/AdFormats/VBoxContainer/RewardedInterstitial");
  32. RequestUserConsent = (Button) GetNode("Background/TabContainer/UMP/VBoxContainer/RequestUserConsent");
  33. ResetConsentState = (Button) GetNode("Background/TabContainer/UMP/VBoxContainer/ResetConsentState");
  34. Advice = (RichTextLabel) GetNode("Background/Advice");
  35. Music = (AudioStreamPlayer) GetNode("Music");
  36. BannerPosition = (CheckBox) GetNode("Background/TabContainer/Banner/VBoxContainer/Position");
  37. BannerSizes = (ItemList) GetNode("Background/TabContainer/Banner/VBoxContainer/BannerSizes");
  38. }
  39. private void _add_text_Advice_Node(String text_value)
  40. {
  41. Advice.BbcodeText += text_value + "\n";
  42. }
  43. public override void _Ready()
  44. {
  45. GetAllNodes();
  46. OS.CenterWindow();
  47. Music.Play();
  48. foreach(String banner_size in (IEnumerable) ((Godot.Object)MobileAds.Get("AdMobSettings")).Get("BANNER_SIZE"))
  49. {
  50. BannerSizes.AddItem(banner_size);
  51. }
  52. if (OS.GetName() == "Android" || OS.GetName() == "iOS"){
  53. BannerPosition.Pressed = Convert.ToBoolean(((IDictionary) config["banner"])["position"]);
  54. MobileAds.Call("request_user_consent");
  55. MobileAds.Connect("consent_info_update_failure", this, nameof(_on_MobileAds_consent_info_update_failure));
  56. MobileAds.Connect("consent_status_changed", this, nameof(_on_MobileAds_consent_status_changed));
  57. MobileAds.Connect("banner_loaded", this, nameof(_on_MobileAds_banner_loaded));
  58. MobileAds.Connect("banner_destroyed", this, nameof(_on_MobileAds_banner_destroyed));
  59. MobileAds.Connect("interstitial_loaded", this, nameof(_on_MobileAds_interstitial_loaded));
  60. MobileAds.Connect("interstitial_closed", this, nameof(_on_MobileAds_interstitial_closed));
  61. MobileAds.Connect("rewarded_ad_loaded", this, nameof(_on_MobileAds_rewarded_ad_loaded));
  62. MobileAds.Connect("rewarded_ad_closed", this, nameof(_on_MobileAds_rewarded_ad_closed));
  63. MobileAds.Connect("rewarded_interstitial_ad_loaded", this, nameof(_on_MobileAds_rewarded_interstitial_ad_loaded));
  64. MobileAds.Connect("rewarded_interstitial_ad_closed", this, nameof(_on_MobileAds_rewarded_interstitial_ad_closed));
  65. MobileAds.Connect("user_earned_rewarded", this, nameof(_on_MobileAds_user_earned_rewarded));
  66. MobileAds.Connect("initialization_complete", this, nameof(_on_MobileAds_initialization_complete));
  67. }
  68. else
  69. {
  70. _add_text_Advice_Node("AdMob only works on Android or iOS devices!");
  71. }
  72. }
  73. private void _on_MobileAds_initialization_complete(int status, String _adapter_name)
  74. {
  75. if (status == (int)((IDictionary) ((Godot.Object)MobileAds.Get("AdMobSettings")).Get("INITIALIZATION_STATUS"))["READY"])
  76. {
  77. MobileAds.Call("load_interstitial");
  78. MobileAds.Call("load_rewarded");
  79. MobileAds.Call("load_rewarded_interstitial");
  80. _add_text_Advice_Node("AdMob initialized on C#! With parameters:");
  81. _add_text_Advice_Node("is_for_child_directed_treatment: " + ((IDictionary) config["general"])["is_for_child_directed_treatment"].ToString());
  82. _add_text_Advice_Node("is_test_europe_user_consent: " + ((IDictionary) config["general"])["is_test_europe_user_consent"].ToString());
  83. _add_text_Advice_Node("max_ad_content_rating: " + ((IDictionary) config["general"])["max_ad_content_rating"].ToString());
  84. _add_text_Advice_Node("instance_id: " + GetInstanceId().ToString());
  85. EnableBanner.Disabled = false;
  86. BannerPosition.Disabled = false;
  87. RequestUserConsent.Disabled = false;
  88. ResetConsentState.Disabled = false;
  89. }
  90. else
  91. {
  92. _add_text_Advice_Node("AdMob not initialized, check your configuration");
  93. }
  94. _add_text_Advice_Node("---------------------------------------------------");
  95. }
  96. private void _on_MobileAds_interstitial_loaded()
  97. {
  98. _add_text_Advice_Node("Interstitial loaded");
  99. Interstitial.Disabled = false;
  100. }
  101. private void _on_MobileAds_interstitial_closed()
  102. {
  103. MobileAds.Call("load_interstitial");
  104. _add_text_Advice_Node("Interstitial closed");
  105. }
  106. private void _on_Interstitial_pressed()
  107. {
  108. MobileAds.Call("show_interstitial");
  109. Interstitial.Disabled = true;
  110. }
  111. private void reset_banner_buttons()
  112. {
  113. DisableBanner.Disabled = true;
  114. EnableBanner.Disabled = false;
  115. ShowBanner.Disabled = true;
  116. HideBanner.Disabled = true;
  117. }
  118. private void _on_MobileAds_banner_destroyed()
  119. {
  120. reset_banner_buttons();
  121. _add_text_Advice_Node("Banner destroyed");
  122. }
  123. private void _on_MobileAds_banner_loaded()
  124. {
  125. DisableBanner.Disabled = false;
  126. EnableBanner.Disabled = true;
  127. ShowBanner.Disabled = false;
  128. HideBanner.Disabled = false;
  129. _add_text_Advice_Node("Banner loaded");
  130. _add_text_Advice_Node("Banner width: " + MobileAds.Call("get_banner_width"));
  131. _add_text_Advice_Node("Banner height: " + MobileAds.Call("get_banner_height"));
  132. _add_text_Advice_Node("Banner width in pixels: " + MobileAds.Call("get_banner_width_in_pixels"));
  133. _add_text_Advice_Node("Banner height in pixels: " + MobileAds.Call("get_banner_height_in_pixels"));
  134. }
  135. private void _on_EnableBanner_pressed()
  136. {
  137. EnableBanner.Disabled = true;
  138. MobileAds.Call("load_banner");
  139. }
  140. private void _on_DisableBanner_pressed()
  141. {
  142. DisableBanner.Disabled = true;
  143. EnableBanner.Disabled = false;
  144. MobileAds.Call("destroy_banner");
  145. }
  146. private void _on_Rewarded_pressed()
  147. {
  148. MobileAds.Call("show_rewarded");
  149. Rewarded.Disabled = true;
  150. }
  151. private void _on_RewardedInterstitial_pressed()
  152. {
  153. MobileAds.Call("show_rewarded_interstitial");
  154. RewardedInterstitial.Disabled = true;
  155. }
  156. private void _on_MobileAds_rewarded_ad_loaded()
  157. {
  158. _add_text_Advice_Node("Rewarded ad loaded");
  159. Rewarded.Disabled = false;
  160. }
  161. private void _on_MobileAds_rewarded_ad_closed()
  162. {
  163. MobileAds.Call("load_rewarded");
  164. _add_text_Advice_Node("Rewarded ad closed");
  165. }
  166. private void _on_MobileAds_rewarded_interstitial_ad_loaded()
  167. {
  168. _add_text_Advice_Node("Rewarded Interstitial ad loaded");
  169. RewardedInterstitial.Disabled = false;
  170. }
  171. private void _on_MobileAds_rewarded_interstitial_ad_closed()
  172. {
  173. MobileAds.Call("load_rewarded_interstitial");
  174. _add_text_Advice_Node("Rewarded Interstitial ad closed");
  175. }
  176. private void _on_MobileAds_user_earned_rewarded(String currency, int amount)
  177. {
  178. Advice.BbcodeText += "EARNED " + currency + " with amount: " + amount.ToString() + "\n";
  179. }
  180. private void _on_MobileAds_consent_info_update_failure(int _error_code, String error_message)
  181. {
  182. _add_text_Advice_Node("Request Consent from European Users failure: " + error_message);
  183. _add_text_Advice_Node("---------------------------------------------------");
  184. }
  185. private void _on_MobileAds_consent_status_changed(String status_message)
  186. {
  187. _add_text_Advice_Node(status_message);
  188. }
  189. private void _on_BannerSizes_item_selected(int index)
  190. {
  191. if ((bool) MobileAds.Call("get_is_initialized"))
  192. {
  193. String item_text = (String) BannerSizes.GetItemText(index);
  194. ((IDictionary)config["banner"])["size"] = index;
  195. _add_text_Advice_Node("Banner Size changed:" + item_text);
  196. if ((bool) MobileAds.Call("get_is_banner_loaded"))
  197. {
  198. MobileAds.Call("load_banner");
  199. }
  200. }
  201. }
  202. private void _on_ResetConsentState_pressed()
  203. {
  204. MobileAds.Call("reset_consent_state", true);
  205. }
  206. private void _on_RequestUserConsent_pressed()
  207. {
  208. MobileAds.Call("request_user_consent");
  209. }
  210. private void _on_Position_pressed()
  211. {
  212. ((IDictionary)config["banner"])["position"] = BannerPosition.Pressed;
  213. if ((bool)MobileAds.Call("get_is_banner_loaded"))
  214. {
  215. MobileAds.Call("load_banner");
  216. }
  217. }
  218. private void _on_IsInitialized_pressed()
  219. {
  220. _add_text_Advice_Node("Is initialized: " + MobileAds.Call("get_is_initialized"));
  221. }
  222. private void _on_IsBannerLoaded_pressed()
  223. {
  224. _add_text_Advice_Node("Is Banner loaded: " + MobileAds.Call("get_is_banner_loaded"));
  225. }
  226. private void _on_IsInterstitialLoaded_pressed()
  227. {
  228. _add_text_Advice_Node("Is Interstitial loaded: " + MobileAds.Call("get_is_interstitial_loaded"));
  229. }
  230. private void _on_IsRewardedLoaded_pressed(){
  231. _add_text_Advice_Node("Is Rewarded loaded: " + MobileAds.Call("get_is_rewarded_loaded"));
  232. }
  233. private void _on_IsRewardedInterstitialLoaded_pressed(){
  234. _add_text_Advice_Node("Is RewardedInterstitial loaded: " + MobileAds.Call("get_is_rewarded_interstitial_loaded"));
  235. }
  236. private void _on_ShowBanner_pressed()
  237. {
  238. MobileAds.Call("show_banner");
  239. }
  240. private void _on_HideBanner_pressed()
  241. {
  242. MobileAds.Call("hide_banner");
  243. }
  244. }