123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// FSM: http://yuml.me/edit/331d4664
- /// </summary>
- public class Skin
- {
- public enum SkinState
- {
- NULL,
- Initialising,
- AvailableForApplying,
- Applied,
- AvailableForPurchase,
- Purchasing,
- Downloading
- }
- enum SkinTransition
- {
- NULL,
- AlreadyPurchasedButNotApplied,
- AlreadyPurchasedAndApplied,
- NotPurchasedYet,
- Purchase,
- StartDownloading,
- PurchaseFailed,
- FinishedDownload,
- FailedDownload,
- Apply,
- UnApply
- }
- public IAPProduct product;
- public const string CURRENTLY_APPLIED_SKIN = "CurrentSkin";
- const string PURCHASED_KEY_PREFIX = "Purchased";
- const string NON_CONSUMABLE_PAYLOAD = "nonconsume";
- FSMSystem<SkinTransition, SkinState> _fsm;
- float _skinDownloadProgress;
- string _pendingTransaction;
- public float SkinDownloadProgress { get { return _skinDownloadProgress; } }
- public Skin(IAPProduct product)
- {
- NotificationCenter.AddListener(OnApplySkin, NotificationType.ApplySkin);
- this.product = product;
- CreateFSM();
- //check if it has been purchased, currently applied etc
- if (HasPurchasedSkin())
- {
- if (IsCurrentlyApplied())
- {
- _fsm.PerformTransition(SkinTransition.AlreadyPurchasedAndApplied);
- } else
- {
- _fsm.PerformTransition(SkinTransition.AlreadyPurchasedButNotApplied);
- }
- } else
- {
- _fsm.PerformTransition(SkinTransition.NotPurchasedYet);
- }
- //#if UNITY_IPHONE
- //{
- // //StoreKitManager.productPurchaseAwaitingConfirmationEvent += OnProductPurchaseAwaitingConfirmationEvent;
- //}
- //#endif
- }
- bool HasPurchasedSkin()
- {
- if (product.productId == "default")
- {
- return true;
- } else
- {
- return PlayerPrefs.GetInt(PURCHASED_KEY_PREFIX+product.productId, 0) == 1;
- }
- }
- bool IsCurrentlyApplied()
- {
- return PlayerPrefs.GetString(CURRENTLY_APPLIED_SKIN, "") == product.productId;
- }
- #region FSM
- void CreateFSM()
- {
- _fsm = new FSMSystem<SkinTransition, SkinState>("Skin");
- var InitialisingState = new FSMState<SkinTransition, SkinState>(SkinState.Initialising);
- InitialisingState.AddTransition(SkinTransition.AlreadyPurchasedAndApplied, SkinState.Applied);
- InitialisingState.AddTransition(SkinTransition.AlreadyPurchasedButNotApplied, SkinState.AvailableForApplying);
- InitialisingState.AddTransition(SkinTransition.NotPurchasedYet, SkinState.AvailableForPurchase);
- _fsm.AddState(InitialisingState);
- var AvailableForApplyingState = new FSMState<SkinTransition, SkinState>(SkinState.AvailableForApplying);
- AvailableForApplyingState.AddTransition(SkinTransition.Apply, SkinState.Applied);
- _fsm.AddState(AvailableForApplyingState);
- var AppliedState = new FSMState<SkinTransition, SkinState>(SkinState.Applied);
- AppliedState.AddTransition(SkinTransition.UnApply, SkinState.AvailableForApplying);
- AppliedState.DoBeforeEntering += OnEnteringAppliedState;
- _fsm.AddState(AppliedState);
- var AvailableForPurchaseState = new FSMState<SkinTransition, SkinState>(SkinState.AvailableForPurchase);
- AvailableForPurchaseState.AddTransition(SkinTransition.Purchase, SkinState.Purchasing);
- AvailableForPurchaseState.AddTransition(SkinTransition.StartDownloading, SkinState.Downloading);
- _fsm.AddState(AvailableForPurchaseState);
- var PurchasingState = new FSMState<SkinTransition, SkinState>(SkinState.Purchasing);
- PurchasingState.AddTransition(SkinTransition.StartDownloading, SkinState.Downloading);
- PurchasingState.AddTransition(SkinTransition.PurchaseFailed, SkinState.AvailableForPurchase);
- PurchasingState.DoBeforeEntering += OnEnteringPurchasingState;
- PurchasingState.DoBeforeLeaving += OnLeavingPurchasingState;
- _fsm.AddState(PurchasingState);
- var DownloadingState = new FSMState<SkinTransition, SkinState>(SkinState.Downloading);
- DownloadingState.AddTransition(SkinTransition.FinishedDownload, SkinState.AvailableForApplying);
- DownloadingState.AddTransition(SkinTransition.FailedDownload, SkinState.AvailableForPurchase);
- DownloadingState.DoBeforeEntering += OnEnteringDownloadingState;
- DownloadingState.DoBeforeLeaving += OnLeavingDownloadingState;
- _fsm.AddState(DownloadingState);
- }
- void OnEnteringPurchasingState()
- {
- #if UNITY_EDITOR || UNITY_WEBPLAYER
- {
- _fsm.PerformTransition(SkinTransition.StartDownloading);
- }
- //Did not use IAP Combo from Prime31 as it wasn't firing properly when the purchase was cancelled.
- #elif UNITY_IPHONE
- {
- //StoreKitManager.purchaseFailedEvent += OnPurchaseFailed;
- //StoreKitManager.purchaseCancelledEvent += OnPurchaseCancelled;
- //StoreKitBinding.purchaseProduct(product.productId, 1);
- }
- #elif UNITY_ANDROID
- {
- GoogleIABManager.purchaseSucceededEvent += OnGooglePurchaseSucceeded;
- GoogleIABManager.purchaseFailedEvent += OnPurchaseFailed;
- GoogleIAB.purchaseProduct( product.productId, NON_CONSUMABLE_PAYLOAD );
- }
- #endif
- }
- void OnLeavingPurchasingState()
- {
- //#if UNITY_IPHONE
- //{
- // StoreKitManager.purchaseFailedEvent -= OnPurchaseFailed;
- // StoreKitManager.purchaseCancelledEvent -= OnPurchaseCancelled;
- //}
- //#elif UNITY_ANDROID
- //{
- // GoogleIABManager.purchaseSucceededEvent -= OnGooglePurchaseSucceeded;
- // GoogleIABManager.purchaseFailedEvent -= OnPurchaseFailed;
- //}
- //#endif
- }
- void OnEnteringDownloadingState()
- {
- NotificationCenter.AddListener(OnSkinDownloadProgress, NotificationType.SkinDownloadProgress);
- NotificationCenter.AddListener(OnSkinDownloadCompleted, NotificationType.SkinDownloadCompleted);
- NotificationCenter.AddListener(OnSkinDownloadFailed, NotificationType.SkinDownloadFailed);
- SkinDownloadManager.Instance.DownloadSkin(product.productId);
- }
- void OnLeavingDownloadingState()
- {
- NotificationCenter.RemoveListener(OnSkinDownloadProgress, NotificationType.SkinDownloadProgress);
- NotificationCenter.RemoveListener(OnSkinDownloadCompleted, NotificationType.SkinDownloadCompleted);
- NotificationCenter.RemoveListener(OnSkinDownloadFailed, NotificationType.SkinDownloadFailed);
- }
- void OnEnteringAppliedState()
- {
- if (PlayerPrefs.GetString(CURRENTLY_APPLIED_SKIN, "") == product.productId)
- {
- return;
- }
- PlayerPrefs.SetString(CURRENTLY_APPLIED_SKIN, product.productId);
- NotificationCenter.Post(NotificationType.ApplySkin, product.productId);
- SkinDownloadManager.Instance.ApplyCachedSkin(product.productId);
- }
- #endregion
- #region Download notifications
- void OnSkinDownloadProgress(Notification note)
- {
- object[] parameters = (object[])note.data;
- string skinId = (string)parameters[0];
- if (skinId == product.productId)
- {
- _skinDownloadProgress = (float)parameters[1];
- }
- }
- void OnSkinDownloadCompleted(Notification note)
- {
- string skinId = (string)note.data;
- if (skinId == product.productId)
- {
- AVDebug.Log("Download Complete "+product.productId);
- PlayerPrefs.SetInt(PURCHASED_KEY_PREFIX+product.productId, 1);
- //#if UNITY_IPHONE
- // StoreKitBinding.finishPendingTransaction(_pendingTransaction);
- //#endif
- _fsm.PerformTransition(SkinTransition.FinishedDownload);
- }
- }
- void OnSkinDownloadFailed(Notification note)
- {
- string skinId = (string)note.data;
- if (skinId == product.productId)
- {
- AVDebug.Log("Download Failed "+product.productId);
- _fsm.PerformTransition(SkinTransition.FailedDownload);
- }
- }
- #endregion
- #region Public methods
- public SkinState State { get { return _fsm.CurrentStateID; } }
- public void Buy()
- {
- _fsm.PerformTransition(SkinTransition.Purchase);
- }
- public void Apply()
- {
- _fsm.PerformTransition(SkinTransition.Apply);
- }
- public void UnApply()
- {
- _fsm.PerformTransition(SkinTransition.UnApply);
- }
- #if UNITY_ANDROID
- public void GoogleSaidItsAlreadyPurchased()
- {
- if (!HasPurchasedSkin())
- {
- _fsm.PerformTransition(SkinTransition.StartDownloading);
- }
- }
- void OnGooglePurchaseSucceeded(GooglePurchase purchase)
- {
- if (purchase.productId == product.productId)
- {
- AVDebug.Log("Google purchase successful "+product.productId);
- _fsm.PerformTransition(SkinTransition.StartDownloading);
- }
- }
- #endif
- #endregion
- #region Store Kit Callbacks
- //#if UNITY_IPHONE
- // public void OnProductPurchaseAwaitingConfirmationEvent(StoreKitTransaction transaction)
- // {
- // if (transaction.productIdentifier == product.productId)
- // {
- // AVDebug.Log("ProductPurchaseAwaitingConfirmationEvent "+transaction.productIdentifier);
- // _pendingTransaction = transaction.transactionIdentifier;
- // _fsm.PerformTransition(SkinTransition.StartDownloading);
- // }
- // }
- //#endif
- void OnPurchaseFailed(string reason)
- {
- AVDebug.Log("Purchase Failed. Reason: "+reason);
- _fsm.PerformTransition(SkinTransition.PurchaseFailed);
- }
- void OnPurchaseCancelled(string reason)
- {
- AVDebug.Log("Purchase Cancelled. Reason "+reason);
- _fsm.PerformTransition(SkinTransition.PurchaseFailed);
- }
- #endregion
- void OnApplySkin(Notification note)
- {
- if (State == SkinState.Applied)
- {
- string skinId = (string)note.data;
- if (skinId != product.productId)
- {
- UnApply();
- }
- }
- }
- }
|