[英]Facebook SDK FB.API post screenshot with user's message
I'm trying to post a screenshot and upload it to the user's facebook account. 我正在尝试发布屏幕截图并将其上传到用户的Facebook帐户。 I'm working with unity (c#) and using facebook SDK and it's working fine with the example code i got from their Docs. 我正在使用unity(c#)并使用facebook SDK,并且我从他们的文档中获得的示例代码可以正常工作。
I even managed to "pre-fill" a message and post it with the screenshot. 我什至设法“预填充”一条消息,并将其与屏幕截图一起发布。 But pre-filling a message is against Facebook Platform Policy exanples , so i'm looking for a way to use the same method and let the user fill in a message while i don't break Facebook Platform Policy. 但是,预先填写消息与Facebook Platform Policy 极端做法不符 ,因此,我在寻找一种使用相同方法的方法,并在我不违反Facebook Platform Policy的情况下让用户填写消息。
here is the working code i used: 这是我使用的工作代码:
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);
}
This is where InputField
and Button
components comes into play. 这是InputField
和Button
组件起作用的地方。 Get user message with InputField
and send when Button is clicked. 使用InputField
获取用户消息,并在单击Button时发送。 You can hide both the InputField
and send Button
before taking screenshot. 您可以在截屏之前隐藏InputField
和send 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.