FBCross Example Codes

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using SimpleJSON;
using FBC.Core;

public class FBCrossExample : MonoBehaviour
{
    public GUITexture myPhoto;
    public string facebookAppId;//Set "FACEBOOK_APP_ID"
    public string appSecret;
    private FBCross facebook;
    public string response = string.Empty;

    private float cursorY;
    private float cursorYInterval = 40;

    public void Awake()
    {
        //Attention : For Official WP Facebook SDK call like this :
        //FB.Init(null, "FACEBOOK_APP_ID", FBSettings.Cookie, FBSettings.Logging, FBSettings.Status, FBSettings.Xfbml, FBSettings.FrictionlessRequests, null, "http://www.ersindemir.net");

        facebook = FBCross.Init(facebookAppId, "http://www.ersindemir.net");
    }

    public void Start()
    {
        if (string.IsNullOrEmpty(facebookAppId))
        {
            Debug.LogError("FacebookTest.facebookAppId : facebookAppId can Not NULL");
        }
        if (facebook.AccessToken != null && facebook.ExpiresIn != DateTime.MinValue)
        {
            response = string.Format("Access Token : {0}\nExpires In:{1}", facebook.AccessToken, facebook.ExpiresIn);
        }
    }

    void OnGUI()
    {
        GUI.Label(new Rect(150, 10, Screen.width - 150, Screen.height), response);

        cursorY = 10;
        if (GUI.Button(new Rect(10, cursorY, 90, 30), "Log In"))
        {
            facebook.LogIn(new string[] { @"publish_actions", @"user_friends", @"user_photos", @"email" }, LoginComplete);
        }

        if (!facebook.IsLoggedIn)
        {
            return;
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 90, 30), "Log Out"))
        {
            facebook.LogOut();
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "ExtendAccessToken"))
        {
            facebook.ExtendAccessToken(appSecret, OnExtendTokenComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "IsSessionValidOnFacebook"))
        {
            facebook.IsSessionValidOnFacebook(OnIsSessionValidOnFacebookComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 130, 30), "GetMyFacebookInfo"))
        {
            facebook.GetMyFacebookInfo(GetMyFacebookInfoComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 140, 30), "GetMyFacebookId"))
        {
            facebook.GetMyFacebookId(GetFacebookIdComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 140, 30), "GetMyFacebookName"))
        {
            facebook.GetMyFacebookName(GetMyFacebookNameComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "GetMyFacebookFriendGamePlayerList"))
        {
            facebook.GetMyFacebookFriendGamePlayerList(GetMyFacebookFriendGamePlayerListComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "GetMyFacebookPhoto"))
        {
            facebook.GetMyFacebookPhoto(GetMyFacebookPhotoComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 180, 30), "GetInvatableFriends"))
        {
            facebook.GetInvatableFriends(GetInvitableFriendsComplete, 10);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "PostOnMyWall"))
        {
            facebook.PostMessageOnMyWall("This is post message From Unity3d", "www.unity3d.com", null, PostMessageOnMyWallComplete);//This is post message From Unity3d
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "PostPhotoOnMyWall"))
        {
            facebook.PostPhotoOnMyWall(TakeScreenShot(), PostPhotoOnMyWallComplete, "This photo has been post from Unity3d");
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "GetEmail"))
        {
            facebook.GetMyEmail(OnGetEmailComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "IsPermissionsGranted"))
        {
            facebook.IsPermissionsGranted(new string[] { "user_birthday" }, OnIsPermissionGranted);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "Get New Permission"))
        {
            facebook.GetNewPermissionIfNotExist("user_birthday", OnGetNewPermissionIfNotExist);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "Feed Request"))
        {
            facebook.FeedRequest(string.Empty, "www.ersindemir.net", "linkName", "linkCaption", "linkDescripton",
                "http://www.ersindemir.net/resume/images/fuwp.png", string.Empty, "", "", "", null, FeedRequestComplete);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 150, 30), "App Request"))
        {
            facebook.AppRequest("Message", null, null, null, null, "", "", "", "send", OnAppRequest);
        }

        cursorY += cursorYInterval;
        if (GUI.Button(new Rect(10, cursorY, 90, 30), "Exit"))
        {
            Application.Quit();
        }
    }

    #region Callbacks

    public void OnExtendTokenComplete(FBCrossResult fbCrossResult)
    {
        if (fbCrossResult.IsSuccess)
        {
            response = string.Empty;
            response += string.Format("Is Logged In : {0}\n", facebook.IsLoggedIn);
            response += string.Format("Access Token : {0}\n", facebook.AccessToken);
            response += string.Format("Expires Date : {0}\n", facebook.ExpiresIn);
        }
        else
        {
            response = fbCrossResult.Error;
        }
    }

    public void OnIsSessionValidOnFacebookComplete(bool isValid)
    {
        response = "Is Session Valid : " + isValid;
    }

    public void OnGetEmailComplete(FBCrossResult fbCrossResult)
    {
        response = fbCrossResult.IsSuccess ? fbCrossResult.Text : fbCrossResult.Error;
    }

    public void OnAppRequest(FBCrossResult fbCrossResult)
    {
        if (fbCrossResult.RequestStatus == FBC.Core.FBCrossResult.RequestCompleteStatus.Success)
        {
            response = fbCrossResult.Text;
        }
        else
        {
            response = fbCrossResult.Error;
        }
    }

    public void OnGetNewPermissionIfNotExist(FBCrossResult fbCrossResult)
    {
        if (fbCrossResult.RequestStatus == FBC.Core.FBCrossResult.RequestCompleteStatus.Success)
        {
            response = fbCrossResult.Text;
        }
        else
        {
            response = fbCrossResult.Error;
        }
    }

    public void OnIsPermissionGranted(Dictionary<string, bool> result)
    {
        string resultStr = string.Empty;

        if (result != null)
        {
            foreach (var item in result)
            {
                resultStr += string.Format("{0} : {1}\n", item.Key, item.Value);
            }
        }
        else
        {
            resultStr = "Error Occur.You can send request manually.(use me/permssions)";
        }

        response = resultStr;
    }

    public void LoginComplete(FBCrossResult fbCrossResult)
    {
        if (fbCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            response = string.Empty;
            response += string.Format("Is Logged In : {0}\n", facebook.IsLoggedIn);
            response += string.Format("Access Token : {0}\n", facebook.AccessToken);
            response += string.Format("Expires Date : {0}\n", facebook.ExpiresIn);
        }
        else
        {
            response = fbCrossResult.Error;
        }
    }

    public void GetMyFacebookInfoComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;
            response += string.Format("Id: {0}\n", jsonNode["id"]);
            response += string.Format("Name: {0}\n", jsonNode["name"]);
            response += string.Format("Link: {0}\n", jsonNode["link"].ToString().Replace(@"\", ""));
            response += string.Format("First Name: {0}\n", jsonNode["first_name"]);
            response += string.Format("Last Name: {0}\n", jsonNode["last_name"]);
            try
            {
                response += string.Format("HomeTown Id : {0}\n", jsonNode["hometown"]["id"]);
                response += string.Format("HomeTown Name : {0}\n", jsonNode["hometown"]["name"]);
                response += string.Format("Location Id : {0}\n", jsonNode["location"]["id"]);
                response += string.Format("Location Name : {0}\n", jsonNode["location"]["name"]);
            }
            catch (Exception exp)
            {
                Debug.Log(exp.Message);
            }
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void GetFacebookIdComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;
            response += string.Format("Facebook Id: {0}", jsonNode["id"]);
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void GetMyFacebookNameComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;
            response += string.Format("Facebook Name: {0}", jsonNode["name"]);
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void PostMessageOnMyWallComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;
            response += string.Format("Post Id :{0}", jsonNode["id"]);
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void GetMyFacebookFriendGamePlayerListComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;

            foreach (JSONNode friendInfo in jsonNode["data"].AsArray)
            {
                response += string.Format("Friend Picture Url : {0}\n", friendInfo["pic_square"].ToString().Replace(@"\", ""));
                response += string.Format("Friend Name: {0}\n", friendInfo["name"]);
                response += string.Format("Friend Facebook Id: {0}\n", friendInfo["uid"]);
                response += "\n";
            }
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void FeedRequestComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            response = fBCrossResult.Text;
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void GetInvitableFriendsComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            response = fBCrossResult.Text;
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void GetMyFacebookPhotoComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            myPhoto.texture = fBCrossResult.Texture;
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    public void PostPhotoOnMyWallComplete(FBCrossResult fBCrossResult)
    {
        if (fBCrossResult.RequestStatus == FBCrossResult.RequestCompleteStatus.Success)
        {
            JSONNode jsonNode = JSONNode.Parse(fBCrossResult.Text);
            response = string.Empty;
            response += string.Format("Post Id :{0}", jsonNode["id"]);
        }
        else
        {
            response = fBCrossResult.Error;
        }
    }

    #endregion

    public byte[] TakeScreenShot()
    {
        Camera camera = Camera.main;

        int resWidth = Screen.width;
        int resHeight = Screen.height;
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
        camera.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        byte[] bytes = screenShot.EncodeToPNG();
        return bytes;
    }

    #region Example Codes of Facebook

    public void GetMyFacebookInfo(Action<FBCrossResult> callAction = null)
    {
        facebook.RequestGet("me", null, callAction);
    }

    public void GetMyFacebookId(Action<FBCrossResult> callAction = null)
    {
        Dictionary<string, object> param = new Dictionary<string, object>();
        param["fields"] = "id";
        facebook.RequestGet("me", param, callAction);
    }

    public void GetMyFacebookPhoto(Action<FBCrossResult> callAction = null)
    {
        facebook.RequestGet("me/picture", null, callAction);
    }


    #endregion
}

Leave a Reply

Your email address will not be published. Required fields are marked *