Created
August 11, 2017 21:51
-
-
Save cjacobwade/f3f51824a99b5bb22315ca757425cc5a to your computer and use it in GitHub Desktop.
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 UnityEngine; | |
using System.Collections; | |
using System.IO; | |
public class ExportScreenshot : SingletonBehaviour<ExportScreenshot> | |
{ | |
// You need to register your game or application in Twitter to get cosumer key and secret. | |
// Go to this page for registration: http://dev.twitter.com/apps/new | |
string CONSUMER_KEY = "FindYoursFromTwitter.com"; | |
string CONSUMER_SECRET = "FindYoursFromTwitter.com"; | |
string ACCESS_TOKEN = "FindYoursFromTwitter.com"; | |
string ACCESS_TOKEN_SECRET = "FindYoursFromTwitter.com"; | |
public void TakeShot() | |
{ | |
string fileName = "BeautyShot_" + Random.Range(0, 1000000).ToString() + ".png"; | |
Application.CaptureScreenshot(fileName); | |
StartCoroutine(Twitter.API.PostTweet(GetScreenshotBytes(), CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET, new Twitter.PostTweetCallback(this.OnPostTweet))); | |
} | |
byte[] GetScreenshotBytes() | |
{ | |
Camera cam = Camera.main; | |
int resWidth = cam.pixelWidth; | |
int resHeight = cam.pixelHeight; | |
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); | |
cam.targetTexture = rt; | |
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); | |
cam.Render(); | |
RenderTexture.active = rt; | |
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); | |
cam.targetTexture = null; | |
RenderTexture.active = null; // JC: added to avoid errors | |
Destroy(rt); | |
return screenShot.EncodeToPNG(); | |
} | |
void OnPostTweet(bool success) | |
{ | |
print("OnPostTweet - " + (success ? "succedded." : "failed.")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment