从unity上传图片到facebook [英] Upload picture to facebook from unity

查看:55
本文介绍了从unity上传图片到facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款 Unity 游戏,您可以在其中拍照并将这张图片连同一些标签和内容(很像 Friendsmash)一起从 unity 上传到 Facebook.问题是我没有可以放置屏幕截图的网络服务器,而且 Fb.Feeb(picture:) 属性只接受 url.

Im working on a unity game where you can take a picture and upload this picture to facebook from unity along with some tags and stuff (much like friendsmash). The problem is that i do not have a web-server that i can put the screenshots on, and the Fb.Feeb(picture:) attribute only accepts urls.

我已经读到您可以使用 HTTP POST 将图片发布到用户图片,然后在图片中使用该链接:,但我对 HTTP POST 一无所知,也无法弄清楚如何去做.

I have read that you can use HTTP POST to post the picture to the users images and then use that link in picture:, but i dont know anything about HTTP POST and i couldnt figure out how to do it.

我还读到您可以使用 FB.API() 以某种方式执行此操作,但我无法弄清楚.

I have also read that you can use FB.API() to somehow do this, but i couldnt figure it out.

任何示例代码将不胜感激.

Any sample code would be greatly appreciated.

我当前的代码:

private string _path = "file://" + System.IO.Path.Combine(Application.persistentDataPath, "Images/image.png");

void Start ()
    {
        if (!FB.IsLoggedIn)
            FB.Login("email, publish_actions, publish_stream, user_photos", LoginCallback);
        StartCamera();
    }


private void OnBragClicked()

{
    FbDebug.Log("OnBragClicked");

//Post(); <-- dont know how

FB.Feed(
    linkCaption: "#hashtag",
    picture: "???",
    linkName: "Im hashtaging!",
    link: "https://apps.facebook.com/" + FB.AppId + "/?challenge_brag=" + (FB.IsLoggedIn ?  FB.UserId : "guest")
    );
}


 void TakeSnapshot()

{
    _snap = new Texture2D(_webCamTexture.width, _webCamTexture.height);
    _snap.SetPixels(_webCamTexture.GetPixels());
    _snap.Apply();

//System.IO.File.WriteAllBytes(_path, _snap.EncodeToPNG());
}

推荐答案

毕竟 facebook sdk 确实有办法让这一切发生.您需要使用 Fb.API().这就是它对我有用的方式:

The facebook sdk does have a way to make this happen after all. You need to use Fb.API(). This is the way it worked for me:

private void TakeScreenshot()
{
    var snap = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    snap.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    snap.Apply();
    var screenshot = snap.EncodeToPNG();

    var wwwForm = new WWWForm();
    wwwForm.AddBinaryData("image", screenshot, "barcrawling.png");

    FB.API("me/photos", HttpMethod.POST, LogCallback, wwwForm);
}

一般来说,要添加某种标题,大致是这样...

In general terms, to add some sort of caption, it's along the lines...

private byte[] postcardAsBytes;
string textMessage = "Play this great game at http://blah.com.";
Dictionary<string, object> d = new Dictionary<string, object>
  {
  { "message", textMessage },
  { "picture", postcardAsBytes }
  };
Facebook.instance.graphRequest(
        "me/photos", HTTPVerb.POST, d, yourCompletionHandler);
// in this example using the prime31 plugin, rather than the fb sdk

关键字段似乎是消息".令人困惑的是,FB 记录的字典"字段似乎不起作用,因此首先尝试消息".

The key field seems to be "message". Confusingly, the "dictionary" field documented by FB, seems to just not work, so try "message" at first.

这篇关于从unity上传图片到facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