Placeholder.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace UnityEngine.Monetization
  5. {
  6. [AddComponentMenu("")]
  7. sealed class Placeholder : MonoBehaviour
  8. {
  9. Texture2D m_LandscapeTexture;
  10. Texture2D m_PortraitTexture;
  11. Texture2D m_LandscapeAdTexture;
  12. Texture2D m_PortraitAdTexture;
  13. bool m_Showing;
  14. bool purchaseButtonIsClicked = false;
  15. string m_PlacementId;
  16. bool m_AllowSkip;
  17. internal event ShowAdStartCallback onStart;
  18. internal event ShowAdFinishCallback onFinish;
  19. static Texture2D TextureFromEmbeddedResource(string resourceName)
  20. {
  21. try
  22. {
  23. Assembly assembly = Assembly.GetExecutingAssembly();
  24. Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
  25. byte[] bytes = new byte[resourceStream.Length];
  26. resourceStream.Read(bytes, 0, (int)resourceStream.Length);
  27. var texture2D = new Texture2D(1, 1);
  28. var loadImage = typeof(Texture2D).GetMethod("LoadImage", new[] { typeof(byte[]) });
  29. if (loadImage != null)
  30. {
  31. loadImage.Invoke(texture2D, new object[] { bytes });
  32. }
  33. else
  34. {
  35. var imageConversion = Type.GetType("UnityEngine.ImageConversion, UnityEngine");
  36. loadImage = imageConversion.GetMethod("LoadImage", new[] { typeof(Texture2D), typeof(byte[]), typeof(bool) });
  37. loadImage.Invoke(texture2D, new object[] { texture2D, bytes, true });
  38. }
  39. return texture2D;
  40. }
  41. catch (Exception)
  42. {
  43. return null;
  44. }
  45. }
  46. public void Awake()
  47. {
  48. m_LandscapeTexture = Resources.Load("LandscapeMon") as Texture2D;
  49. m_PortraitTexture = Resources.Load("PortraitMon") as Texture2D;
  50. m_LandscapeAdTexture = Resources.Load("Landscape") as Texture2D;
  51. m_PortraitAdTexture = Resources.Load("Portrait") as Texture2D;
  52. }
  53. public void Show(string placementId, bool allowSkip)
  54. {
  55. m_PlacementId = placementId;
  56. m_AllowSkip = allowSkip;
  57. m_Showing = true;
  58. onStart?.Invoke();
  59. }
  60. public void OnGUI()
  61. {
  62. if (!m_Showing)
  63. {
  64. return;
  65. }
  66. GUI.ModalWindow(0, new Rect(0, 0, Screen.width, Screen.height), ModalWindowFunction, "");
  67. }
  68. void OnApplicationQuit()
  69. {
  70. m_Showing = false;
  71. }
  72. void ModalWindowFunction(int id)
  73. {
  74. if (m_PlacementId == "ShowAdPlacement")
  75. {
  76. // show normal ads
  77. if (m_LandscapeAdTexture != null && m_PortraitAdTexture != null)
  78. {
  79. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Screen.width > Screen.height ? m_LandscapeAdTexture : m_PortraitAdTexture, ScaleMode.ScaleAndCrop);
  80. }
  81. else
  82. {
  83. GUIStyle myStyle = new GUIStyle(GUI.skin.label);
  84. myStyle.alignment = TextAnchor.MiddleCenter;
  85. myStyle.fontSize = 32;
  86. GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "This screen would be your Ad Unit", myStyle);
  87. }
  88. }
  89. else
  90. {
  91. // show promo ads
  92. if (m_LandscapeTexture != null && m_PortraitTexture != null)
  93. {
  94. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Screen.width > Screen.height ? m_LandscapeTexture : m_PortraitTexture, ScaleMode.ScaleAndCrop);
  95. }
  96. else
  97. {
  98. GUIStyle myStyle = new GUIStyle(GUI.skin.label);
  99. myStyle.alignment = TextAnchor.MiddleCenter;
  100. myStyle.fontSize = 32;
  101. GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "This screen would be your Promo Ad Unit", myStyle);
  102. }
  103. GUIStyle purchaseStyle = new GUIStyle(GUI.skin.button);
  104. purchaseStyle.fontSize = 40;
  105. purchaseStyle.normal.textColor = Color.white;
  106. if (!purchaseButtonIsClicked)
  107. {
  108. if (Screen.width > Screen.height)
  109. {
  110. if (GUI.Button(new Rect(Screen.width * 3 / 4 - 100, Screen.height * 3 / 4 - 100, 200, 100), "Purchase", purchaseStyle))
  111. {
  112. purchaseButtonIsClicked = true;
  113. }
  114. }
  115. else
  116. {
  117. if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height * 3 / 4, 200, 100), "Purchase", purchaseStyle))
  118. {
  119. purchaseButtonIsClicked = true;
  120. }
  121. }
  122. }
  123. else
  124. {
  125. GUIStyle confirmStyle = new GUIStyle(GUI.skin.button);
  126. confirmStyle.fontSize = 40;
  127. confirmStyle.normal.textColor = Color.white;
  128. GUIStyle cancelStyle = new GUIStyle(GUI.skin.button);
  129. cancelStyle.fontSize = 40;
  130. cancelStyle.normal.textColor = Color.white;
  131. if (GUI.Button(new Rect(Screen.width * 3 / 4, Screen.height - 150, 200, 100), "Cancel", confirmStyle))
  132. {
  133. purchaseButtonIsClicked = false;
  134. m_Showing = false;
  135. }
  136. if (GUI.Button(new Rect(Screen.width * 3 / 4 - 200, Screen.height - 150, 200, 100), "Confirm", cancelStyle))
  137. {
  138. purchaseButtonIsClicked = false;
  139. m_Showing = false;
  140. }
  141. }
  142. }
  143. if (m_AllowSkip && GUI.Button(new Rect(20, 20, 150, 50), "Skip"))
  144. {
  145. m_Showing = false;
  146. onFinish?.Invoke(ShowResult.Skipped);
  147. }
  148. if (GUI.Button(new Rect(Screen.width - 170, 20, 150, 50), "Close"))
  149. {
  150. m_Showing = false;
  151. onFinish?.Invoke(ShowResult.Finished);
  152. }
  153. }
  154. }
  155. }