using System; using System.IO; using UnityEditor; using UnityEngine; internal static class AppHelpProjectSettings { private const string DirectoryPath = "Assets/Resources"; internal static void EnsureDefaultSettings() { LoadOrCreateAppSettings(); LoadOrCreateFacebookSettings(); LoadOrCreateUniWebViewSettings(); } internal static AppSettings LoadOrCreateAppSettings() { var settings = Resources.Load("AppSettings"); if (settings != null) return settings; return CopyDefault("AppSettings.asset"); } internal static ScriptableObject LoadOrCreateFacebookSettings() { var settings = Resources.Load("FacebookSettings"); if (settings != null) return settings; return CopyDefault("FacebookSettings.asset"); } internal static ScriptableObject LoadOrCreateUniWebViewSettings() { return UniWebViewEditorSettings.GetOrCreateSettings(); } private static T CopyDefault(string fileName) where T : ScriptableObject { string destination = DirectoryPath + "/" + fileName; if (File.Exists(destination)) throw new InvalidOperationException("AppRuntimes settings exist but could not be loaded: " + destination); string template = AppHelpPackagePaths.AssetPath("Editor/DefaultSettings/" + fileName); if (AssetDatabase.LoadAssetAtPath(template) == null) throw new InvalidOperationException("AppRuntimes default settings template could not be loaded: " + template); if (!AssetDatabase.IsValidFolder(DirectoryPath)) AssetDatabase.CreateFolder("Assets", "Resources"); if (!AssetDatabase.CopyAsset(template, destination)) throw new IOException("Could not copy AppRuntimes default settings to " + destination); return AssetDatabase.LoadAssetAtPath(destination) ?? throw new InvalidOperationException("Copied settings could not be imported: " + destination); } } internal sealed class AppHelpSettingsInitializer : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) { if (didDomainReload) AppHelpProjectSettings.EnsureDefaultSettings(); } }