Skip to content

Instantly share code, notes, and snippets.

@luispedrofonseca
Created June 18, 2014 16:24
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 luispedrofonseca/2dcb93154812ee3d452b to your computer and use it in GitHub Desktop.
Save luispedrofonseca/2dcb93154812ee3d452b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class ShareButton : MonoBehaviour
{
Texture2D _texture;
RenderTexture _renderTexture;
void OnGUI()
{
if (GUI.Button(new Rect (10, 10, 80, 50), "SHARE"))
{
StartCoroutine(PostToInstagram());
}
}
byte[] GrabScreenshot()
{
if(_texture != null)
Destroy(_texture);
// Initialize and render
_renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
Camera.main.targetTexture = _renderTexture;
Camera.main.Render();
RenderTexture.active = _renderTexture;
// Read pixels
_texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
_texture.ReadPixels(new Rect(0,0,Screen.width,Screen.height), 0, 0);
// Clean up
Camera.main.targetTexture = null;
RenderTexture.active = null;
DestroyImmediate(_renderTexture);
return _texture.EncodeToPNG();
}
IEnumerator PostToInstagram()
{
yield return new WaitForEndOfFrame();
InstagramShare.PostToInstagram("Hello from Unity!", GrabScreenshot());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment