I managed to incorporate the Facebook API into my project successfully, meaning, my game can take screenshots and post them into Facebook when clicking a button. However, this only works when running the application within the editor; touching that same button on my android device crashes the application instead (Unfortunately, appName has stopped).
I have been looking for a solution to this issue two days now, and while I came across a lot of different fixes, nothing seems to work; I already tried generating the correct hash key (with several different approaches), the apparently famous Rafael Fix, I tried fixing the screen orientation issue via AndroidManifest.xml, and I tried commenting the ScreenShot posting code (to make sure that Login is at fault).
I haven’t made the app public on facebook, or uploaded to game to the play store yet (since it isnt ready for release yet anyways), but I don’t think that should cause the game to crash like this.
I have a couple of days off my mandatory army service, and I really wanted to get this up and running before I had to go back to base. But, I am at my wit’s end.
I’d really appreciate it if anyone could help me out with this.
Relevant Code:
void Awake() {
FB.Init(SetInit, OnHideUnity);
}
private void SetInit() {
print("FB: Init");
if (FB.IsLoggedIn) print("FB: logged in");
else print("FB: Not logged in");
}
public void FBLogin() {
FB.LogInWithPublishPermissions(new List<string>() { "email", "publish_actions" }, AuthCallback);
}
void AuthCallback(ILoginResult result) {
if (FB.IsLoggedIn) {
print("FB: Login success");
StartCoroutine(TakeScreenshot());
}
else print("FB: Login failure");
}
public void ShareScore() {
if (!FB.IsLoggedIn) FBLogin();
}
private IEnumerator TakeScreenshot() {
yield return new WaitForEndOfFrame();
//print("W : " + Screen.width + " H : " + Screen.height);
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] screenshot = tex.EncodeToPNG();
var wwwForm = new WWWForm();
wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("me/photos", HttpMethod.POST, ShareCallback, wwwForm);
}
private void ShareCallback(IResult result) {
if (result == null) {
print("no result");
return;
}
// Some platforms return the empty string instead of null.
if (!string.IsNullOrEmpty(result.Error)) print(result.ToString());
else if (result.Cancelled) print("Cancelled");
else if (!string.IsNullOrEmpty(result.RawResult)) print("Sucess, check log");
else print("empty");
}
private void OnHideUnity(bool isGameShown) {
print("FB: Hide Unity : " + !isGameShown);
/* if (!isGameShown) Time.timeScale = 0;
else Time.timeScale = 1;*/
}