Facebook Story Kit for Android

Facebook Story Kit for Android Documentation

Introduction

This Unity package lets you share your funny videos or pictures of your game directly to Facebook Android application.

Installation

  • Download the package from Package Manager
  • Import it in your project
  • Download Google’s Unity Jar Resolver from: Unity Jar Resolver and import in your project. After that resolve all dependencies by using: Assets -> External Dependency Manager -> Android Resolver -> Resolve or Force Resolve
  • And that’s it, you can start coding!

Setup

Share

Sharing content using Facebook Share helper is easier than anything else, but before that there are a few things that needs to be done.

  • Open your main AndroidManifest.xml file in Assets/Plugins/Android/ folder and add this code inside tag:

      <provider
          android:name="androidx.core.content.FileProvider"
          android:authorities="com.package.name.fileprovider"
          android:exported="false"
          android:grantUriPermissions="true">
          <meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_paths"/>
      </provider>
    

Where com.package.name is the package name of your application.

  • Add this permission if it’s not already included in your AndroidManifest.xml file:

      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
  • Last but not least add <queries> tag inside your manifest file:

      <queries>
          <package android:name="com.facebook.katana" />
      </queries>
    

Here is a sample AndroidManifest.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" android:installLocation="preferExternal" android:theme="@android:style/Theme.NoTitleBar" android:versionCode="1" android:versionName="1.0">
        <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
        <queries>
            <package android:name="com.facebook.katana" />
        </queries>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
            <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
                <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
            </activity>
            <provider android:name="androidx.core.content.FileProvider" android:authorities="com.hardartcore.facebookshare.fileprovider" android:exported="false" android:grantUriPermissions="true">
                <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>
            </provider>
        </application>
    </manifest>

And after the setup is complete you can use the helper class as:

    public class DemoSceneScript : MonoBehaviour
    {
        // Share image to Facebook Story by providing the path to the image file
        public void ShareImageToStory()
        {
            FacebookStoryShareHelper.ShareImageToFacebookStory(_imageFilePath);
        }
        // Share image & sticker to Facebook Story by providing file paths
        public void ShareImageAndStickerToStory()
        {
            FacebookStoryShareHelper.ShareImageWithStickerToFacebookStory(_imageFilePath, _stickerFilePath);
        }
        // Share image with gradient to Facebook Story by providing file paths and color values
        public void ShareImageWithGradientToStory()
        {
            FacebookStoryShareHelper.ShareStickerWithGradientToFacebookStory(_imageFilePath, "#CC3322", "#99AE32");
        }
        // Share video to Facebook Story by providing file path
        public void ShareVideo()
        {
            FacebookStoryShareHelper.ShareVideoToFacebookStory(_videoFilePath);
        }
        // Share video & sticker to Facebook Story by providing file paths
        public void ShareVideoWithSticker()
        {
            FacebookStoryShareHelper.ShareVideoWithStickerToFacebookStory(_videoFilePath, _stickerFilePath);
        }
        // Share video to Facebook Reel by providing file path
        public void ShareVideoReel()
        {
            FacebookStoryShareHelper.ShareVideoReel(_videoFilePath);
        }
        // Share video & sticker to Facebook Reel by providing file path
        public void ShareVideoReelWithSticker()
        {
            FacebookStoryShareHelper.ShareVideoReelWithSticker(_videoFilePath, _stickerFilePath);
        }
    }

Build Errors

If you encounter an issue: unexpected element <queries> found in <manifest> There is an easy fix for that: go to File -> Build Settings -> Player Settings -> Publishing Settings and select Custom Base Gradle Template. After that open the generated file baseProjectTemplate.gradle under Assets/Plugins/Android folder and change com.android.tools.build:gradle:*** to

Unity 2019.4+ -> com.android.tools.build:gradle:3.4.3
Unity 2021.3+ -> com.android.tools.build:gradle:4.0.1