unitytips: Social Share
07/07/2020
Using Süleyman Yasir KULA Unity Native Share Plugin you can easily add native share to your Android/iOS games.
I made the gist below that uses the plugin to add a social share component to any game object.

Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using UnityEngine; | |
namespace Giacomelli.Framework | |
{ | |
public class SocialShare : MonoBehaviour | |
{ | |
[SerializeField] | |
string _title; | |
[SerializeField] | |
[Multiline] | |
string _text; | |
[SerializeField] | |
Texture2D[] _images; | |
public void Share() | |
{ | |
Share(null); | |
} | |
public void ShareWithScreenshot() | |
{ | |
Share(ScreenCapture.CaptureScreenshotAsTexture()); | |
} | |
void Share(Texture2D screenshot) | |
{ | |
var ns = new NativeShare(); | |
if (string.IsNullOrEmpty(_title)) | |
{ | |
ns.SetTitle(_text); | |
ns.SetSubject(_text); | |
} | |
else | |
{ | |
ns.SetTitle(_title); | |
ns.SetSubject(_title); | |
} | |
ns.SetText(_text); | |
if (screenshot != null) | |
ns.AddFile(GetFilePath(screenshot)); | |
foreach (var img in _images) | |
{ | |
ns.AddFile(GetFilePath(img)); | |
} | |
ns.Share(); | |
} | |
private string GetFilePath(Texture2D texture) | |
{ | |
var filePath = Path.Combine(Application.temporaryCachePath, $"{Guid.NewGuid()}.png"); | |
File.WriteAllBytes(filePath, texture.EncodeToPNG()); | |
return filePath; | |
} | |
} | |
} |
Loading comments...