| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using System;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public sealed class AppSettingsWindow : EditorWindow
- {
- private const string SettingsPath = "Assets/AppRuntimes/Resources/AppSettings.asset";
- private AppSettings settings;
- private SerializedObject facebook;
- private int appIndex;
- private string appName, appId, clientToken;
- private string appAddress, appChannel, privacyUrl;
- private string addressError;
- private Vector2 scrollPosition;
- private bool showTestUrlHelp = false;
- private bool showUsageHelp = false;
- private const string TestUrlExample = @"<key>TestUserID</key>
- <string>https://example.com/test/</string>";
- private const string UsageExample = @"using UnityEngine;
- public class AppStartup : MonoBehaviour
- {
- private async void Start()
- {
- AppRuntimes.Initialization();
- if (await AppRuntimes.CheckVersion())
- {
- // 已打开平台页面,停止游戏背景音乐。
- // 在这里调用项目的 BGM 停止方法。
- return;
- }
- // 未打开平台页面,继续游戏主流程。
- }
- }";
- [MenuItem("AppSettings/Open Settings")]
- public static void Open()
- {
- var window = GetWindow<AppSettingsWindow>("AppSettings");
- window.minSize = new Vector2(560, 420);
- window.LoadSettings();
- window.Show();
- }
- private void OnEnable() => LoadSettings();
- private void LoadSettings()
- {
- settings = Resources.Load<AppSettings>("AppSettings");
- if (settings == null)
- {
- if (!AssetDatabase.IsValidFolder("Assets/AppRuntimes/Resources"))
- AssetDatabase.CreateFolder("Assets/AppRuntimes", "Resources");
- settings = CreateInstance<AppSettings>();
- AssetDatabase.CreateAsset(settings, SettingsPath);
- AssetDatabase.SaveAssets();
- }
- addressError = null;
- try
- {
- appAddress = string.IsNullOrEmpty(settings.AppAddress)
- ? string.Empty : ClientCrypto.DecryptString(settings.AppAddress);
- }
- catch (Exception)
- {
- appAddress = string.Empty;
- addressError = "AppAddress 解密失败,请重新填写明文地址后保存。";
- }
- appChannel = settings.AppChannel;
- privacyUrl = settings.PrivacyUrl;
- // Read the SDK's active application, preserving other configured apps.
- var facebookAsset = Resources.Load<ScriptableObject>("FacebookSettings");
- facebook = facebookAsset != null ? new SerializedObject(facebookAsset) : null;
- appName = appId = clientToken = string.Empty;
- if (facebook == null) return;
- facebook.Update();
- appIndex = Mathf.Max(0, facebook.FindProperty("selectedAppIndex").intValue);
- appName = ReadFacebookValue("appLabels");
- appId = ReadFacebookValue("appIds");
- clientToken = ReadFacebookValue("clientTokens");
- }
- private string ReadFacebookValue(string name)
- {
- var property = facebook.FindProperty(name);
- return appIndex < property.arraySize
- ? property.GetArrayElementAtIndex(appIndex).stringValue : string.Empty;
- }
- private void WriteFacebookValue(string name, string value)
- {
- var property = facebook.FindProperty(name);
- while (property.arraySize <= appIndex)
- {
- var index = property.arraySize;
- property.arraySize++;
- property.GetArrayElementAtIndex(index).stringValue = string.Empty;
- }
- property.GetArrayElementAtIndex(appIndex).stringValue = value;
- }
- private void OnGUI()
- {
- using (new FontSizeScope(2))
- DrawWindow();
- }
- private void DrawWindow()
- {
- scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
- EditorGUILayout.LabelField("Facebook", EditorStyles.boldLabel);
- if (facebook == null)
- EditorGUILayout.HelpBox("FacebookSettings.asset was not found in Resources.", MessageType.Error);
- using (new EditorGUI.DisabledScope(facebook == null))
- {
- appName = EditorGUILayout.TextField("Facebook AppName", appName);
- appId = EditorGUILayout.TextField("Facebook AppID", appId);
- clientToken = EditorGUILayout.TextField("Client Token", clientToken);
- }
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("App", EditorStyles.boldLabel);
- EditorGUI.BeginChangeCheck();
- appAddress = EditorGUILayout.TextField(new GUIContent("AppAddress", "填写明文地址,保存时自动加密。"), appAddress);
- if (EditorGUI.EndChangeCheck()) addressError = null;
- if (!string.IsNullOrEmpty(addressError))
- EditorGUILayout.HelpBox(addressError, MessageType.Error);
- appChannel = EditorGUILayout.TextField("AppChannel", appChannel);
- privacyUrl = EditorGUILayout.TextField("PrivacyUrl", privacyUrl);
- EditorGUILayout.Space();
- using (new EditorGUILayout.HorizontalScope())
- {
- if (GUILayout.Button("Reload")) LoadSettings();
- if (GUILayout.Button("Save")) SaveSettings();
- }
- DrawUsageHelp();
- EditorGUILayout.EndScrollView();
- }
- // Restore shared editor styles after drawing this window.
- private sealed class FontSizeScope : IDisposable
- {
- private readonly Dictionary<GUIStyle, int> originalSizes = new Dictionary<GUIStyle, int>();
- internal FontSizeScope(int increase)
- {
- var styles = new[]
- {
- EditorStyles.label, EditorStyles.boldLabel, EditorStyles.textField,
- EditorStyles.foldout, EditorStyles.helpBox, EditorStyles.textArea,
- GUI.skin.button
- };
- foreach (var style in styles)
- {
- if (originalSizes.ContainsKey(style)) continue;
- originalSizes.Add(style, style.fontSize);
- var defaultSize = style.font != null && style.font.fontSize > 0
- ? style.font.fontSize : 12;
- style.fontSize = (style.fontSize > 0 ? style.fontSize : defaultSize) + increase;
- }
- }
- public void Dispose()
- {
- foreach (var entry in originalSizes)
- entry.Key.fontSize = entry.Value;
- }
- }
- private void DrawUsageHelp()
- {
- EditorGUILayout.Space(12);
- showTestUrlHelp = EditorGUILayout.Foldout(showTestUrlHelp, "添加测试 URL(iOS)", true);
- if (showTestUrlHelp)
- {
- EditorGUILayout.HelpBox(
- "导出 iOS 工程后,在 Xcode 的主 App Target → Info 中添加 TestUserID,类型选 String," +
- "值填写完整测试 URL(例如 https://example.com/test/)。\n" +
- "也可以直接在 Info.plist 的根 <dict> 内加入以下内容。",
- MessageType.Info);
- DrawExample(TestUrlExample, "复制 Info.plist 示例");
- EditorGUILayout.HelpBox(
- "iOS 启动时读取 TestUserID;非空时直接使用该测试地址。修改后需重新构建并启动 App。\n" +
- "删除该键或清空值可恢复正常流程。Android 和 Unity Editor 的 TestUrl 始终为空。",
- MessageType.None);
- }
- EditorGUILayout.Space(8);
- showUsageHelp = EditorGUILayout.Foldout(showUsageHelp, "AppRuntimes 使用方法", true);
- if (showUsageHelp)
- {
- EditorGUILayout.HelpBox(
- "1. 填写上方配置并点击 Save。AppAddress 填明文,保存时自动加密。\n" +
- "2. 在App的启动脚本时 先调用 Initialization(),再等待 CheckVersion():返回 true 表示已打开平台页面," +
- "应停止游戏背景音乐;返回 false 时继续游戏主流程。",
- MessageType.Info);
- DrawExample(UsageExample, string.Empty);
- }
- }
- private void DrawExample(string example, string copyLabel)
- {
- var style = new GUIStyle(EditorStyles.textArea) { wordWrap = true };
- var height = style.CalcHeight(new GUIContent(example), Mathf.Max(200, position.width - 40));
- EditorGUILayout.SelectableLabel(example, style, GUILayout.Height(height));
- if (!string.IsNullOrEmpty(copyLabel) && GUILayout.Button(copyLabel))
- {
- EditorGUIUtility.systemCopyBuffer = example;
- ShowNotification(new GUIContent("示例已复制"));
- }
- }
- private void SaveSettings()
- {
- // Do not overwrite an unreadable address when saving unrelated settings.
- if (!string.IsNullOrEmpty(addressError)) return;
- string encryptedAddress;
- try
- {
- encryptedAddress = string.IsNullOrEmpty(appAddress)
- ? string.Empty : ClientCrypto.EncryptString(appAddress);
- }
- catch (Exception)
- {
- addressError = "AppAddress 加密失败,配置未保存,请重新填写后重试。";
- return;
- }
- Undo.RecordObject(settings, "Save AppSettings");
- settings.AppAddress = encryptedAddress;
- settings.AppChannel = appChannel;
- settings.PrivacyUrl = privacyUrl;
- EditorUtility.SetDirty(settings);
- if (facebook != null)
- {
- facebook.Update();
- WriteFacebookValue("appLabels", appName);
- WriteFacebookValue("appIds", appId);
- WriteFacebookValue("clientTokens", clientToken);
- facebook.ApplyModifiedProperties();
- EditorUtility.SetDirty(facebook.targetObject);
- }
- AssetDatabase.SaveAssets();
- ShowNotification(new GUIContent("Settings saved"));
- }
- }
|