MainMenu.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  3. *
  4. * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  5. * copy, modify, and distribute this software in source code or binary form for use
  6. * in connection with the web services and APIs provided by Facebook.
  7. *
  8. * As with any software that integrates with the Facebook platform, your use of
  9. * this software is subject to the Facebook Developer Principles and Policies
  10. * [http://developers.facebook.com/policy/]. This copyright notice shall be
  11. * included in all copies or substantial portions of the software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. namespace Facebook.Unity.Example
  21. {
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using UnityEngine;
  25. internal sealed class MainMenu : MenuBase
  26. {
  27. protected override bool ShowBackButton()
  28. {
  29. return false;
  30. }
  31. protected override void GetGui()
  32. {
  33. GUILayout.BeginVertical();
  34. bool enabled = GUI.enabled;
  35. if (this.Button("FB.Init"))
  36. {
  37. FB.Init(this.OnInitComplete, this.OnHideUnity);
  38. this.Status = "FB.Init() called with " + FB.AppId;
  39. }
  40. GUILayout.BeginHorizontal();
  41. GUI.enabled = enabled && FB.IsInitialized;
  42. if (this.Button("Login"))
  43. {
  44. this.CallFBLogin();
  45. this.Status = "Login called";
  46. }
  47. GUI.enabled = FB.IsLoggedIn;
  48. if (this.Button("Get publish_actions"))
  49. {
  50. this.CallFBLoginForPublish();
  51. this.Status = "Login (for publish_actions) called";
  52. }
  53. // Fix GUILayout margin issues
  54. GUILayout.Label(GUIContent.none, GUILayout.MinWidth(ConsoleBase.MarginFix));
  55. GUILayout.EndHorizontal();
  56. GUILayout.BeginHorizontal();
  57. // Fix GUILayout margin issues
  58. GUILayout.Label(GUIContent.none, GUILayout.MinWidth(ConsoleBase.MarginFix));
  59. GUILayout.EndHorizontal();
  60. #if !UNITY_WEBGL
  61. if (this.Button("Logout"))
  62. {
  63. CallFBLogout();
  64. this.Status = "Logout called";
  65. }
  66. #endif
  67. GUI.enabled = enabled && FB.IsInitialized;
  68. if (this.Button("Share Dialog"))
  69. {
  70. this.SwitchMenu(typeof(DialogShare));
  71. }
  72. bool savedEnabled = GUI.enabled;
  73. GUI.enabled = enabled &&
  74. AccessToken.CurrentAccessToken != null &&
  75. AccessToken.CurrentAccessToken.Permissions.Contains("publish_actions");
  76. if (this.Button("Game Groups"))
  77. {
  78. this.SwitchMenu(typeof(GameGroups));
  79. }
  80. GUI.enabled = savedEnabled;
  81. if (this.Button("App Requests"))
  82. {
  83. this.SwitchMenu(typeof(AppRequests));
  84. }
  85. if (this.Button("Graph Request"))
  86. {
  87. this.SwitchMenu(typeof(GraphRequest));
  88. }
  89. if (Constants.IsWeb && this.Button("Pay"))
  90. {
  91. this.SwitchMenu(typeof(Pay));
  92. }
  93. if (this.Button("App Events"))
  94. {
  95. this.SwitchMenu(typeof(AppEvents));
  96. }
  97. if (this.Button("App Links"))
  98. {
  99. this.SwitchMenu(typeof(AppLinks));
  100. }
  101. if (Constants.IsMobile && this.Button("App Invites"))
  102. {
  103. this.SwitchMenu(typeof(AppInvites));
  104. }
  105. if (Constants.IsMobile && this.Button("Access Token"))
  106. {
  107. this.SwitchMenu(typeof(AccessTokenMenu));
  108. }
  109. GUILayout.EndVertical();
  110. GUI.enabled = enabled;
  111. }
  112. private void CallFBLogin()
  113. {
  114. FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, this.HandleResult);
  115. }
  116. private void CallFBLoginForPublish()
  117. {
  118. // It is generally good behavior to split asking for read and publish
  119. // permissions rather than ask for them all at once.
  120. //
  121. // In your own game, consider postponing this call until the moment
  122. // you actually need it.
  123. FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, this.HandleResult);
  124. }
  125. private void CallFBLogout()
  126. {
  127. FB.LogOut();
  128. }
  129. private void OnInitComplete()
  130. {
  131. this.Status = "Success - Check log for details";
  132. this.LastResponse = "Success Response: OnInitComplete Called\n";
  133. string logMessage = string.Format(
  134. "OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'",
  135. FB.IsLoggedIn,
  136. FB.IsInitialized);
  137. LogView.AddLog(logMessage);
  138. if (AccessToken.CurrentAccessToken != null)
  139. {
  140. LogView.AddLog(AccessToken.CurrentAccessToken.ToString());
  141. }
  142. }
  143. private void OnHideUnity(bool isGameShown)
  144. {
  145. this.Status = "Success - Check log for details";
  146. this.LastResponse = string.Format("Success Response: OnHideUnity Called {0}\n", isGameShown);
  147. LogView.AddLog("Is game shown: " + isGameShown);
  148. }
  149. }
  150. }