Skip to content

Commit 6ec2b56

Browse files
committed
- add: type runtime auto init
1 parent c50b4ab commit 6ec2b56

File tree

5 files changed

+67
-41
lines changed

5 files changed

+67
-41
lines changed

Editor/IapSettingsEditor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class IapSettingsEditor : Editor
1111
{
1212
private IapSettings _iapSettings;
1313
private SerializedProperty _runtimeAutoInit;
14+
private SerializedProperty _runtimeAutoInitType;
1415
private SerializedProperty _iapDataProducts;
1516
private SerializedProperty _isValidatePurchase;
1617
private SerializedProperty _googlePlayStoreKey;
@@ -19,6 +20,7 @@ void Init()
1920
{
2021
_iapSettings = target as IapSettings;
2122
_runtimeAutoInit = serializedObject.FindProperty("runtimeAutoInit");
23+
_runtimeAutoInitType = serializedObject.FindProperty("runtimeAutoInitType");
2224
_iapDataProducts = serializedObject.FindProperty("iapDataProducts");
2325
_isValidatePurchase = serializedObject.FindProperty("isValidatePurchase");
2426
_googlePlayStoreKey = serializedObject.FindProperty("googlePlayStoreKey");
@@ -32,6 +34,12 @@ public override void OnInspectorGUI()
3234
GuiLine(2);
3335
GUILayout.Space(10);
3436
EditorGUILayout.PropertyField(_runtimeAutoInit);
37+
if (_runtimeAutoInit.boolValue)
38+
{
39+
EditorGUILayout.PropertyField(_runtimeAutoInitType);
40+
}
41+
42+
GUILayout.Space(10);
3543
EditorGUILayout.PropertyField(_iapDataProducts);
3644
GUILayout.Space(10);
3745
if (GUILayout.Button("Generate Product"))
@@ -128,7 +136,8 @@ void ObfuscatorKeyImpl()
128136
var asmdef = (TextAsset)AssetDatabase.LoadAssetAtPath(pathAsmdef, typeof(TextAsset));
129137
var meta = (TextAsset)AssetDatabase.LoadAssetAtPath(pathAsmdefMeta, typeof(TextAsset));
130138
string path = Path.Combine(TangleFileConsts.k_OutputPath, "UnityCommon.Purchasing.Generate.asmdef");
131-
string pathMeta = Path.Combine(TangleFileConsts.k_OutputPath, "UnityCommon.Purchasing.Generate.asmdef.meta");
139+
string pathMeta =
140+
Path.Combine(TangleFileConsts.k_OutputPath, "UnityCommon.Purchasing.Generate.asmdef.meta");
132141
if (!File.Exists(path))
133142
{
134143
var writer = new StreamWriter(path, false);

Runtime/IapManager.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,6 @@ ProductType ConvertProductType(IapProductType iapProductType)
237237
return ProductType.Consumable;
238238
}
239239

240-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
241-
private static void AutoInitialize()
242-
{
243-
if (IapSettings.Instance == null) return;
244-
if (IapSettings.Instance.RuntimeAutoInit)
245-
{
246-
var iapManager = new GameObject("IapManager");
247-
iapManager.AddComponent<IapManager>();
248-
DontDestroyOnLoad(iapManager);
249-
}
250-
}
251-
252240
#region Internal API
253241

254242
private IapDataProduct InternalPurchaseProduct(string id)

Runtime/IapSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using UnityEngine;
4+
using VirtueSky.Core;
45

56
namespace VirtueSky.Iap
67
{
@@ -22,13 +23,15 @@ public static IapSettings Instance
2223
}
2324

2425
[SerializeField] private bool runtimeAutoInit = true;
26+
[SerializeField] private CoreEnum.RuntimeAutoInitType runtimeAutoInitType;
2527
[SerializeField] private List<IapDataProduct> iapDataProducts = new List<IapDataProduct>();
2628
[SerializeField] private bool isValidatePurchase = true;
2729
#if UNITY_EDITOR
2830
[SerializeField, TextArea] private string googlePlayStoreKey;
2931
public string GooglePlayStoreKey => googlePlayStoreKey;
3032
#endif
3133
public bool RuntimeAutoInit => runtimeAutoInit;
34+
public CoreEnum.RuntimeAutoInitType RuntimeAutoInitType => runtimeAutoInitType;
3235
public List<IapDataProduct> IapDataProducts => iapDataProducts;
3336
public bool IsValidatePurchase => isValidatePurchase;
3437

Runtime/IapStatic.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
using System;
2+
using UnityEngine;
3+
using VirtueSky.Core;
24

35

46
namespace VirtueSky.Iap
57
{
68
public static class IapStatic
79
{
10+
#if VIRTUESKY_IAP
11+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
12+
private static void RuntimeBeforeSceneLoad()
13+
{
14+
AutoInitialize(CoreEnum.RuntimeAutoInitType.BeforeSceneLoad);
15+
}
16+
17+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
18+
private static void RuntimeAfterSceneLoad()
19+
{
20+
AutoInitialize(CoreEnum.RuntimeAutoInitType.AfterSceneLoad);
21+
}
22+
23+
private static void AutoInitialize(CoreEnum.RuntimeAutoInitType iapRuntimeAutoInitType)
24+
{
25+
if (IapSettings.Instance == null) return;
26+
if (!IapSettings.Instance.RuntimeAutoInit) return;
27+
if (IapSettings.Instance.RuntimeAutoInitType != iapRuntimeAutoInitType) return;
28+
var iapManager = new GameObject("IapManager");
29+
iapManager.AddComponent<IapManager>();
30+
UnityEngine.Object.DontDestroyOnLoad(iapManager);
31+
}
32+
#endif
833
public static IapDataProduct OnCompleted(this IapDataProduct product, Action onComplete)
934
{
1035
product.purchaseSuccessCallback = onComplete;

Runtime/UnityCommon.IAP.asmdef

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
{
2-
"name": "UnityCommon.IAP",
3-
"rootNamespace": "",
4-
"references": [
5-
"GUID:60bfecf5cb232594891bc622f40d6bed",
6-
"GUID:94e1de2458b07d0bf1e9f13e6ae06443",
7-
"GUID:e63a64384cc3ef04cac761c1ce76e9c2",
8-
"GUID:d0bf1e9f644394e1de13e6ae02458b07",
9-
"GUID:08d1c582746949b40ba6a45cdb776bdf",
10-
"GUID:fe25561d224ed4743af4c60938a59d0b",
11-
"GUID:70ea675efa2644cef98c7ece24158333",
12-
"GUID:5a4d3c3ff86da0f459ebef74b42549cd",
13-
"GUID:ae6fee6164ec0134e9a5b8094e226755"
14-
],
15-
"includePlatforms": [],
16-
"excludePlatforms": [],
17-
"allowUnsafeCode": false,
18-
"overrideReferences": false,
19-
"precompiledReferences": [],
20-
"autoReferenced": true,
21-
"defineConstraints": [],
22-
"versionDefines": [
23-
{
24-
"name": "com.unity.purchasing",
25-
"expression": "4.11.0",
26-
"define": "VIRTUESKY_IAP"
27-
}
28-
],
29-
"noEngineReferences": false
2+
"name": "UnityCommon.IAP",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:60bfecf5cb232594891bc622f40d6bed",
6+
"GUID:94e1de2458b07d0bf1e9f13e6ae06443",
7+
"GUID:e63a64384cc3ef04cac761c1ce76e9c2",
8+
"GUID:d0bf1e9f644394e1de13e6ae02458b07",
9+
"GUID:08d1c582746949b40ba6a45cdb776bdf",
10+
"GUID:fe25561d224ed4743af4c60938a59d0b",
11+
"GUID:70ea675efa2644cef98c7ece24158333",
12+
"GUID:5a4d3c3ff86da0f459ebef74b42549cd",
13+
"GUID:ae6fee6164ec0134e9a5b8094e226755",
14+
"GUID:7be4b92359ce2d8459bbb5183b835c1a"
15+
],
16+
"includePlatforms": [],
17+
"excludePlatforms": [],
18+
"allowUnsafeCode": false,
19+
"overrideReferences": false,
20+
"precompiledReferences": [],
21+
"autoReferenced": true,
22+
"defineConstraints": [],
23+
"versionDefines": [
24+
{
25+
"name": "com.unity.purchasing",
26+
"expression": "4.11.0",
27+
"define": "VIRTUESKY_IAP"
28+
}
29+
],
30+
"noEngineReferences": false
3031
}

0 commit comments

Comments
 (0)