SHARE
TWEET

FacebookUtil Unity

a guest Dec 9th, 2014 264 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Facebook.MiniJSON;
  5.  
  6. public class FacebookUtil : ScriptableObject {
  7.     public static string GetPictureURL(string facebookID, int? width = null, int? height = null, string type = null) {
  8.         string url = string.Format("/{0}/picture", facebookID);
  9.         string query = width != null ? "&width=" + width.ToString() : "";
  10.         query += height != null ? "&height=" + height.ToString() : "";
  11.         query += type != null ? "&type=" + type : "";
  12.         query += "&redirect=false";
  13.         if (query != "")
  14.             url += ("?g" + query);
  15.         return url;
  16.     }    
  17.    
  18.     public static Dictionary<string, string> RandomFriend(List<object> friends) {
  19.         var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count)]));
  20.         var friend = new Dictionary<string, string>();
  21.         friend["id"] = (string)fd["id"];
  22.         friend["first_name"] = (string)fd["first_name"];
  23.         var pictureDict = ((Dictionary<string, object>)(fd["picture"]));
  24.         var pictureDataDict = ((Dictionary<string, object>)(pictureDict["data"]));
  25.         friend["image_url"] = (string)pictureDataDict["url"];
  26.         return friend;
  27.     }
  28.    
  29.     public static Dictionary<string, string> DeserializeJSONProfile(string response) {
  30.         var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
  31.         object nameH;
  32.         var profile = new Dictionary<string, string>();
  33.         if (responseObject.TryGetValue("first_name", out nameH)) {
  34.             profile["first_name"] = (string)nameH;
  35.         }
  36.         return profile;
  37.     }
  38.    
  39.     public static List<object> DeserializeScores(string response) {
  40.        
  41.         var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
  42.         object scoresh;
  43.         var scores = new List<object>();
  44.         if (responseObject.TryGetValue("data", out scoresh)) {
  45.             scores = (List<object>)scoresh;
  46.         }
  47.        
  48.         return scores;
  49.     }
  50.    
  51.     public static List<object> DeserializeJSONFriends(string response) {
  52.         var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
  53.         object friendsH;
  54.         var friends = new List<object>();
  55.         if (responseObject.TryGetValue("invitable_friends", out friendsH)) {
  56.             friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
  57.         }
  58.         if (responseObject.TryGetValue("friends", out friendsH)) {
  59.             friends.AddRange((List<object>)(((Dictionary<string, object>)friendsH)["data"]));
  60.         }
  61.         return friends;
  62.     }
  63.    
  64.     public static string DeserializePictureURLString(string response) {
  65.         return DeserializePictureURLObject(Json.Deserialize(response));
  66.     }
  67.    
  68.     public static string DeserializePictureURLObject(object pictureObj) {
  69.        
  70.        
  71.         var picture = (Dictionary<string, object>)(((Dictionary<string, object>)pictureObj)["data"]);
  72.         object urlH = null;
  73.         if (picture.TryGetValue("url", out urlH)) {
  74.             return (string)urlH;
  75.         }
  76.        
  77.         return null;
  78.     }  
  79.    
  80.     public static void DrawActualSizeTexture(Vector2 pos, Texture texture, float scale = 1.0f) {
  81.         Rect rect = new Rect(pos.x, pos.y, texture.width * scale, texture.height * scale);
  82.         GUI.DrawTexture(rect, texture);
  83.     }
  84.     public static void DrawSimpleText(Vector2 pos, GUIStyle style, string text) {
  85.         Rect rect = new Rect(pos.x, pos.y, Screen.width, Screen.height);
  86.         GUI.Label(rect, text, style);
  87.     }
  88.    
  89.     private static void JavascriptLog(string msg) {
  90.         Application.ExternalCall("console.log", msg);
  91.     }
  92.    
  93.     public static void Log(string message) {
  94.         Debug.Log(message);
  95.         if (Application.isWebPlayer)
  96.             JavascriptLog(message);
  97.     }
  98.     public static void LogError(string message) {
  99.         Debug.LogError(message);
  100.         if (Application.isWebPlayer)
  101.             JavascriptLog(message);
  102.     }
  103.    
  104. }
RAW Paste Data
Want to get better at C#?
Learn to code C# in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top