1
+ using System . Globalization ;
2
+ using System . IO ;
3
+ using System . Linq ;
4
+ using UnityEditor ;
5
+ using UnityEngine ;
6
+
7
+ namespace VirtueSky . Iap
8
+ {
9
+ [ CustomEditor ( typeof ( IapSettings ) , true ) ]
10
+ public class IapSettingsEditor : Editor
11
+ {
12
+ private IapSettings _iapSettings ;
13
+ private SerializedProperty _runtimeAutoInit ;
14
+ private SerializedProperty _iapDataProducts ;
15
+ private SerializedProperty _isValidatePurchase ;
16
+ private SerializedProperty _googlePlayStoreKey ;
17
+
18
+ void Init ( )
19
+ {
20
+ _iapSettings = target as IapSettings ;
21
+ _runtimeAutoInit = serializedObject . FindProperty ( "runtimeAutoInit" ) ;
22
+ _iapDataProducts = serializedObject . FindProperty ( "iapDataProducts" ) ;
23
+ _isValidatePurchase = serializedObject . FindProperty ( "isValidatePurchase" ) ;
24
+ _googlePlayStoreKey = serializedObject . FindProperty ( "googlePlayStoreKey" ) ;
25
+ }
26
+
27
+ public override void OnInspectorGUI ( )
28
+ {
29
+ serializedObject . Update ( ) ;
30
+ Init ( ) ;
31
+ EditorGUILayout . LabelField ( "IAP SETTING" , EditorStyles . boldLabel ) ;
32
+ GuiLine ( 2 ) ;
33
+ GUILayout . Space ( 10 ) ;
34
+ EditorGUILayout . PropertyField ( _runtimeAutoInit ) ;
35
+ EditorGUILayout . PropertyField ( _iapDataProducts ) ;
36
+ GUILayout . Space ( 10 ) ;
37
+ if ( GUILayout . Button ( "Generate Product" ) )
38
+ {
39
+ GenerateProductImpl ( ) ;
40
+ }
41
+
42
+ GUILayout . Space ( 10 ) ;
43
+ GuiLine ( 2 ) ;
44
+ GUILayout . Space ( 10 ) ;
45
+ EditorGUILayout . PropertyField ( _isValidatePurchase ) ;
46
+ if ( _isValidatePurchase . boolValue )
47
+ {
48
+ EditorGUILayout . PropertyField ( _googlePlayStoreKey ) ;
49
+ GUILayout . Space ( 10 ) ;
50
+ if ( GUILayout . Button ( "Obfuscator Key" ) )
51
+ {
52
+ ObfuscatorKeyImpl ( ) ;
53
+ }
54
+ }
55
+
56
+ serializedObject . ApplyModifiedProperties ( ) ;
57
+ }
58
+
59
+ private const string pathDefaultScript = "Assets/_Root/Scripts" ;
60
+
61
+ void GenerateProductImpl ( )
62
+ {
63
+ if ( ! Directory . Exists ( pathDefaultScript ) )
64
+ {
65
+ Directory . CreateDirectory ( pathDefaultScript ) ;
66
+ }
67
+ var productImplPath = $ "{ pathDefaultScript } /IapProduct.cs";
68
+ var str = "namespace VirtueSky.Iap\n {" ;
69
+ str += "\n \t public struct IapProduct\n \t {" ;
70
+
71
+ var iapDataProducts = _iapSettings . IapDataProducts ;
72
+ for ( int i = 0 ; i < _iapSettings . IapDataProducts . Count ; i ++ )
73
+ {
74
+ var itemName = iapDataProducts [ i ] . Id . Split ( '.' ) . Last ( ) ;
75
+
76
+ str += $ "\n \t \t public const string ID_{ itemName . ToUpper ( ) } = \" { iapDataProducts [ i ] . Id } \" ;";
77
+
78
+ str +=
79
+ $ "\n \t \t public static IapDataProduct Purchase{ CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( itemName ) } ()";
80
+ str += "\n \t \t {" ;
81
+ str +=
82
+ $ "\n \t \t \t return IapManager.Instance.PurchaseProduct(IapSettings.Instance.IapDataProducts[{ i } ]);";
83
+ str += "\n \t \t }" ;
84
+ str += "\n " ;
85
+
86
+ str +=
87
+ $ "\n \t \t public static bool IsPurchased{ CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( itemName ) } ()";
88
+ str += "\n \t \t {" ;
89
+ str +=
90
+ $ "\n \t \t \t return IapManager.Instance.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[{ i } ]);";
91
+ str += "\n \t \t }" ;
92
+
93
+ str += "\n " ;
94
+
95
+ str +=
96
+ $ "\n \t \t public static string LocalizedPrice{ CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( itemName ) } ()";
97
+ str += "\n \t \t {" ;
98
+ str +=
99
+ $ "\n \t \t \t return IapManager.Instance.LocalizedPriceProduct(IapSettings.Instance.IapDataProducts[{ i } ]);";
100
+ str += "\n \t \t }" ;
101
+ str += "\n " ;
102
+ }
103
+
104
+ str += "\n \t }" ;
105
+ str += "\n }" ;
106
+
107
+ var writer = new StreamWriter ( productImplPath , false ) ;
108
+ writer . Write ( str ) ;
109
+ writer . Close ( ) ;
110
+ AssetDatabase . ImportAsset ( productImplPath ) ;
111
+ }
112
+
113
+ void ObfuscatorKeyImpl ( )
114
+ {
115
+ var googleError = "" ;
116
+ var appleError = "" ;
117
+ ObfuscationGenerator . ObfuscateSecrets ( includeGoogle : true ,
118
+ appleError : ref googleError ,
119
+ googleError : ref appleError ,
120
+ googlePlayPublicKey : _iapSettings . GooglePlayStoreKey ) ;
121
+ string pathAsmdef =
122
+ GetPathInCurrentEnvironent (
123
+ $ "Editor/TempAssembly/PurchasingGeneratedAsmdef.txt") ;
124
+ string pathAsmdefMeta =
125
+ GetPathInCurrentEnvironent (
126
+ $ "Editor/TempAssembly/PurchasingGeneratedAsmdefMeta.txt") ;
127
+ var asmdef = ( TextAsset ) AssetDatabase . LoadAssetAtPath ( pathAsmdef , typeof ( TextAsset ) ) ;
128
+ var meta = ( TextAsset ) AssetDatabase . LoadAssetAtPath ( pathAsmdefMeta , typeof ( TextAsset ) ) ;
129
+ string path = Path . Combine ( TangleFileConsts . k_OutputPath , "Wolf.Purchasing.Generate.asmdef" ) ;
130
+ string pathMeta = Path . Combine ( TangleFileConsts . k_OutputPath , "Wolf.Purchasing.Generate.asmdef.meta" ) ;
131
+ if ( ! File . Exists ( path ) )
132
+ {
133
+ var writer = new StreamWriter ( path , false ) ;
134
+ writer . Write ( asmdef . text ) ;
135
+ writer . Close ( ) ;
136
+ }
137
+
138
+ if ( ! File . Exists ( pathMeta ) )
139
+ {
140
+ var writer = new StreamWriter ( pathMeta , false ) ;
141
+ writer . Write ( meta . text ) ;
142
+ writer . Close ( ) ;
143
+ }
144
+
145
+ AssetDatabase . ImportAsset ( path ) ;
146
+ }
147
+
148
+ void GuiLine ( int i_height = 1 )
149
+ {
150
+ Rect rect = EditorGUILayout . GetControlRect ( false , i_height ) ;
151
+
152
+ rect . height = i_height ;
153
+
154
+ EditorGUI . DrawRect ( rect , new Color32 ( 0 , 0 , 0 , 255 ) ) ;
155
+ }
156
+ string GetPathInCurrentEnvironent ( string fullRelativePath )
157
+ {
158
+ var upmPath = $ "Packages/com.wolf-package.in-app-purchasing/{ fullRelativePath } ";
159
+ var normalPath = $ "Assets/in-app-purchasing/{ fullRelativePath } ";
160
+ return ! File . Exists ( Path . GetFullPath ( upmPath ) ) ? normalPath : upmPath ;
161
+ }
162
+ }
163
+ }
0 commit comments