Created
February 21, 2013 21:36
-
-
Save gashtio/5008473 to your computer and use it in GitHub Desktop.
Component that handles signals received upon mech death
This file contains 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 Coherent.UI; | |
using Coherent.UI.Binding; | |
using System.IO; | |
public class SignalReceiver : MonoBehaviour { | |
private CoherentUIView m_View; | |
private string m_LocalAppURL; | |
private const string FacebookAppURL = "http://www.coherent-labs.com/sample.html"; | |
// Use this for initialization | |
void Start () { | |
m_View = GetComponent<CoherentUIView>(); | |
m_LocalAppURL = m_View.Page; | |
if (m_View != null && m_View.Listener != null) | |
{ | |
m_View.OnViewCreated += (view) => { view.InterceptURLRequests(true); }; | |
m_View.Listener.URLRequest += OnURLRequestHandler; | |
} | |
} | |
void OnURLRequestHandler (string url, URLResponse response) | |
{ | |
if (url.StartsWith(FacebookAppURL)) | |
{ | |
// change the url, keeping all parameters intact | |
string redirectURL = m_LocalAppURL + url.Substring(FacebookAppURL.Length); | |
response.RedirectRequest(redirectURL); | |
return; | |
} | |
response.AllowRequest(); | |
} | |
void OnEnemyMechDeath() { | |
if (m_View != null && m_View.View != null) | |
{ | |
m_View.View.TriggerEvent("ShowAchievementPopup"); | |
// Uncomment the next line if you want a wall post along with the photo | |
//m_View.View.TriggerEvent("PostOnWall", "If you can see this, it's coming from Unity3D and apparently it's working!"); | |
SaveScreenShotAsync(Application.dataPath + "/mechdeath.png"); | |
} | |
} | |
void SaveScreenShotAsync(string filename) { | |
StartCoroutine(ScreenshotEncode(filename)); | |
} | |
void SavePNGBytes(string filename, byte[] bytes) { | |
// Save to disk code path | |
//File.WriteAllBytes(filename, bytes); | |
// Upload to FaceBook | |
if (m_View != null && m_View.View != null) | |
{ | |
m_View.View.TriggerEvent("UploadImageOnFacebook", bytes, "Victory for mankind! The evil mech was destroyed!"); | |
} | |
} | |
IEnumerator ScreenshotEncode(string filename) | |
{ | |
// wait for graphics to render | |
yield return new WaitForEndOfFrame(); | |
// create a texture to pass to encoding | |
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); | |
// put buffer into texture | |
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); | |
texture.Apply(); | |
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy | |
yield return 0; | |
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots. | |
DestroyObject( texture ); | |
byte[] bytes = texture.EncodeToPNG(); | |
SavePNGBytes(filename, bytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment