How to Make Share Button in unity easily

SF Games
By -
0

 How to Make Share Button in unity easily

How to Make Share Button in unity easily


To Make Share Button in unity easily follow the steps bellow :

  1. Download  " Native Share for Android & iOS" package .
  2. Create The Script SheareButton.cs.

1 .Download  Native Share package .

visite the officiel Unity  Asset Store do download The package from here


  • Click on " Add to my Assets " .
  • Go to Unity Editor .
  • Open The package manager , Select "Package : My Assets" and search for Native Share for Android & iOS" 

  • Click on Import


2 . Create The Script SheareButton.cs.

  • Add new Button ,and name it " Share Button "
  • add the following script :

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

public class ShareButton : MonoBehaviour
{
    private string shareMessage;
    public void ClickShareButton()
    {
        shareMessage = "Wawwww I cant believe I get this score in Maths Game";
        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 = Path.Combine(Application.temporaryCachePath, "shared img.png");
        File.WriteAllBytes(filePath, ss.EncodeToPNG());

        // To avoid memory leaks
        Destroy(ss);

        new NativeShare().AddFile(filePath).SetSubject("Maths Game").SetText(shareMessage).Share();
    }
}

  • than select like in image :



...
Tags:

Post a Comment

0 Comments

Post a Comment (0)
3/related/default