NearbyConnectionUI.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  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. {
  34. #if UNITY_ANDROID
  35. return true;
  36. #else
  37. return false;
  38. #endif
  39. }
  40. public void OnEnable()
  41. {
  42. mNearbyServiceId = GPGSProjectSettings.Instance.Get(GPGSUtil.SERVICEIDKEY);
  43. }
  44. public void OnGUI()
  45. {
  46. GUI.skin.label.wordWrap = true;
  47. GUILayout.BeginVertical();
  48. GUILayout.Space(10);
  49. GUILayout.Label(GPGSStrings.NearbyConnections.Blurb);
  50. GUILayout.Space(10);
  51. GUILayout.Label(GPGSStrings.Setup.NearbyServiceId, EditorStyles.boldLabel);
  52. GUILayout.Space(10);
  53. GUILayout.Label(GPGSStrings.Setup.NearbyServiceBlurb);
  54. mNearbyServiceId = EditorGUILayout.TextField(GPGSStrings.Setup.NearbyServiceId,
  55. mNearbyServiceId, GUILayout.Width(350));
  56. GUILayout.FlexibleSpace();
  57. GUILayout.BeginHorizontal();
  58. GUILayout.FlexibleSpace();
  59. if (GUILayout.Button(GPGSStrings.Setup.SetupButton,
  60. GUILayout.Width(100)))
  61. {
  62. DoSetup();
  63. }
  64. if (GUILayout.Button("Cancel", GUILayout.Width(100)))
  65. {
  66. this.Close();
  67. }
  68. GUILayout.FlexibleSpace();
  69. GUILayout.EndHorizontal();
  70. GUILayout.Space(20);
  71. GUILayout.EndVertical();
  72. }
  73. private void DoSetup()
  74. {
  75. if (PerformSetup(mNearbyServiceId, true))
  76. {
  77. EditorUtility.DisplayDialog(GPGSStrings.Success,
  78. GPGSStrings.NearbyConnections.SetupComplete, GPGSStrings.Ok);
  79. this.Close();
  80. }
  81. }
  82. /// Provide static access to setup for facilitating automated builds.
  83. /// <param name="nearbyServiceId">The nearby connections service Id</param>
  84. /// <param name="androidBuild">true if building android</param>
  85. public static bool PerformSetup(string nearbyServiceId, bool androidBuild)
  86. {
  87. // check for valid app id
  88. if (!GPGSUtil.LooksLikeValidServiceId(nearbyServiceId))
  89. {
  90. if (EditorUtility.DisplayDialog(
  91. "Remove Nearby connection permissions? ",
  92. "The service Id is invalid. It must follow package naming rules. " +
  93. "Do you want to remove the AndroidManifest entries for Nearby connections?",
  94. "Yes",
  95. "No"))
  96. {
  97. GPGSProjectSettings.Instance.Set(GPGSUtil.SERVICEIDKEY, null);
  98. GPGSProjectSettings.Instance.Save();
  99. }
  100. else
  101. {
  102. return false;
  103. }
  104. }
  105. else
  106. {
  107. GPGSProjectSettings.Instance.Set(GPGSUtil.SERVICEIDKEY, nearbyServiceId);
  108. GPGSProjectSettings.Instance.Save();
  109. }
  110. if (androidBuild)
  111. {
  112. // create needed directories
  113. GPGSUtil.EnsureDirExists("Assets/Plugins");
  114. GPGSUtil.EnsureDirExists("Assets/Plugins/Android");
  115. // Generate AndroidManifest.xml
  116. GPGSUtil.GenerateAndroidManifest();
  117. GPGSProjectSettings.Instance.Set(GPGSUtil.NEARBYSETUPDONEKEY, true);
  118. GPGSProjectSettings.Instance.Save();
  119. // Resolve the dependencies
  120. Google.VersionHandler.VerboseLoggingEnabled = true;
  121. Google.VersionHandler.UpdateVersionedAssets(forceUpdate: true);
  122. Google.VersionHandler.Enabled = true;
  123. AssetDatabase.Refresh();
  124. Google.VersionHandler.InvokeStaticMethod(
  125. Google.VersionHandler.FindClass(
  126. "Google.JarResolver",
  127. "GooglePlayServices.PlayServicesResolver"),
  128. "MenuResolve", null);
  129. }
  130. return true;
  131. }
  132. }
  133. }
  134. #endif