Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.8 KiB

3 years ago
  1. // -----------------------------------------------------------------------
  2. // <copyright file="PhotonAppSettings.cs" company="Exit Games GmbH">
  3. // </copyright>
  4. // <author>developer@photonengine.com</author>
  5. // ----------------------------------------------------------------------------
  6. #if UNITY_2017_4_OR_NEWER
  7. #define SUPPORTED_UNITY
  8. #endif
  9. #if !PHOTON_UNITY_NETWORKING
  10. namespace Photon.Realtime
  11. {
  12. using System;
  13. using System.IO;
  14. using UnityEditor;
  15. using UnityEngine;
  16. /// <summary>
  17. /// Collection of connection-relevant settings, used internally by PhotonNetwork.ConnectUsingSettings.
  18. /// </summary>
  19. /// <remarks>
  20. /// Includes the AppSettings class from the Realtime APIs plus some other, PUN-relevant, settings.</remarks>
  21. [Serializable]
  22. [HelpURL("https://doc.photonengine.com/en-us/pun/v2/getting-started/initial-setup")]
  23. public class PhotonAppSettings : ScriptableObject
  24. {
  25. [Tooltip("Core Photon Server/Cloud settings.")]
  26. public AppSettings AppSettings;
  27. #if UNITY_EDITOR
  28. [HideInInspector]
  29. public bool DisableAutoOpenWizard;
  30. //public bool ShowSettings;
  31. //public bool DevRegionSetOnce;
  32. #endif
  33. private static PhotonAppSettings instance;
  34. /// <summary>Serialized server settings, written by the Setup Wizard for use in ConnectUsingSettings.</summary>
  35. public static PhotonAppSettings Instance
  36. {
  37. get
  38. {
  39. if (instance == null)
  40. {
  41. LoadOrCreateSettings();
  42. }
  43. return instance;
  44. }
  45. private set { instance = value; }
  46. }
  47. public static void LoadOrCreateSettings()
  48. {
  49. if (instance != null)
  50. {
  51. Debug.LogWarning("Instance is not null. Will not LoadOrCreateSettings().");
  52. return;
  53. }
  54. #if UNITY_EDITOR
  55. // let's check if the AssetDatabase finds the file; aimed to avoid multiple files being created, potentially a futile step
  56. AssetDatabase.Refresh();
  57. #endif
  58. // try to load the resource / asset (ServerSettings a.k.a. PhotonServerSettings)
  59. instance = (PhotonAppSettings)Resources.Load(typeof(PhotonAppSettings).Name, typeof(PhotonAppSettings));
  60. if (instance != null)
  61. {
  62. //Debug.LogWarning("Settings from Resources."); // DEBUG
  63. return;
  64. }
  65. // create it if not loaded
  66. if (instance == null)
  67. {
  68. instance = (PhotonAppSettings)CreateInstance(typeof(PhotonAppSettings));
  69. if (instance == null)
  70. {
  71. Debug.LogError("Failed to create ServerSettings. PUN is unable to run this way. If you deleted it from the project, reload the Editor.");
  72. return;
  73. }
  74. //Debug.LogWarning("Settings created!"); // DEBUG
  75. }
  76. // in the editor, store the settings file as it's not loaded
  77. #if UNITY_EDITOR
  78. string punResourcesDirectory = "Assets/Photon/Resources/";
  79. string serverSettingsAssetPath = punResourcesDirectory + typeof(PhotonAppSettings).Name + ".asset";
  80. string serverSettingsDirectory = Path.GetDirectoryName(serverSettingsAssetPath);
  81. if (!Directory.Exists(serverSettingsDirectory))
  82. {
  83. Directory.CreateDirectory(serverSettingsDirectory);
  84. AssetDatabase.ImportAsset(serverSettingsDirectory);
  85. }
  86. AssetDatabase.CreateAsset(instance, serverSettingsAssetPath);
  87. AssetDatabase.SaveAssets();
  88. //Debug.Log("Settings stored to DB."); // DEBUG
  89. #endif
  90. }
  91. }
  92. }
  93. #endif