Создание игры на Android

#12 - Внутриигровые покупки через плагин Soomla

#12 - Внутриигровые покупки через плагин Soomla

Для получения дохода от вашей игры её необходимо монетизировать. Это можно сделать за счёт рекламы или же внутриигровых покупок. В ходе урока вы научитесь добавлять покупки через плагин Soomla.

Видеоурок

Материалы для курса

Чтобы скачивать материалы к видеокурсам необходимо оформить подписку на сайт

Исходный код

Скрипт BuyGame
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Soomla.Store.Example {
	public class BuyGame : MonoBehaviour {
		private float secTime = 1.0f, totTime = 0.0f;
		private bool adsBought, add;
		private static bool soomlaInitialized;
		
		void Start () {
			if (gameObject.name == "Restore Purchase")
				StoreEvents.OnRestoreTransactionsFinished += onRestoreTransactionsFinished;
			
			if (!soomlaInitialized)
				SoomlaStore.Initialize(new BuyGogPlay());
			if (PlayerPrefs.GetString ("NoAds") == "yes")
				gameObject.SetActive (false);
			soomlaInitialized = true;
		}
		
		void Update () {
			if (gameObject.name != "Restore Purchase") {
				if (Time.timeSinceLevelLoad > totTime) {
					CheckIAP_PurchaseStatus ();
					totTime = Time.timeSinceLevelLoad + secTime;
				}
				if (adsBought && !add) {
					PlayerPrefs.SetString ("NoAds", "yes");		
					add = true;
				}
			}
		}
		
		public void onRestoreTransactionsFinished(bool success) {
			// success - true if the restore transactions operation has succeeded
			if (success)
				adsBought = true;
		}
		
		void CheckIAP_PurchaseStatus () {
			if (StoreInventory.GetItemBalance ("no_ads") >= 1)
				adsBought = true;
		}
		
		void OnMouseUpAsButton () {
			try {
				if (gameObject.name != "Restore Purchase")
					StoreInventory.BuyItem ("no_ads");
				else
					SoomlaStore.RestoreTransactions ();
			} 
			catch (Exception e) {
				Debug.Log ("SOOMLA/UNITY" + e.Message);
			}
		}
		
	}
}
Скрипт BuyGogPlay
using UnityEngine;
using System.Collections;

namespace Soomla.Store.Example {
	public class BuyGogPlay : IStoreAssets {
		public int GetVersion() { // Get Current Version
			return 0;
		}
		
		public VirtualCurrency[] GetCurrencies() {
			return new VirtualCurrency[]{};
		}
		
		public VirtualGood[] GetGoods() {
			return new VirtualGood[]{NO_ADS};
		}
		
		public VirtualCurrencyPack[] GetCurrencyPacks() {
			return new VirtualCurrencyPack[]{};
		}
		
		public VirtualCategory[] GetCategories() {
			return new VirtualCategory[]{};
		}
		
		public const string NO_ADS_PRODUCT_ID = "no_ads";
		
		public static VirtualGood NO_ADS = new LifetimeVG(		
        	"No more ads",
     		"No more ads forever",
     		"no_ads",
      	new PurchaseWithMarket(NO_ADS_PRODUCT_ID, 0.99));
	}
}

Также стоит посмотреть

Разработка игры на Unity
9 уроков
3D шутер c мультиплеером в Unity
8 уроков
Дополненная реальность (Vuforia AR и Unity)
3 урока
Создание Андроид игры на движке Unity
Создание игр на Unity | Для начинающих
9 уроков
Создание 3D игры на Unity / Разработка стратегии
10 уроков
Комментарии
Добавить комментарий

Пока комментариев нет