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 = true;
private bool showUsageHelp = true;
private const string TestUrlExample = @"TestUserID
https://example.com/test/";
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("AppSettings");
window.minSize = new Vector2(560, 420);
window.LoadSettings();
window.Show();
}
private void OnEnable() => LoadSettings();
private void LoadSettings()
{
settings = Resources.Load("AppSettings");
if (settings == null)
{
if (!AssetDatabase.IsValidFolder("Assets/AppRuntimes/Resources"))
AssetDatabase.CreateFolder("Assets/AppRuntimes", "Resources");
settings = CreateInstance();
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("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 originalSizes = new Dictionary();
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 的根 内加入以下内容。",
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);
EditorGUILayout.HelpBox(
"AppRuntimes.Log 在编辑器中始终输出。iOS 在 Info.plist 中添加 Boolean 类型的 EnableUnityLog," +
"设为 YES 时输出日志;未设置时默认为 NO。其他平台默认关闭。",
MessageType.Info);
}
}
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"));
}
}