【问题标题】:Unity Facebook Canvas inject background imageUnity Facebook Canvas 注入背景图片
【发布时间】:2014-05-30 20:42:47
【问题描述】:

我的游戏是用统一制作的,并且集成了 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:对不起我的英语,它太低了:)

【问题讨论】:

    标签: c# javascript html facebook unity3d


    【解决方案1】:

    我知道这已经存在了大约 3 个月,但我偶然发现了一个类似的问题,这篇文章帮助我获得了一些关于如何解决它的想法。这是我更改统一游戏的 Facebook 画布页面的解决方案:

    Facebook 应用配置页面:

    -Unity 集成:是的

    -固定宽度:否

    -固定高度:否

    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());
        }
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      相关资源
      最近更新 更多