index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <!DOCTYPE html>
  2. <html lang="en-us">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Unity WebGL Player | {{{ PRODUCT_NAME }}}</title>
  7. <link rel="shortcut icon" href="TemplateData/favicon.ico">
  8. <link rel="stylesheet" href="TemplateData/style.css">
  9. <script src="https://yandex.ru/games/sdk/v2"></script>
  10. <!-- Yandex.Metrika counter -->
  11. <script type="text/javascript">
  12. (function (m, e, t, r, i, k, a) {
  13. m[i] = m[i] || function () { (m[i].a = m[i].a || []).push(arguments) };
  14. m[i].l = 1 * new Date();
  15. for (var j = 0; j < document.scripts.length; j++) { if (document.scripts[j].src === r) { return; } }
  16. k = e.createElement(t), a = e.getElementsByTagName(t)[0], k.async = 1, k.src = r, a.parentNode.insertBefore(k, a)
  17. })
  18. (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
  19. ym( {{{ parseInt(APP_METRICA) }}} , "init", {
  20. clickmap: true,
  21. trackLinks: true,
  22. accurateTrackBounce: true,
  23. webvisor: true
  24. });
  25. </script>
  26. <noscript><div><img src="https://mc.yandex.ru/watch/{{{parseInt(APP_METRICA)}}}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
  27. <!-- /Yandex.Metrika counter -->
  28. </head>
  29. <body bgcolor="#2DC8FF">
  30. <div align="center">
  31. <div id="unity-container" class="unity-desktop">
  32. <canvas id="unity-canvas" width={{{ WIDTH }}} height={{{ HEIGHT }}}></canvas>
  33. <div id="unity-loading-bar">
  34. <div id="unity-logo"></div>
  35. <div id="unity-progress-bar-empty">
  36. <div id="unity-progress-bar-full"></div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. <div id="bottomAd"></div>
  42. <!--UnityBuild-->
  43. <script>
  44. var buildUrl = "Build";
  45. var loaderUrl = buildUrl + "/{{{ LOADER_FILENAME }}}";
  46. var config = {
  47. dataUrl: buildUrl + "/{{{ DATA_FILENAME }}}",
  48. frameworkUrl: buildUrl + "/{{{ FRAMEWORK_FILENAME }}}",
  49. codeUrl: buildUrl + "/{{{ CODE_FILENAME }}}",
  50. #if MEMORY_FILENAME
  51. memoryUrl: buildUrl + "/{{{ MEMORY_FILENAME }}}",
  52. #endif
  53. #if SYMBOLS_FILENAME
  54. symbolsUrl: buildUrl + "/{{{ SYMBOLS_FILENAME }}}",
  55. #endif
  56. streamingAssetsUrl: "StreamingAssets",
  57. companyName: "{{{ COMPANY_NAME }}}",
  58. productName: "{{{ PRODUCT_NAME }}}",
  59. productVersion: "{{{ PRODUCT_VERSION }}}",
  60. };
  61. var container = document.querySelector("#unity-container");
  62. var canvas = document.querySelector("#unity-canvas");
  63. var loadingBar = document.querySelector("#unity-loading-bar");
  64. var progressBarFull = document.querySelector("#unity-progress-bar-full");
  65. var fullscreenButton = document.querySelector("#unity-fullscreen-button");
  66. var mobileWarning = document.querySelector("#unity-mobile-warning");
  67. // By default Unity keeps WebGL canvas render target size matched with
  68. // the DOM size of the canvas element (scaled by window.devicePixelRatio)
  69. // Set this to false if you want to decouple this synchronization from
  70. // happening inside the engine, and you would instead like to size up
  71. // the canvas DOM size and WebGL render target sizes yourself.
  72. // config.matchWebGLToCanvasSize = false;
  73. Resize();
  74. window.addEventListener(`resize`, event => {
  75. Resize();
  76. }, false);
  77. function Resize() {
  78. var size = document.documentElement.clientHeight + 'px';
  79. console.log(size);
  80. container.style.height = size;
  81. }
  82. if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
  83. container.className = "unity-mobile";
  84. // Avoid draining fillrate performance on mobile devices,
  85. // and default/override low DPI mode on mobile browsers.
  86. //config.devicePixelRatio = 1;
  87. } else {
  88. canvas.style.width = "100%";
  89. canvas.style.height = "100%";
  90. }
  91. #if BACKGROUND_FILENAME
  92. canvas.style.background = "url('" + buildUrl + "/{{{ BACKGROUND_FILENAME.replace(/'/g, '%27') }}}') center / cover";
  93. #endif
  94. loadingBar.style.display = "block";
  95. var script = document.createElement("script");
  96. script.src = loaderUrl;
  97. script.onload = () => {
  98. createUnityInstance(canvas, config, (progress) => {
  99. progressBarFull.style.width = 100 * progress + "%";
  100. }).then((unityInstance) => {
  101. window.unityInstance = unityInstance;
  102. loadingBar.style.display = "none";
  103. }).catch((message) => {
  104. alert(message);
  105. });
  106. };
  107. document.body.appendChild(script);
  108. </script>
  109. <!-- Yandex Games SDK -->
  110. <script type="text/javascript">
  111. var player;
  112. var sdk;
  113. var payments = null;
  114. var playerData = null;
  115. var playerStats = null;
  116. YaGames.init({
  117. adv: {
  118. onAdvClose: wasShown => {
  119. console.info('adv closed!');
  120. }
  121. }
  122. })
  123. .then(ysdk => {
  124. sdk = ysdk;
  125. sdk.adv.showFullscreenAdv({ callbacks: {} });
  126. });
  127. function auth() {
  128. sdk.auth.openAuthDialog().then(() => {
  129. // Игрок успешно авторизован, теперь объект Player будет инициализирован.
  130. initPlayer();
  131. console.log('auth ok');
  132. }).catch(() => {
  133. // Игрок не авторизован.
  134. console.log('auth failed');
  135. });
  136. }
  137. function initPlayer() {
  138. return sdk.getPlayer({ scopes: false }).then(_player => {
  139. player = _player;
  140. return player;
  141. }).catch(err => {
  142. // Если игрок не авторизован, выбрасывает исключение USER_NOT_AUTHORIZED.
  143. });
  144. }
  145. function setScore(key, value) {
  146. playerStats[key] = value;
  147. console.log('Set score' + key + value);
  148. player.setStats(playerStats).then(() => { console.log('setted data!'); }).catch(err => { console.log('not setted' + err) });
  149. }
  150. function setData(key, value) {
  151. playerData[key] = value;
  152. console.log('Set Data' + key + value);
  153. player.setData(playerData, true).then(() => { console.log('setted data!'); }).catch(err => { console.log('not setted' + err) });
  154. }
  155. function initPlayerData() {
  156. initPlayer().then(_player => {
  157. if (_player.getMode() != 'lite') {
  158. var sendData = {};
  159. player.getData().then(datas => {
  160. var i = 0;
  161. playerData = datas;
  162. var adata = Array();
  163. for (var key in datas) {
  164. adata[i] = { key: key, value: datas[key] };
  165. i++;
  166. }
  167. sendData.data = adata;
  168. player.getStats().then(stats => {
  169. i = 0;
  170. playerStats = stats;
  171. var astats = Array();
  172. for (var keya in stats) {
  173. astats[i] = { key: keya, value: stats[keya] };
  174. i++;
  175. }
  176. sendData.score = astats;
  177. console.log('Geted data' + JSON.stringify(sendData));
  178. window.unityInstance.SendMessage('YandexSDK', 'OnGetData', JSON.stringify(sendData));
  179. }).catch(err => {
  180. console.log('No get data score' + err);
  181. window.unityInstance.SendMessage('YandexSDK', 'OnGetData', JSON.stringify(sendData));
  182. });
  183. }).catch(err => {
  184. console.log('NoNoAutorized' + err);
  185. window.unityInstance.SendMessage('YandexSDK', 'NoAutorized');
  186. });
  187. }
  188. else {
  189. console.log('NoNoAutorized');
  190. window.unityInstance.SendMessage('YandexSDK', 'NoAutorized');
  191. }
  192. }).catch(err => {
  193. console.log('NoNoAutorized' + err);
  194. window.unityInstance.SendMessage('YandexSDK', 'NoAutorized');
  195. // Ошибка при инициализации объекта Player.
  196. });
  197. }
  198. function getUserData() {
  199. if (initPlayer) {
  200. var data = { "id": player.getID(), "name": player.getName(), "avatarUrlSmall": player.getPhoto('small'), "avatarUrlMedium": player.getPhoto('medium'), "avatarUrlLarge": player.getPhoto('large') };
  201. window.unityInstance.SendMessage('YandexSDK', 'StoreUserData', JSON.stringify(data));
  202. }
  203. }
  204. function showFullscrenAd() {
  205. sdk.adv.showFullscreenAdv({
  206. callbacks: {
  207. onClose: function (wasShown) {
  208. window.unityInstance.SendMessage('YandexSDK', 'OnInterstitialShown');
  209. },
  210. onError: function (error) {
  211. window.unityInstance.SendMessage('YandexSDK', 'OnInterstitialError', JSON.stringify(error));
  212. }
  213. }
  214. })
  215. }
  216. function showRewardedAd(id) {
  217. sdk.adv.showRewardedVideo({
  218. callbacks: {
  219. onOpen: () => {
  220. window.unityInstance.SendMessage('YandexSDK', 'OnRewardedOpen', id);
  221. console.log('Video ad open. Id: ' + id);
  222. },
  223. onRewarded: () => {
  224. window.unityInstance.SendMessage('YandexSDK', 'OnRewarded', id);
  225. console.log('Rewarded! Id: ' + id);
  226. },
  227. onClose: () => {
  228. window.unityInstance.SendMessage('YandexSDK', 'OnRewardedClose', id);
  229. console.log('Video ad closed. Id: ' + id);
  230. },
  231. onError: (error )=> {
  232. var data = { "id": id, "error": error };
  233. window.unityInstance.SendMessage('YandexSDK', 'OnRewardedError', JSON.stringify(data));
  234. console.log('Error while open video ad:', error);
  235. }
  236. }
  237. })
  238. }
  239. function initPayments() {
  240. sdk.getPayments().then(_payments => {
  241. // Покупки доступны.
  242. payments = _payments;
  243. console.log('Purchases initialized!');
  244. window.unityInstance.SendMessage('YandexSDK', 'OnPurchaseInitialize');
  245. }).catch(err => {
  246. console.log(err);
  247. window.unityInstance.SendMessage('YandexSDK', 'OnPurchaseInitializeFailed', err);
  248. });
  249. }
  250. function buy(id) {
  251. payments.purchase(id).then(purchase => {
  252. // Покупка успешно совершена!
  253. var json = JSON.stringify(purchase);
  254. console.log(json);
  255. window.unityInstance.SendMessage('YandexSDK', 'OnPurchaseSuccess', json);
  256. }).catch(err => {
  257. // Покупка не удалась: в консоли разработчика не добавлен товар с таким id,
  258. // пользователь не авторизовался, передумал и закрыл окно оплаты,
  259. // истекло отведенное на покупку время, не хватило денег и т. д.
  260. console.log(err);
  261. window.unityInstance.SendMessage('YandexSDK', 'OnPurchaseFailed', err);
  262. })
  263. }
  264. window.onunload = function (e) {
  265. console.log("Calling OnClose from Browser!");
  266. window.unityInstance.SendMessage("YandexSDK", "OnClose");
  267. //var dialogText = "You game has been saved! Would you like to continue unloading the page?";
  268. // e.returnValue = dialogText;
  269. // return dialogText;
  270. };
  271. function ShowReview() {
  272. sdk.feedback.canReview()
  273. .then(({ value, reason }) => {
  274. if (value) {
  275. sdk.feedback.requestReview()
  276. .then(({ feedbackSent }) => {
  277. var data = { "CanReview": value, "Reason": "Success", "FeedbackSent": feedbackSent };
  278. SendCallback('OnReview', data);
  279. })
  280. } else {
  281. var data = { "CanReview": value, "Reason": reason, "FeedbackSent": false };
  282. SendCallback('OnReview', data);
  283. }
  284. });
  285. }
  286. function SendCallback(target, data) {
  287. window.unityInstance.SendMessage('YandexSDK', target, JSON.stringify(data));
  288. }
  289. </script>
  290. <script>
  291. function sendEvent(key, value) {
  292. ym({{{ parseInt(APP_METRICA) }}}, key, value);
  293. console.log('метрика отправлена');
  294. }
  295. </script>
  296. </body>
  297. </html>