Skip to content

Commit 6c79f14

Browse files
committed
- init
1 parent b36c228 commit 6c79f14

31 files changed

+1136
-0
lines changed

Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/IapSettingsEditor.cs

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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\tpublic 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\tpublic const string ID_{itemName.ToUpper()} = \"{iapDataProducts[i].Id}\";";
77+
78+
str +=
79+
$"\n\t\tpublic static IapDataProduct Purchase{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(itemName)}()";
80+
str += "\n\t\t{";
81+
str +=
82+
$"\n\t\t\treturn IapManager.Instance.PurchaseProduct(IapSettings.Instance.IapDataProducts[{i}]);";
83+
str += "\n\t\t}";
84+
str += "\n";
85+
86+
str +=
87+
$"\n\t\tpublic static bool IsPurchased{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(itemName)}()";
88+
str += "\n\t\t{";
89+
str +=
90+
$"\n\t\t\treturn IapManager.Instance.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[{i}]);";
91+
str += "\n\t\t}";
92+
93+
str += "\n";
94+
95+
str +=
96+
$"\n\t\tpublic static string LocalizedPrice{CultureInfo.CurrentCulture.TextInfo.ToTitleCase(itemName)}()";
97+
str += "\n\t\t{";
98+
str +=
99+
$"\n\t\t\treturn 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+
}

Editor/IapSettingsEditor.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/IapWindowEditor.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace VirtueSky.Iap
8+
{
9+
public class IapWindowEditor : EditorWindow
10+
{
11+
[MenuItem("Unity-Common/IapSettings %W", false)]
12+
public static void MenuOpenIapSettings()
13+
{
14+
string path = "Assets/_Root/Resources";
15+
if (!Directory.Exists(path))
16+
{
17+
Directory.CreateDirectory(path);
18+
}
19+
var settings = CreateAndGetScriptableAsset<IapSettings>(path);
20+
Selection.activeObject = settings;
21+
EditorGUIUtility.PingObject(settings);
22+
EditorUtility.FocusProjectWindow();
23+
}
24+
static T CreateAndGetScriptableAsset<T>(string path = "")
25+
where T : ScriptableObject
26+
{
27+
var so = FindAssetAtFolder<T>(new string[] { "Assets" }).FirstOrDefault();
28+
if (so == null)
29+
{
30+
var settings = ScriptableObject.CreateInstance<T>();
31+
AssetDatabase.CreateAsset(settings, $"{path}/{typeof(T).Name}.asset");
32+
AssetDatabase.SaveAssets();
33+
AssetDatabase.Refresh();
34+
so = FindAssetAtFolder<T>(new string[] { "Assets" }).FirstOrDefault();
35+
}
36+
37+
return so;
38+
}
39+
static T[] FindAssetAtFolder<T>(string[] paths) where T : Object
40+
{
41+
var list = new List<T>();
42+
var guids = AssetDatabase.FindAssets($"t:{typeof(T).Name}", paths);
43+
foreach (var guid in guids)
44+
{
45+
var asset = AssetDatabase.LoadAssetAtPath<T>(AssetDatabase.GUIDToAssetPath(guid));
46+
if (asset)
47+
{
48+
list.Add(asset);
49+
}
50+
}
51+
52+
return list.ToArray();
53+
}
54+
}
55+
}

Editor/IapWindowEditor.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)