我试图发布截图并将其上传到用户的Facebook帐户。我正在使用Unity(c#)并使用facebook SDK,它可以正常使用我们从他们的Docs获得的示例代码。
我甚至设法"预先填写"一条消息并用截图发布。但是预先填写邮件是针对Facebook Platform Policy exanples的,因此我正在寻找一种方法来使用相同的方法,让用户在我不打破时填写邮件Facebook平台政策。
这是我使用的工作代码:
private IEnumerator TakeScreenshot()
{
yield return new WaitForEndOfFrame();
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","some message"); // violating Facebook Platform Policy
FB.API("me/photos", Facebook.HttpMethod.POST, Callback, wwwForm);
}
答案 0 :(得分:1)
这是InputField
和Button
组件发挥作用的地方。获取InputField
的用户消息,并在单击按钮时发送。您可以在截取屏幕截图之前隐藏InputField
和发送Button
。
public InputField userInput;
public Button postButton;
public void OnEnable()
{
postButton.onClick.AddListener(postToFB);
}
void postToFB()
{
Debug.Log("Posting To FB");
StartCoroutine(TakeScreenshot(userInput.text));
}
private IEnumerator TakeScreenshot(string textToPost)
{
//Hide User Input
userInput.gameObject.SetActive(false);
//Hide Send Button
postButton.gameObject.SetActive(false);
yield return new WaitForEndOfFrame();
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", textToPost);
FB.API("me/photos", Facebook.HttpMethod.POST, Callback, wwwForm);
}
public void OnDisable()
{
postButton.onClick.RemoveAllListeners();
}