Skip to content

Commit d83520d

Browse files
committed
2 parents 068dcc5 + aa27905 commit d83520d

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

README.md

+175
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,178 @@ for version `1.0.0`
2727
```csharp
2828
"com.wolf-package.in-app-purchasing":"https://github.com/wolf-package/in-app-purchasing.git#1.0.0",
2929
```
30+
31+
## Use
32+
33+
### Setup
34+
- Use via MenuItem `Unity-Common` > `IapSettings` or shortcut `Ctrl + W / Command + W` to open `IapSettings`
35+
36+
![Unity_BVkNMfm6fL](https://github.com/wolf-package/in-app-purchasing/assets/102142404/798a790c-c988-48c5-8b32-e88dab94a594)
37+
38+
39+
- Add data product to list (enter id and select product type), then click `Generate Product`
40+
- Select `Validate Product` and enter `Google Play Store Key`, then click `Obfuscator Key`
41+
- Don't forget add Define Symbol `VIRTUESKY_IAP`
42+
43+
- After clicking `Generate Product`, an `IapProduct.cs` script is generated in the following format
44+
45+
```csharp
46+
47+
public struct IapProduct
48+
{
49+
public const string ID_COIN = "com.test.coin";
50+
public static IapDataProduct PurchaseCoin()
51+
{
52+
return IapManager.Instance.PurchaseProduct(IapSettings.Instance.IapDataProducts[0]);
53+
}
54+
55+
public static bool IsPurchasedCoin()
56+
{
57+
return IapManager.Instance.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[0]);
58+
}
59+
60+
public static string LocalizedPriceCoin()
61+
{
62+
return IapManager.Instance.LocalizedPriceProduct(IapSettings.Instance.IapDataProducts[0]);
63+
}
64+
65+
public const string ID_REMOVEADS = "com.test.removeads";
66+
public static IapDataProduct PurchaseRemoveads()
67+
{
68+
return IapManager.Instance.PurchaseProduct(IapSettings.Instance.IapDataProducts[1]);
69+
}
70+
71+
public static bool IsPurchasedRemoveads()
72+
{
73+
return IapManager.Instance.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[1]);
74+
}
75+
76+
public static string LocalizedPriceRemoveads()
77+
{
78+
return IapManager.Instance.LocalizedPriceProduct(IapSettings.Instance.IapDataProducts[1]);
79+
}
80+
81+
}
82+
83+
```
84+
85+
### Handle purchase product
86+
- Example 1:
87+
```csharp
88+
public Button buttonRemoveAds;
89+
public TextMeshProUGUI textLocalizedPriceRemoveAds;
90+
91+
/// <summary>
92+
/// set text localized price for RemoveAds product
93+
/// </summary>
94+
void SetupTextPrice()
95+
{
96+
textLocalizedPriceRemoveAds.text = IapProduct.LocalizedPriceRemoveads();
97+
}
98+
99+
/// <summary>
100+
/// refresh ui button remove ads
101+
/// disable buttonRemoveAds if RemoveAds product has been purchased
102+
/// </summary>
103+
void RefreshUI()
104+
{
105+
buttonRemoveAds.gameObject.SetActive(!IapProduct.IsPurchasedRemoveads());
106+
}
107+
108+
/// <summary>
109+
/// Buy remove ads iap
110+
/// </summary>
111+
public void OnClickRemoveAds()
112+
{
113+
IapProduct.PurchaseRemoveads().OnCompleted(() =>
114+
{
115+
// handle purchase success
116+
RefreshUI();
117+
}).OnFailed(() =>
118+
{
119+
// handle purchase failed
120+
});
121+
}
122+
123+
```
124+
- Example 2: You create a new script similar to the demo below. then attach it to LoadingScene
125+
(From version 1.1.1, `OnPurchaseSuccessEvent` and `OnPurchaseFailedEvent` will be called via `IapManager`)
126+
127+
```csharp
128+
129+
public class HandlePurchaseIap : MonoBehaviour
130+
{
131+
private void Awake()
132+
{
133+
DontDestroyOnLoad(this.gameObject);
134+
IapManager.OnPurchaseSucceedEvent += HandlePurchaseSuccess;
135+
IapManager.OnPurchaseFailedEvent += HandlePurchaseFailed;
136+
}
137+
138+
void HandlePurchaseSuccess(string id)
139+
{
140+
switch (id)
141+
{
142+
case IapProduct.ID_REMOVEADS:
143+
// handle
144+
break;
145+
case IapProduct.ID_1000GEM:
146+
// handle
147+
break;
148+
}
149+
}
150+
151+
void HandlePurchaseFailed(string id)
152+
{
153+
switch (id)
154+
{
155+
case IapProduct.ID_REMOVEADS:
156+
// handle
157+
break;
158+
case IapProduct.ID_1000GEM:
159+
// handle
160+
break;
161+
}
162+
}
163+
}
164+
165+
```
166+
167+
- Note: Example 1 is typically used to handle the user interface after a successful or failed purchase and cannot handle restore purchase. Therefore, you should use example 2 to handle logic for tasks such as removing ads, unlocking skins,...
168+
169+
- Check to see if the product has been purchased (only applies to Non-Consumable items)
170+
```csharp
171+
172+
public Button buttonRemoveAds;
173+
174+
private void OnEnable()
175+
{
176+
if (IapProduct.IsPurchasedRemoveads())
177+
{
178+
buttonRemoveAds.gameObject.SetActive(false);
179+
}
180+
}
181+
182+
```
183+
### Restore purchase
184+
Restore purchase only applies to Non-Consumable items
185+
186+
Restore Purchase is a mandatory feature on iOS to be able to be released to the store.
187+
188+
On Android when you successfully purchased RemoveAds. Then you uninstall your game and reinstall it. If you click buy remove ads again, google play system will report that you already own this item and can't buy it again, now the user has removed ads but the game still displays ads (incorrect). We will need to handle restore purchase of the user's purchased items so that the user avoids this situation.
189+
190+
On Android restore purchase will be called automatically when you reinstall the game via method `ConfirmPendingPurchase` call in `OnInitialized`. On ios you will need to create a restore purchase button for the user to click
191+
192+
When the restore is successful, it will automatically call the successful purchase callback of each item for further processing for the user
193+
194+
```csharp
195+
196+
public void OnClickRestorePurchase()
197+
{
198+
#if UNITY_IOS
199+
IapManager.Instance.RestorePurchase();
200+
#endif
201+
}
202+
203+
```
204+

0 commit comments

Comments
 (0)