AppHelpProjectSettings.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. internal static class AppHelpProjectSettings
  6. {
  7. private const string DirectoryPath = "Assets/Resources";
  8. internal static void EnsureDefaultSettings()
  9. {
  10. LoadOrCreateAppSettings();
  11. LoadOrCreateFacebookSettings();
  12. LoadOrCreateUniWebViewSettings();
  13. }
  14. internal static AppSettings LoadOrCreateAppSettings()
  15. {
  16. var settings = Resources.Load<AppSettings>("AppSettings");
  17. if (settings != null) return settings;
  18. return CopyDefault<AppSettings>("AppSettings.asset");
  19. }
  20. internal static ScriptableObject LoadOrCreateFacebookSettings()
  21. {
  22. var settings = Resources.Load<Facebook.Unity.Settings.FacebookSettings>("FacebookSettings");
  23. if (settings != null) return settings;
  24. return CopyDefault<Facebook.Unity.Settings.FacebookSettings>("FacebookSettings.asset");
  25. }
  26. internal static ScriptableObject LoadOrCreateUniWebViewSettings()
  27. {
  28. return UniWebViewEditorSettings.GetOrCreateSettings();
  29. }
  30. private static T CopyDefault<T>(string fileName) where T : ScriptableObject
  31. {
  32. string destination = DirectoryPath + "/" + fileName;
  33. if (File.Exists(destination))
  34. throw new InvalidOperationException("AppRuntimes settings exist but could not be loaded: " + destination);
  35. string template = AppHelpPackagePaths.AssetPath("Editor/DefaultSettings/" + fileName);
  36. if (AssetDatabase.LoadAssetAtPath<T>(template) == null)
  37. throw new InvalidOperationException("AppRuntimes default settings template could not be loaded: " + template);
  38. if (!AssetDatabase.IsValidFolder(DirectoryPath))
  39. AssetDatabase.CreateFolder("Assets", "Resources");
  40. if (!AssetDatabase.CopyAsset(template, destination))
  41. throw new IOException("Could not copy AppRuntimes default settings to " + destination);
  42. return AssetDatabase.LoadAssetAtPath<T>(destination)
  43. ?? throw new InvalidOperationException("Copied settings could not be imported: " + destination);
  44. }
  45. }
  46. internal sealed class AppHelpSettingsInitializer : AssetPostprocessor
  47. {
  48. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
  49. string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
  50. {
  51. if (didDomainReload)
  52. AppHelpProjectSettings.EnsureDefaultSettings();
  53. }
  54. }