Skip to content

Instantly share code, notes, and snippets.

@giacomelli
Last active July 7, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giacomelli/c03ec79fa0f0def94726f4b6914b4769 to your computer and use it in GitHub Desktop.
Save giacomelli/c03ec79fa0f0def94726f4b6914b4769 to your computer and use it in GitHub Desktop.
SocialShare component using NativeShare plugin (https://github.com/yasirkula/UnityNativeShare)
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment