GraphRequest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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;
  23. using UnityEngine;
  24. internal class GraphRequest : MenuBase
  25. {
  26. private string apiQuery = string.Empty;
  27. private Texture2D profilePic;
  28. protected override void GetGui()
  29. {
  30. bool enabled = GUI.enabled;
  31. GUI.enabled = enabled && FB.IsLoggedIn;
  32. if (this.Button("Basic Request - Me"))
  33. {
  34. FB.API("/me", HttpMethod.GET, this.HandleResult);
  35. }
  36. if (this.Button("Retrieve Profile Photo"))
  37. {
  38. FB.API("/me/picture", HttpMethod.GET, this.ProfilePhotoCallback);
  39. }
  40. if (this.Button("Take and Upload screenshot"))
  41. {
  42. this.StartCoroutine(this.TakeScreenshot());
  43. }
  44. this.LabelAndTextField("Request", ref this.apiQuery);
  45. if (this.Button("Custom Request"))
  46. {
  47. FB.API(this.apiQuery, HttpMethod.GET, this.HandleResult);
  48. }
  49. if (this.profilePic != null)
  50. {
  51. GUILayout.Box(this.profilePic);
  52. }
  53. GUI.enabled = enabled;
  54. }
  55. private void ProfilePhotoCallback(IGraphResult result)
  56. {
  57. if (string.IsNullOrEmpty(result.Error) && result.Texture != null)
  58. {
  59. this.profilePic = result.Texture;
  60. }
  61. this.HandleResult(result);
  62. }
  63. private IEnumerator TakeScreenshot()
  64. {
  65. yield return new WaitForEndOfFrame();
  66. var width = Screen.width;
  67. var height = Screen.height;
  68. var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
  69. // Read screen contents into the texture
  70. tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  71. tex.Apply();
  72. byte[] screenshot = tex.EncodeToPNG();
  73. var wwwForm = new WWWForm();
  74. wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
  75. wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
  76. FB.API("me/photos", HttpMethod.POST, this.HandleResult, wwwForm);
  77. }
  78. }
  79. }