Unity Facebook Canvas注入背景图像

时间:2014-05-30 20:42:47

标签: c# javascript html facebook unity3d

我的团结和facebook集成我的游戏。所有工作都很好,但我不能改变背景图像,我不知道任何关于HTML :)。我可以使用chrome中的开发人员工具来制作,但我不能注入代码。 我有这个代码。

private void ConfigureCanvas(){
        Debug.Log("CONFIGURE FACEBOOK CANVAS ");
        // INJECT TOP ADS

        string injectionAdsFacebook =
            "var headerElement = document.createElement('div'); " +
                "headerElement.id = ('adsWebFacebook')  ; " +
                "headerElement.textContent = ('Check out our other great games: ...');" +
                "headerElement.style.width = 400; " +
                "headerElement.style.height = 200; " +
                "headerElement.style.marginLeft = (window.innerWidth - 400)/2;" +
                "headerElement.style.marginRight = (window.innerWidth - 400)/2;" +
                "var body = document.getElementById('unityPlayerEmbed');" +
                "var insertionPoint = body.children[0]; " +
                "body.insertBefore(headerElement, insertionPoint);";

        Application.ExternalEval(injectionAdsFacebook);

        // Execute javascript in iframe to keep the player centered and recalculate resolution
        string javaScript = @"
                    window.onresize = function() {
                    var width = window.innerWidth;
                    var height = window.innerHeight;

                    var marginX = width * 0.1;
                    var marginY = height *0.1;

                    var appHeight = (( window.innerHeight - marginY) / 500) * 500;
                    var appWidth = (appHeight *16) /10; 

                    if(appWidth > window.innerWidth){
                        appWidth = (( window.innerWidth - marginX) / 800) * 800;
                        appHeight = (appWidth *10) /16; 

                    }

                    var unity = UnityObject2.instances[0].getUnity();
                    var unityDiv = document.getElementById(""unityPlayerEmbed"");

                    unity.style.width = appWidth + ""px"";
                    unity.style.height = appHeight + ""px"";

                    unityDiv.style.marginLeft = (width - appWidth)/2 + ""px"";
                    unityDiv.style.marginTop = (height - appHeight)/2 + ""px"";
                    unityDiv.style.marginRight = (width - appWidth)/2 + ""px"" ;
                    unityDiv.style.marginBottom = (height - appHeight)/2 + ""px"";
                }

                window.onresize(); // force it to resize now";
        Application.ExternalCall(javaScript);



        string changeCanvasBackground = 

                "var body = document.getElementsByTagName('body');" +
                "body[0].background-image =  \"url('https://tictacpuzzlesfb.parseapp.com/backgroundsFaceCanvas/spaceCanvasBack.png')\";" +
                "body[0].background-size = 'cover'";

        Application.ExternalEval(changeCanvasBackground);
    }

好吧,如果有任何人可以给我一个解决方案,我认为对于一个html程序员来说这很容易,如果我可以想用ExternalEval(changaCanvasBackground),每次我得到一个错误的错误,我尝试很多选项,并得到相同的错误

P.d:对不起我的英语,它相当低:)

1 个答案:

答案 0 :(得分:0)

我知道现在已经有3个月了,但我遇到了类似的问题,这篇文章帮助我了解了如何解决这个问题。这是我改变统一游戏的Facebook画布页面的解决方案:

Facebook应用配置页

-Unity Integration:YES

- 固定宽度:否

- 固定高度:否

Unity编辑配置:

- 设置所需的网络播放器分辨率(我的是760 x 800)

- 在任何脚本中从Start()调用它:

private void changeCanvasBackground()
    {
        string script = "document.getElementsByTagName('body')[0].style.backgroundImage=\"url('https://YOURSECUREURL.COM/background.jpg')\";";

        Application.ExternalEval(script);

        //This FB.Canvas.SetResolution should set to the W and H that is set on your build preferences in unity editor.
        FB.Canvas.SetResolution(760, 800, false, 0, FBScreen.CenterHorizontal(), FBScreen.CenterVertical());
    }

感谢。