Published on

Implemented a function to share screen shots to SNS (Unity / Native Share for Android & iOS)

Authors
  • Evelyn

Please share this article if you like!

Welcome to the Evelyn GameDev Blog, Evelyn here! I'm an individual game developer, so I can't spend much money on advertising. If you visited this blog post, I'm sure you have the same problem.

So, in order to increase the chances that users who have played the game will spread the word on SNS, we will show you how to implement a function to share screenshots of the game screen on SNS.

I'd like to increase word-of-mouth about the games I develop and increase the number of active users without spending money on advertising.

This article will help you solve those problems.

It's a short 5-minute video that explains the content of this article in detail.


What you can learn from this article

I'll show you how to share your Unity game screen to social networking sites!


Table of Contents

Importing a free package

To make the implementation very easy, let's use a free Unity asset. We will be using an asset called "Native Share for Android & iOS".

Click on the link below to access the asset store and import it into your Unity project.

Native Share for Android & iOS

Press the Import button and wait for the folder to be created in the project.

Importing packages

Take advantage of a handy package called Native Share for Android & iOS!

Create a script

Once the package import is complete, the next step is to create a script, create a C# file and copy and paste the following code into it.

SnsShare.cs

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

public class SnsShare : MonoBehaviour
{
    public void Share()
    {
        StartCoroutine(TakeScreenShotAndShare());
    }

    private IEnumerator TakeScreenShotAndShare()
    {
        yield return new WaitForEndOfFrame();

        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

        string filePath = System.IO.Path.Combine(Application.temporaryCachePath, "share.png");
        System.IO.File.WriteAllBytes(filePath, ss.EncodeToPNG());

        Destroy(ss);

        new NativeShare().AddFile(filePath)
            .SetSubject("").SetText("").SetUrl("")
            .SetCallback((res, target) => Debug.Log($"result {res}, target app: {target}"))
            .Share();
    }
}

Let's take a look at the script one by one.

yield return new WaitForEndOfFrame();

Use a coroutine to wait until the end of the frame. If you don't do this, the screenshot image may turn black in some cases.

Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
ss.Apply();

This is the description for creating a screenshot.

string filePath = System.IO.Path.Combine(Application.temporaryCachePath, "share.png");
System.IO.File.WriteAllBytes(filePath, ss.EncodeToPNG());

Create a Png file.

Destroy(ss);

Now that we have an image file, let's discard the screenshot data to prevent a memory leak.

new NativeShare().AddFile(filePath)
  .SetSubject("").SetText("").SetUrl("")
  .SetCallback((res, target) => Debug.Log($"result {res}, target app: {target}"))
  .Share();

Finally, it creates an instance of the NativeShare class and executes the Share method. You can set the title, text, and Url freely.

Attaching a script

Let's attach the script we created to the game object and register the Share method to the button click event.

Demo

That's it, the work is done. I used this code and incorporated it into my own game. Please check out its behavior.

It would be great if people who play the game share it on SNS and the game spreads by word of mouth. I'm happy if I can be of help to you.

I love you for reading to the end.

Please share this article if you like!