Skip to content

Commit

Permalink
Merge pull request #22
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
DucNV2000 authored Sep 18, 2024
2 parents 389515c + f063d0e commit 6f26a85
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 74 deletions.
9 changes: 0 additions & 9 deletions VirtueSky/ControlPanel/CPIapDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ public static void OnDrawIap(Rect position)
#endif
}

if (GUILayout.Button("Create Iap Get Product Event"))
{
#if VIRTUESKY_IAP
IapWindowEditor.CreateIapGetProductEvent();

#else
Debug.LogError("Add scripting define symbols ( VIRTUESKY_IAP ) to use IAP");
#endif
}

GUILayout.Space(10);
// Handles.DrawAAPolyLine(3, new Vector3(210, GUILayoutUtility.GetLastRect().y + 10),
Expand Down
6 changes: 0 additions & 6 deletions VirtueSky/Iap/Editor/IapWindowEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ public static void CreateIsPurchaseProductEvent()
"iap_is_purchase_product_event");
}


public static void CreateIapGetProductEvent()
{
CreateAsset.CreateScriptableAssets<EventGetProduct>("/Iap",
"iap_get_product_event");
}
#endif
}
}
Expand Down
15 changes: 0 additions & 15 deletions VirtueSky/Iap/Runtime/Event/EventGetProduct.cs

This file was deleted.

11 changes: 0 additions & 11 deletions VirtueSky/Iap/Runtime/Event/EventGetProduct.cs.meta

This file was deleted.

4 changes: 3 additions & 1 deletion VirtueSky/Iap/Runtime/IapDataVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class IapDataVariable : ScriptableObject
public float price;
[Space] [SerializeField] private IapPurchaseSuccess onPurchaseSuccess;
[SerializeField] IapPurchaseFailed onPurchaseFailed;

[ReadOnly] public Product product;
internal IapPurchaseSuccess OnPurchaseSuccess => onPurchaseSuccess;
internal IapPurchaseFailed OnPurchaseFailed => onPurchaseFailed;

[NonSerialized] public Action purchaseSuccessCallback;
[NonSerialized] public Action purchaseFailedCallback;
[NonSerialized] public Action<string> purchaseFailedCallback;
}
}
#endif
38 changes: 14 additions & 24 deletions VirtueSky/Iap/Runtime/IapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public class IapManager : BaseMono, IDetailedStoreListener
[Tooltip("Allows nulls"), SerializeField]
private EventIsPurchaseProduct eventIsPurchaseProduct;

[Tooltip("Allows nulls"), SerializeField]
private EventGetProduct eventGetProduct;

[Tooltip("Allows nulls"), SerializeField]
private BooleanEvent changePreventDisplayAppOpenEvent;
#if UNITY_IOS
Expand Down Expand Up @@ -55,10 +52,6 @@ public override void OnEnable()
eventIsPurchaseProduct.AddListener(IsPurchasedProduct);
}

if (eventGetProduct != null)
{
eventGetProduct.AddListener(GetProduct);
}

#if UNITY_IOS
restoreEvent.AddListener(RestorePurchase);
Expand All @@ -74,10 +67,6 @@ public override void OnDisable()
eventIsPurchaseProduct.RemoveListener(IsPurchasedProduct);
}

if (eventGetProduct != null)
{
eventGetProduct.RemoveListener(GetProduct);
}

#if UNITY_IOS
restoreEvent.RemoveListener(RestorePurchase);
Expand Down Expand Up @@ -120,12 +109,6 @@ private string GetLocalizedPriceProduct(IapDataVariable product)
return _controller.products.WithID(product.id).metadata.localizedPriceString;
}

private Product GetProduct(IapDataVariable iapDataVariable)
{
if (_controller == null) return null;
return _controller.products.WithID(iapDataVariable.id);
}


public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
Expand Down Expand Up @@ -172,6 +155,15 @@ public void OnInitialized(IStoreController controller, IExtensionProvider extens
if (product != null && !string.IsNullOrEmpty(product.transactionID)) _controller.ConfirmPendingPurchase(product);
}
#endif
InitProductIapDataVariable();
}

private void InitProductIapDataVariable()
{
foreach (var iapDataVariable in iapSetting.Products)
{
iapDataVariable.product = _controller.products.WithID(iapDataVariable.id);
}
}

private IapDataVariable PurchaseProductInternal(IapDataVariable product)
Expand Down Expand Up @@ -239,22 +231,22 @@ public void OnInitializeFailed(InitializationFailureReason error, string message

public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
InternalPurchaseFailed(product.definition.id);
InternalPurchaseFailed(product.definition.id, failureReason.ToString());
}

public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
{
InternalPurchaseFailed(product.definition.id);
InternalPurchaseFailed(product.definition.id, failureDescription.reason.ToString());
}

private void InternalPurchaseFailed(string id)
private void InternalPurchaseFailed(string id, string reason)
{
if (changePreventDisplayAppOpenEvent != null) changePreventDisplayAppOpenEvent.Raise(false);
foreach (var product in iapSetting.Products)
{
if (product.id != id) continue;
product.OnPurchaseFailed.Raise();
Common.CallActionAndClean(ref product.purchaseFailedCallback);
product.OnPurchaseFailed.Raise(reason);
Common.CallActionAndClean(ref product.purchaseFailedCallback, reason);
}
}

Expand Down Expand Up @@ -304,8 +296,6 @@ private void Reset()
eventIsPurchaseProduct =
CreateAsset.CreateAndGetScriptableAsset<VirtueSky.Iap.EventIsPurchaseProduct>("/Iap",
"iap_is_purchase_product_event");
eventGetProduct = CreateAsset.CreateAndGetScriptableAsset<VirtueSky.Iap.EventGetProduct>("/Iap",
"iap_get_product_event");
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion VirtueSky/Iap/Runtime/IapPurchaseFailed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace VirtueSky.Iap
{
public abstract class IapPurchaseFailed : ScriptableObject
{
public abstract void Raise();
public abstract void Raise(string reason);
}
}
#endif
8 changes: 1 addition & 7 deletions VirtueSky/Iap/Runtime/IapStatic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IapDataVariable OnPurchaseCompleted(this IapDataVariable product,
return product;
}

public static IapDataVariable OnPurchaseFailed(this IapDataVariable product, Action onFailed)
public static IapDataVariable OnPurchaseFailed(this IapDataVariable product, Action<string> onFailed)
{
product.purchaseFailedCallback = onFailed;
return product;
Expand All @@ -32,12 +32,6 @@ public static bool IsPurchased(this IapDataVariable product, EventIsPurchaseProd
{
return @event.Raise(product);
}

public static UnityEngine.Purchasing.Product GetProduct(this IapDataVariable product,
EventGetProduct @event)
{
return @event.Raise(product);
}
}
}
#endif
8 changes: 8 additions & 0 deletions VirtueSky/Misc/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public static void CallActionAndClean(ref Action action)
action = null;
}

public static void CallActionAndClean<T>(ref Action<T> action, T _value)
{
if (action == null) return;
var a = action;
a(_value);
action = null;
}

public static void Clear<T>(this T[] collection)
{
if (collection == null) throw new ArgumentNullException(nameof(collection));
Expand Down

0 comments on commit 6f26a85

Please sign in to comment.