You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace uc
{
//https://developers.facebook.com/docs/graph-api/reference/page/photos/
public class FBFanPagePhoto : MonoBehaviour
{
public Texture photo;
public string message;
public bool noStory = false;
[Header("Settings")]
[Range(1, 5)]
public int downScaleRatio = 1;
public WWWForm GetForm()
{
WWWForm form = new WWWForm();
if (message != "")
form.AddField("message", message);
if (photo != null)
{
byte[] bytes = ProcessPhoto(photo);
form.AddBinaryData("source", bytes, "screenShot.png", "image/png");
}
if(noStory)
form.AddField("no_story", "true");
return form;
}
byte[] ProcessPhoto(Texture tex)
{
try
{
if (downScaleRatio != 1 || (tex as Texture2D) == null)
return BlitPhoto(tex).EncodeToPNG();
else
return (tex as Texture2D).EncodeToPNG();
}
catch (UnityException)
{
return BlitPhoto(tex).EncodeToPNG();
}
}
Texture2D BlitPhoto(Texture tex)
{
RenderTexture temp = RenderTexture.GetTemporary(photo.width / downScaleRatio, photo.height / downScaleRatio, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(photo, temp);
RenderTexture current = RenderTexture.active;
RenderTexture.active = temp;
Texture2D newTex = new Texture2D(temp.width, temp.height, TextureFormat.RGB24, false);
newTex.ReadPixels(new Rect(0, 0, temp.width, temp.height), 0, 0);
newTex.Apply();
RenderTexture.active = current;
return newTex;
}
}
}