Building a social media sharing Android plugin for Unity.
Written by Michael on March 16, 2015
When writing a game with friends using Swift we had a feature where players could share their score via a screenshot to social media. I needed to implement this in the Unity version of the game for both Android and iPhone. Unity doesn’t provide an API for sharing to social media on Android and the SDK’s require you to set up Facebook and Twitter applications. The more efficient way is to use native sharing. Native sharing doesn’t require the player to authorize Facebook or Twitter applications when wanting to share their screen. To do this I needed to implement an Android library that Unity could interact with.
First thing I did was create a directory for the Android library using the following command:
Then inside that directory I created a blank Android library by running the following command (you will need to install the android sdk for the below to work):
Then I added the following to the build.xml that was generated:
The above allows me to build a jar by using the command ant jar. The jar file is what we will add to the Unity project a little later.
Next I created the following folders to follow the package structure:
The next step was to write the android code required to share an image and text to social media. I created the file: src/com/frispgames/frispsocial/FrispSocial.java
The code above has a couple dependencies these need to be placed in the libs directory:
You can find these here as I have open sourced the android library.
The next step was to add the following line into the AndroidManifest.xml to make sure the Unity project that uses this library inherits the correct permissions:
The final step is to run ant jar to create the jar file. This will need to be placed under the Plugins/Android/ directory in your unity project. Now you are able to interact with the methods inside the jar file using Unity. In my next blog post I will cover more about how I used this Android Library inside of a Unity project.