在Unity製作Facebook Share分享功能

views

Prime [31] Social Networking Plugin在Unity的設定

  • 將Prime[31]的Social Networking Plugin載入
  • 將Facebook App ID填入Plistunity-to-facebook-social-networking-plugin-prime-31-2

建立一個將Facebook相關功能整合到遊戲(Script)

主要分成幾個步驟:

  • Facebook功能初始化
  • 使用者Facebook帳號登入+取得使用者資料

以下分段說明:

Facebook功能初始化

在官方範例中是按下按鈕才初始化 為簡化流程這邊改成自動

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Prime31;

public class ShareButtonTestOnGUI : MonoBehaviour 
{
  #if UNITY_IPHONE || UNITY_ANDROID
  private string _userId;
  private string _userNameFirst; 
  private int bestScore=123; //link to highscore later
  
  
  // common event handler used for all graph requests that logs the data to the console
  void completionHandler( string error, object result )
  {
    if( error != null )
      Debug.LogError( error );
    else
      Prime31.Utils.logObject( result );
  }


  // Use this for initialization
  void Start () 
  {
    // dump custom data to log after a request completes
    FacebookManager.graphRequestCompletedEvent += result =>
    {
      Prime31.Utils.logObject( result );
    };
    
    //Initialize Facebook plugin here
    FacebookCombo.init();
  
  }
  
  
  void OnGUI()
  {
    Debug.Log("Share"+CStoJS.saved);
    
    //Login Button
    if (GUI.Button(new Rect(10,70,90,30), "LOGIN"))
    {
      // Note: requesting publish permissions here will result in a crash. Only read permissions are permitted.
      var permissions = new string[] { "email" };
      FacebookCombo.loginWithReadPermissions( permissions );
      
      //User data request
      Facebook.instance.getMe( ( error, result ) =>
      {
        // if we have an error we dont proceed any further
        if( error != null )
          return;
  
        if( result == null )
          return;
  
        // grab the userId and persist it for later use
        _userId = result.id;
        _userNameFirst = result.first_name; //by marc
      
        Debug.Log( "me Graph Request finished: " );
        Debug.Log( result );
      });
    }
    
    
    
    // parameters are optional. See Facebook's documentation for all the dialogs and paramters that they support
    //--see here: https://developers.facebook.com/docs/reference/dialogs/feed/
    if (GUI.Button(new Rect(10, 130, 90, 30), "SHARE"))
    {
      if( _userNameFirst != null )
      {
        var parameters = new Dictionary<string,string>
        {
          { "link", "http://goo.gl/WZGLVI" }, //use URL shortener if it's too long
          { "name", _userNameFirst+" just saved " + score + " persons!!" },
          { "picture", "http://wordpress-tw.marcpov.com/wp-content/uploads/top-trend-of-2013.png" },
          { "caption", "go get BOUNCY BOUNCE" }
        };
        FacebookCombo.showDialog( "stream.publish", parameters ); //this is for user can key in some words
      }  
      
      if( _userNameFirst == null )
      {
        //User data request
        Facebook.instance.getMe( ( error, result ) =>
        {
          // if we have an error we dont proceed any further
          if( error != null )
            return;
    
          if( result == null )
            return;
    
          // grab the userId and persist it for later use
          _userId = result.id;
          _userNameFirst = result.first_name; //by marc
        
          Debug.Log( "me Graph Request finished: " );
          Debug.Log( result );
        });  
      }
    }
    
  }


#endif
}

									

使用者Facebook帳號登入(LOGIN) 並同時取得使用者資料

unity-to-facebook-social-networking-plugin-prime-31-4

  • A:使用者Facebook帳號按下LOGIN後登入Facebook
  • B: 同時取得使用者資料 並且將使用者的名字存在變數 _userNameFirst

//Login Button
    if (GUI.Button(new Rect(10,70,90,30), "LOGIN"))
    {
      // Note: requesting publish permissions here will result in a crash. Only read permissions are permitted.
      var permissions = new string[] { "email" };
      FacebookCombo.loginWithReadPermissions( permissions );
      
      //User data request
      Facebook.instance.getMe( ( error, result ) =>
      {
        // if we have an error we dont proceed any further
        if( error != null )
          return;
  
        if( result == null )
          return;
  
        // grab the userId and persist it for later use
        _userId = result.id;
        _userNameFirst = result.first_name; //by marc
      
        Debug.Log( "me Graph Request finished: " );
        Debug.Log( result );
      });
    }
									

分享遊戲成果到Facebook Timeline (SHARE button)

unity-to-facebook-social-networking-plugin-prime-31-4

  • 注意:網址連結過長可能會導致錯誤 可以參考這篇文章將網址變短

// parameters are optional. See Facebook's documentation for all the dialogs and paramters that they support
    //--see here: https://developers.facebook.com/docs/reference/dialogs/feed/
    if (GUI.Button(new Rect(10, 130, 90, 30), "SHARE"))
    {
      if( _userNameFirst != null )
      {
        var parameters = new Dictionary<string,string>
        {
          { "link", "http://goo.gl/WZGLVI" }, //use URL shortener if it's too long
          { "name", _userNameFirst+" just saved " + score + " persons!!" },
          { "picture", "http://wordpress-tw.marcpov.com/wp-content/uploads/top-trend-of-2013.png" },
          { "caption", "go get BOUNCY BOUNCE" }
        };
        FacebookCombo.showDialog( "stream.publish", parameters ); //this is for user can key in some words
      }  
      
      if( _userNameFirst == null )
      {
        //User data request
        Facebook.instance.getMe( ( error, result ) =>
        {
          // if we have an error we dont proceed any further
          if( error != null )
            return;
    
          if( result == null )
            return;
    
          // grab the userId and persist it for later use
          _userId = result.id;
          _userNameFirst = result.first_name; //by marc
        
          Debug.Log( "me Graph Request finished: " );
          Debug.Log( result );
        });  
      }
    }
									

