MainMenu.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if (this.Button("App Requests"))
  73. {
  74. this.SwitchMenu(typeof(AppRequests));
  75. }
  76. if (this.Button("Graph Request"))
  77. {
  78. this.SwitchMenu(typeof(GraphRequest));
  79. }
  80. if (Constants.IsWeb && this.Button("Pay"))
  81. {
  82. this.SwitchMenu(typeof(Pay));
  83. }
  84. if (this.Button("App Events"))
  85. {
  86. this.SwitchMenu(typeof(AppEvents));
  87. }
  88. if (this.Button("App Links"))
  89. {
  90. this.SwitchMenu(typeof(AppLinks));
  91. }
  92. if (Constants.IsMobile && this.Button("App Invites"))
  93. {
  94. this.SwitchMenu(typeof(AppInvites));
  95. }
  96. if (Constants.IsMobile && this.Button("Access Token"))
  97. {
  98. this.SwitchMenu(typeof(AccessTokenMenu));
  99. }
  100. GUILayout.EndVertical();
  101. GUI.enabled = enabled;
  102. }
  103. private void CallFBLogin()
  104. {
  105. FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, this.HandleResult);
  106. }
  107. private void CallFBLoginForPublish()
  108. {
  109. // It is generally good behavior to split asking for read and publish
  110. // permissions rather than ask for them all at once.
  111. //
  112. // In your own game, consider postponing this call until the moment
  113. // you actually need it.
  114. FB.LogInWithPublishPermissions(new List<string>() { "publish_actions" }, this.HandleResult);
  115. }
  116. private void CallFBLogout()
  117. {
  118. FB.LogOut();
  119. }
  120. private void OnInitComplete()
  121. {
  122. this.Status = "Success - Check log for details";
  123. this.LastResponse = "Success Response: OnInitComplete Called\n";
  124. string logMessage = string.Format(
  125. "OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'",
  126. FB.IsLoggedIn,
  127. FB.IsInitialized);
  128. LogView.AddLog(logMessage);
  129. if (AccessToken.CurrentAccessToken != null)
  130. {
  131. LogView.AddLog(AccessToken.CurrentAccessToken.ToString());
  132. }
  133. }
  134. private void OnHideUnity(bool isGameShown)
  135. {
  136. this.Status = "Success - Check log for details";
  137. this.LastResponse = string.Format("Success Response: OnHideUnity Called {0}\n", isGameShown);
  138. LogView.AddLog("Is game shown: " + isGameShown);
  139. }
  140. }
  141. }