NearbyConnectionUI.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // <copyright file="NearbyConnectionUI.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. #if (UNITY_ANDROID || (UNITY_IPHONE && !NO_GPGS))
  17. namespace GooglePlayGames.Editor
  18. {
  19. using UnityEngine;
  20. using UnityEditor;
  21. public class NearbyConnectionUI : EditorWindow
  22. {
  23. private string mNearbyServiceId = string.Empty;
  24. [MenuItem("Window/Google Play Games/Setup/Nearby Connections setup...", false, 3)]
  25. public static void MenuItemNearbySetup()
  26. {
  27. EditorWindow window = EditorWindow.GetWindow(
  28. typeof(NearbyConnectionUI), true, GPGSStrings.NearbyConnections.Title);
  29. window.minSize = new Vector2(400, 200);
  30. }
  31. [MenuItem("Window/Google Play Games/Setup/Nearby Connections setup...", true)]
  32. public static bool EnableNearbyMenuItem() {
  33. #if UNITY_ANDROID
  34. return true;
  35. #else
  36. return false;
  37. #endif
  38. }
  39. public void OnEnable()
  40. {
  41. mNearbyServiceId = GPGSProjectSettings.Instance.Get(GPGSUtil.SERVICEIDKEY);
  42. }
  43. public void OnGUI()
  44. {
  45. GUI.skin.label.wordWrap = true;
  46. GUILayout.BeginVertical();
  47. GUILayout.Space(10);
  48. GUILayout.Label(GPGSStrings.NearbyConnections.Blurb);
  49. GUILayout.Space(10);
  50. GUILayout.Label(GPGSStrings.Setup.NearbyServiceId, EditorStyles.boldLabel);
  51. GUILayout.Space(10);
  52. GUILayout.Label(GPGSStrings.Setup.NearbyServiceBlurb);
  53. mNearbyServiceId = EditorGUILayout.TextField(GPGSStrings.Setup.NearbyServiceId,
  54. mNearbyServiceId,GUILayout.Width(350));
  55. GUILayout.FlexibleSpace();
  56. GUILayout.BeginHorizontal();
  57. GUILayout.FlexibleSpace();
  58. if (GUILayout.Button(GPGSStrings.Setup.SetupButton,
  59. GUILayout.Width(100)))
  60. {
  61. DoSetup();
  62. }
  63. if (GUILayout.Button("Cancel", GUILayout.Width(100)))
  64. {
  65. this.Close();
  66. }
  67. GUILayout.FlexibleSpace();
  68. GUILayout.EndHorizontal();
  69. GUILayout.Space(20);
  70. GUILayout.EndVertical();
  71. }
  72. private void DoSetup()
  73. {
  74. if (PerformSetup(mNearbyServiceId, true))
  75. {
  76. EditorUtility.DisplayDialog(GPGSStrings.Success,
  77. GPGSStrings.NearbyConnections.SetupComplete, GPGSStrings.Ok);
  78. this.Close();
  79. }
  80. }
  81. /// Provide static access to setup for facilitating automated builds.
  82. /// <param name="nearbyServiceId">The nearby connections service Id</param>
  83. /// <param name="androidBuild">true if building android</param>
  84. public static bool PerformSetup(string nearbyServiceId, bool androidBuild)
  85. {
  86. // check for valid app id
  87. if (!GPGSUtil.LooksLikeValidServiceId(nearbyServiceId))
  88. {
  89. if (EditorUtility.DisplayDialog(
  90. "Remove Nearby connection permissions? ",
  91. "The service Id is invalid. It must follow package naming rules. " +
  92. "Do you want to remove the AndroidManifest entries for Nearby connections?",
  93. "Yes",
  94. "No"))
  95. {
  96. GPGSProjectSettings.Instance.Set(GPGSUtil.SERVICEIDKEY, null);
  97. GPGSProjectSettings.Instance.Save();
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. else
  105. {
  106. GPGSProjectSettings.Instance.Set(GPGSUtil.SERVICEIDKEY, nearbyServiceId);
  107. GPGSProjectSettings.Instance.Save();
  108. }
  109. if (androidBuild)
  110. {
  111. // create needed directories
  112. GPGSUtil.EnsureDirExists("Assets/Plugins");
  113. GPGSUtil.EnsureDirExists("Assets/Plugins/Android");
  114. // Generate AndroidManifest.xml
  115. GPGSUtil.GenerateAndroidManifest();
  116. GPGSProjectSettings.Instance.Set(GPGSUtil.NEARBYSETUPDONEKEY, true);
  117. GPGSProjectSettings.Instance.Save();
  118. // Resolve the dependencies
  119. Google.VersionHandler.VerboseLoggingEnabled = true;
  120. Google.VersionHandler.UpdateVersionedAssets(forceUpdate: true);
  121. Google.VersionHandler.Enabled = true;
  122. AssetDatabase.Refresh();
  123. Google.VersionHandler.InvokeStaticMethod(
  124. Google.VersionHandler.FindClass(
  125. "Google.JarResolver",
  126. "GooglePlayServices.PlayServicesResolver"),
  127. "MenuResolve", null);
  128. }
  129. return true;
  130. }
  131. }
  132. }
  133. #endif