ChatEditor.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #if !PHOTON_UNITY_NETWORKING
  2. using System;
  3. using Photon.Chat;
  4. using Photon.Chat.Demo;
  5. using Photon.Realtime;
  6. using UnityEditor;
  7. using UnityEngine;
  8. [InitializeOnLoad]
  9. public class ChatEditor : EditorWindow
  10. {
  11. static ChatEditor()
  12. {
  13. EditorApplication.update += OnEditorUpdate;
  14. }
  15. private static void OnEditorUpdate()
  16. {
  17. EditorApplication.update -= OnEditorUpdate;
  18. //ChatSettings settings = ChatSettings.Load();
  19. //if (settings != null && !settings.WizardDone && string.IsNullOrEmpty(settings.AppId))
  20. //{
  21. // OpenWizard();
  22. //}
  23. }
  24. [MenuItem("Window/Photon Chat/Setup")]
  25. public static void OpenWizard()
  26. {
  27. //currentSettings = ChatSettings.Load();
  28. //currentSettings.WizardDone = true;
  29. //EditorUtility.SetDirty(currentSettings);
  30. ChatEditor editor = (ChatEditor)EditorWindow.GetWindow(typeof (ChatEditor), false, "Photon Chat");
  31. editor.minSize = editor.preferredSize;
  32. }
  33. private ChatGui cGui;
  34. internal string mailOrAppId;
  35. internal bool showDashboardLink = false;
  36. internal bool showRegistrationDone = false;
  37. internal bool showRegistrationError = false;
  38. private readonly Vector2 preferredSize = new Vector2(350, 400);
  39. internal static string UrlCloudDashboard = "https://dashboard.photonengine.com/en-US/";
  40. public string WelcomeText = "Thanks for importing Photon Chat.\nThis window should set you up.\n\nYou will need a free Photon Account to setup a Photon Chat application.\nOpen the Photon Dashboard (webpage) to access your account (see button below).\n\nCopy and paste a Chat AppId into the field below and click \"Setup\".";
  41. //public string AlreadyRegisteredInfo = "The email is registered so we can't fetch your AppId (without password).\n\nPlease login online to get your AppId and paste it above.";
  42. //public string RegisteredNewAccountInfo = "We created a (free) account and fetched you an AppId.\nWelcome. Your Photon Chat project is setup.";
  43. //public string FailedToRegisterAccount = "This wizard failed to register an account right now. Please check your mail address or try via the Dashboard.";
  44. //public string AppliedToSettingsInfo = "Your AppId is now applied to this project.";
  45. public string SetupCompleteInfo = "<b>Done!</b>\nYour Chat AppId is now stored in the <b>Scripts</b> object, Chat App Settings.";
  46. public string CloseWindowButton = "Close";
  47. public string OpenCloudDashboardText = "Photon Dashboard Login";
  48. public string OpenCloudDashboardTooltip = "Review Cloud App information and statistics.";
  49. public void OnGUI()
  50. {
  51. if (this.cGui == null)
  52. {
  53. cGui = FindObjectOfType<ChatGui>();
  54. }
  55. GUI.skin.label.wordWrap = true;
  56. GUI.skin.label.richText = true;
  57. if (string.IsNullOrEmpty(mailOrAppId))
  58. {
  59. mailOrAppId = string.Empty;
  60. }
  61. GUILayout.Label("Chat Settings", EditorStyles.boldLabel);
  62. GUILayout.Label(this.WelcomeText);
  63. GUILayout.Space(15);
  64. GUILayout.Label("Chat AppId");
  65. string input = EditorGUILayout.TextField(this.mailOrAppId);
  66. if (GUI.changed)
  67. {
  68. this.mailOrAppId = input.Trim();
  69. }
  70. //bool isMail = false;
  71. bool minimumInput = false;
  72. bool isAppId = false;
  73. if (IsValidEmail(this.mailOrAppId))
  74. {
  75. // this should be a mail address
  76. minimumInput = true;
  77. //isMail = true;
  78. }
  79. else if (IsAppId(this.mailOrAppId))
  80. {
  81. // this should be an appId
  82. minimumInput = true;
  83. isAppId = true;
  84. }
  85. EditorGUI.BeginDisabledGroup(!minimumInput);
  86. GUILayout.BeginHorizontal();
  87. GUILayout.FlexibleSpace();
  88. bool setupBtn = GUILayout.Button("Setup", GUILayout.Width(205));
  89. GUILayout.FlexibleSpace();
  90. GUILayout.EndHorizontal();
  91. if (setupBtn)
  92. {
  93. this.showDashboardLink = false;
  94. this.showRegistrationDone = false;
  95. this.showRegistrationError = false;
  96. //if (isMail)
  97. //{
  98. // EditorUtility.DisplayProgressBar("Fetching Account", "Trying to register a Photon Cloud Account.", 0.5f);
  99. // AccountService service = new AccountService();
  100. // //service.RegisterByEmail(this.mailOrAppId, AccountService.Origin.Pun);
  101. // //EditorUtility.ClearProgressBar();
  102. // //if (service.ReturnCode == 0)
  103. // //{
  104. // // currentSettings.AppId = service.AppId;
  105. // // EditorUtility.SetDirty(currentSettings);
  106. // // this.showRegistrationDone = true;
  107. // // Selection.objects = new UnityEngine.Object[] { currentSettings };
  108. // //}
  109. // //else
  110. // //{
  111. // // if (service.Message.Contains("registered"))
  112. // // {
  113. // // this.showDashboardLink = true;
  114. // // }
  115. // // else
  116. // // {
  117. // // this.showRegistrationError = true;
  118. // // }
  119. // //}
  120. //}
  121. //else
  122. if (isAppId)
  123. {
  124. //currentSettings.AppId = this.mailOrAppId;
  125. //EditorUtility.SetDirty(currentSettings);
  126. if (this.cGui != null)
  127. {
  128. this.cGui.ChatAppSettings.AppIdChat = this.mailOrAppId;
  129. EditorUtility.SetDirty(this.cGui);
  130. }
  131. showRegistrationDone = true;
  132. }
  133. //EditorGUIUtility.PingObject(currentSettings);
  134. }
  135. EditorGUI.EndDisabledGroup();
  136. GUILayout.BeginHorizontal();
  137. GUILayout.FlexibleSpace();
  138. if (GUILayout.Button(new GUIContent(OpenCloudDashboardText, OpenCloudDashboardTooltip), GUILayout.Width(205)))
  139. {
  140. EditorUtility.OpenWithDefaultApp(UrlCloudDashboard);
  141. }
  142. GUILayout.FlexibleSpace();
  143. GUILayout.EndHorizontal();
  144. //if (this.showDashboardLink)
  145. //{
  146. // // button to open dashboard and get the AppId
  147. // GUILayout.Space(15);
  148. // GUILayout.Label(AlreadyRegisteredInfo);
  149. // GUILayout.BeginHorizontal();
  150. // GUILayout.FlexibleSpace();
  151. // if (GUILayout.Button(new GUIContent(OpenCloudDashboardText, OpenCloudDashboardTooltip), GUILayout.Width(205)))
  152. // {
  153. // EditorUtility.OpenWithDefaultApp(UrlCloudDashboard + Uri.EscapeUriString(this.mailOrAppId));
  154. // this.mailOrAppId = string.Empty;
  155. // this.showDashboardLink = false;
  156. // }
  157. // GUILayout.FlexibleSpace();
  158. // GUILayout.EndHorizontal();
  159. //}
  160. //if (this.showRegistrationError)
  161. //{
  162. // GUILayout.Space(15);
  163. // GUILayout.Label(FailedToRegisterAccount);
  164. // GUILayout.BeginHorizontal();
  165. // GUILayout.FlexibleSpace();
  166. // if (GUILayout.Button(new GUIContent(OpenCloudDashboardText, OpenCloudDashboardTooltip), GUILayout.Width(205)))
  167. // {
  168. // EditorUtility.OpenWithDefaultApp(UrlCloudDashboard + Uri.EscapeUriString(this.mailOrAppId));
  169. // this.mailOrAppId = string.Empty;
  170. // this.showDashboardLink = false;
  171. // }
  172. // GUILayout.FlexibleSpace();
  173. // GUILayout.EndHorizontal();
  174. //}
  175. if (this.showRegistrationDone)
  176. {
  177. GUILayout.Space(15);
  178. //GUILayout.Label("Registration done");
  179. ////if (isMail)
  180. ////{
  181. //// GUILayout.Label(RegisteredNewAccountInfo);
  182. ////}
  183. ////else
  184. ////{
  185. // GUILayout.Label(AppliedToSettingsInfo);
  186. ////}
  187. // setup-complete info
  188. GUILayout.Space(15);
  189. GUILayout.Label(SetupCompleteInfo);
  190. // close window (done)
  191. GUILayout.BeginHorizontal();
  192. GUILayout.FlexibleSpace();
  193. if (GUILayout.Button(CloseWindowButton, GUILayout.Width(205)))
  194. {
  195. this.Close();
  196. }
  197. GUILayout.FlexibleSpace();
  198. GUILayout.EndHorizontal();
  199. }
  200. }
  201. public static bool IsAppId(string val)
  202. {
  203. if (string.IsNullOrEmpty(val) || val.Length < 16)
  204. {
  205. return false;
  206. }
  207. try
  208. {
  209. new Guid(val);
  210. }
  211. catch
  212. {
  213. return false;
  214. }
  215. return true;
  216. }
  217. // https://stackoverflow.com/a/1374644/1449056
  218. private static bool IsValidEmail(string email)
  219. {
  220. if (string.IsNullOrEmpty(email) || !email.Contains("@"))
  221. {
  222. return false;
  223. }
  224. try
  225. {
  226. System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email);
  227. return email.Equals(addr.Address);
  228. }
  229. catch
  230. {
  231. return false;
  232. }
  233. }
  234. }
  235. #endif