完整C#程式碼參考如下:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Prime31;

public class ShareButtonTestOnGUI : MonoBehaviour 
{
  #if UNITY_IPHONE || UNITY_ANDROID
  private string _userId;
  private string _userNameFirst; 
  private int bestScore=123; //link to highscore later
  
  
  // common event handler used for all graph requests that logs the data to the console
  void completionHandler( string error, object result )
  {
    if( error != null )
      Debug.LogError( error );
    else
      Prime31.Utils.logObject( result );
  }


  // Use this for initialization
  void Start () 
  {
    // dump custom data to log after a request completes
    FacebookManager.graphRequestCompletedEvent += result =>
    {
      Prime31.Utils.logObject( result );
    };
    
    //Initialize Facebook plugin here
    FacebookCombo.init();
  
  }
  
  
  void OnGUI()
  {
    Debug.Log("Share"+CStoJS.saved);
    
    //Login Button
    if (GUI.Button(new Rect(10,70,90,30), "LOGIN"))
    {
      // Note: requesting publish permissions here will result in a crash. Only read permissions are permitted.
      var permissions = new string[] { "email" };
      FacebookCombo.loginWithReadPermissions( permissions );
      
      //User data request
      Facebook.instance.getMe( ( error, result ) =>
      {
        // if we have an error we dont proceed any further
        if( error != null )
          return;
  
        if( result == null )
          return;
  
        // grab the userId and persist it for later use
        _userId = result.id;
        _userNameFirst = result.first_name; //by marc
      
        Debug.Log( "me Graph Request finished: " );
        Debug.Log( result );
      });
    }
    
    
    
    // parameters are optional. See Facebook's documentation for all the dialogs and paramters that they support
    //--see here: https://developers.facebook.com/docs/reference/dialogs/feed/
    if (GUI.Button(new Rect(10, 130, 90, 30), "SHARE"))
    {
      if( _userNameFirst != null )
      {
        var parameters = new Dictionary<string,string>
        {
          { "link", "http://goo.gl/WZGLVI" }, //use URL shortener if it's too long
          { "name", _userNameFirst+" just saved " + score + " persons!!" },
          { "picture", "http://wordpress-tw.marcpov.com/wp-content/uploads/top-trend-of-2013.png" },
          { "caption", "go get BOUNCY BOUNCE" }
        };
        FacebookCombo.showDialog( "stream.publish", parameters ); //this is for user can key in some words
      }  
      
      if( _userNameFirst == null )
      {
        //User data request
        Facebook.instance.getMe( ( error, result ) =>
        {
          // if we have an error we dont proceed any further
          if( error != null )
            return;
    
          if( result == null )
            return;
    
          // grab the userId and persist it for later use
          _userId = result.id;
          _userNameFirst = result.first_name; //by marc
        
          Debug.Log( "me Graph Request finished: " );
          Debug.Log( result );
        });  
      }
    }
    
  }


#endif
}

									

*Prime31原始的Script可以在project中搜尋“FacebookComboUI”  以上為修改版本

This entry was posted in Facebook, Unity Plugin and tagged by Mr.M. Bookmark the permalink.


8 thoughts on “在Unity製作Facebook Share分享功能

  1. 喔!我知道我的問題在哪裡了…原來他最新版本social networking初始化設定不一樣…
    而android的版本則是進入他的xml當中填入你的app id就可以了.

  2. 嗯嗯,謝謝你的解答,因為我現在的問題就是在初始化的設定上我也已經做了,可是我要在FacebookBinding.init()當中輸入我的app id的話,unity就會出現錯誤.

  3. 先快速回答你的問題,
    1.看了一下Prime31上的給Android開發用的文件 初始化的設定似乎與iOS的方法有所不同 但因為我沒實際用過Android版的 所以這部份只能回答到這

    2.在iOS上 我去看了一下Social Networking文件頁面 看來應該如你說的 你的Social Networking plugin是新版的 所以跟我的版本內容不同
    按照文件來看 各項功能呼叫方式應該差不了多少.
    其中Prime31有說到 要執行之前 要先做好初始化的設定:
    Once your application is setup, you will need call the “FacebookBinding.init”
    主要是將你的Facebook的App ID填入像這樣:FacebookBinding.init(你的Facebook app ID)
    通常初始化ok後 使用測試場景就可直接build到你的iOS設備使用了 不好意思 因為資訊不夠比較難解決你的問題

  4. 嗯嗯…因為我寄了信過去可是都沒有收到他們的回音…

    那麼我想再請問一下,如果是用在android上的話呢? 步驟上也是一樣嗎?

    (因為ios的都發佈不出來,所以android的也一直不敢下手買…)

  5. 如果Prime 31的social networking套件的內容與我的不同 是有可能的
    因為Prime31會一直出更新版本
    建議你可以email去問他們 並告知他們你的Unity版本, social networking套件的版本
    或是你也可以讓我知道更多的相關資訊 看看我有沒有辦法幫到你

  6. 你好,想請問一下,因為我一樣去下載了prime31的ios social networking插件,但是它們插件裡面的內容卻和你教程裡的內容不一樣,而且還不能執行.
    他們的scene名為FacebookTestScene

Leave a Reply

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