1

我正在开发希望在 Facebook 上发布的 Unity3d 游戏。

有关设置的信息:

如您所知,Facebook Canvas 提供了使用 Unity 集成的选项。此选项的说明是:

“是”以使用 Facebook Unity SDK

但我希望在加载统一播放器之前显示自定义画布页面。所以这就是为什么我需要停止使用 Facebook Canvas 的 Unity 集成。所以目前我有一个服务器来服务我的应用程序。除统一插件外,一切正常。

我面临的第一个问题是缺少CanvasFacebook.dllAuthToken.unityhash文件。我通过从这个链接(dll)和这个链接(unityhash)下载它们来解决这个问题。并将它们放在我的服务器上的适当路径(rsrc/unity/lib/sdk_6.1/CanvasFacebook.dll和)中。rsrc/unity/key/sdk_6.1/AuthToken.unityhash所以现在FB.cs加载这个dll没有问题。

但现在我又遇到了一个问题。里面有FB.cs一段代码:

var fb = typeof(FBComponentFactory)
         .GetMethod("GetComponent")
         .MakeGenericMethod(facebookClass)
         .Invoke(null, new object[] { IfNotExist.AddNew }) as IFacebook;

当我在 Facebook Canvas 中使用 Unity 集成时,此代码有效。但是当我禁用 Unity 集成并在 facebook 内运行应用程序时 - 提到的代码执行并失败(没有任何错误或消息)。我怎么知道它失败了?好吧,它不会更进一步。简单检查 -Debug.Log("Got/Added CanvasFacebook component");之后添加var fb = ... ;并查看Log未调用该方法。顺便说一句,我有一个日志处理程序,它调用类似的东西Application.ExternalCall("console.log", message),但有一些漂亮的东西。

好的,所以我尝试检查为什么此代码不适用于自定义画布。为什么我认为 dll 已加载并且应该可以工作?因为紧接着

var 程序集 = Security.LoadAndVerifyAssembly(www.bytes, authTokenWww.text); ... var facebookClass = assembly.GetType(facebookNamespace + className);

facebookClass不为空且等于CanvasFacebook。更重要的是,我发现

var methodInfo = typeof(FBComponentFactory)
                 .GetMethod("GetComponent")
                 .MakeGenericMethod(facebookClass);

也有效。这只是调用此方法的问题。当我尝试

var fb = methodInfo.Invoke(null, new object[] { IfNotExist.ReturnNull }) as IFacebook;

呼叫有效,fbnull. 所以我使用来自 sdk的 on检查了FBComponentFactory类。这是类定义:ILSpyIFacebook.dll

using System;
using UnityEngine;
namespace Facebook
{
    public class FBComponentFactory
    {
        public const string gameObjectName = "UnityFacebookSDKPlugin";
        private static GameObject facebookGameObject;
        private static GameObject FacebookGameObject
        {
            get
            {
                if (FBComponentFactory.facebookGameObject == null)
                {
                    FBComponentFactory.facebookGameObject = new GameObject("UnityFacebookSDKPlugin");
                }
                return FBComponentFactory.facebookGameObject;
            }
        }
        public static T GetComponent<T>(IfNotExist ifNotExist = IfNotExist.AddNew) where T : MonoBehaviour
        {
            GameObject gameObject = FBComponentFactory.FacebookGameObject;
            T t = gameObject.GetComponent<T>();
            if (t == null && ifNotExist == IfNotExist.AddNew)
            {
                t = gameObject.AddComponent<T>();
            }
            return t;
        }
        public static T AddComponent<T>() where T : MonoBehaviour
        {
            return FBComponentFactory.FacebookGameObject.AddComponent<T>();
        }
    }
}

所以现在我认为问题在于添加组件。但它不会抛出任何错误(好吧,我无法捕捉到任何 using try catch)。所以我试图从FB.cs显式添加组件(我知道这是丑陋的黑客,但我只是想了解问题的根源):

var type = typeof(FBComponentFactory);
Debug.LogError("I got type: " + type);
var fieldInfo = type.GetField("facebookGameObject", BindingFlags.NonPublic | BindingFlags.Static);
Debug.LogError("I got fieldInfo: " + fieldInfo);
var go = fieldInfo.GetValue(null) as GameObject;

if (go == null) {
    Debug.LogError("go is null, so creating new one");
    fieldInfo.SetValue(null, new GameObject("UnityFacebookSDKPlugin"));
    go = fieldInfo.GetValue(null) as GameObject;
}

Debug.LogError("I got go with name: " + (go == null ? "null" : go.name));
var component = go.AddComponent(facebookClass);
Debug.LogError("got component of type: " + component.GetType());
var fb = component is IFacebook;

我使用日志错误,因为它们会调用console.error并且在浏览器日志中更容易看到。

因此,当我使用这些更改构建游戏并在 facebook 画布内运行时,我会看到下一个输出:

[Error] 7.24    I got type: Facebook.FBComponentFactory
[Error] 7.24    I got fieldInfo: UnityEngine.GameObject facebookGameObject
[Error] 7.24    I got go with name: UnityFacebookSDKPlugin

但是代码Debug.LogError("got component of type: " + component.GetType());没有被执行。没有错误,没有例外。Executer(或者它是如何调用的)只是从这个方法中移出并继续。我可以玩我的游戏,但 facebook 插件不起作用。

然后再次。以前的代码不会抛出任何异常(至少,我无法使用 捕获它们try catch)。

另外,当我检查Player.log文件时,我看到:

7.24    I got type: Facebook.FBComponentFactory
7.24    I got fieldInfo: UnityEngine.GameObject facebookGameObject
7.24    I got go: UnityFacebookSDKPlugin
7.24    got component of type: Facebook.CanvasFacebook

所以它表明一切都很好。但实际上,插件没有加载。当我尝试使用时,FB.Login我看到:

180.49  Facebook: tried to login but we're not init'd yet.

问题)

抱歉这么长的帖子。但这是我的问题:

  • 有谁知道这里到底发生了什么?
  • dll是否可能CanvasFacebook已损坏?
  • 我找不到有关AddComponent. 有没有人经历过类似的事情?

主要问题:

是否可以使用 Facebook Unity SDK自定义画布?我知道我可以在 JS SDK 和 Unity SDK 之间创建自己的桥梁,但我不喜欢这个想法,因为我知道有人已经创建了它并且唯一的问题 - 创建CanvasFacebook组件。

提前致谢。

4

1 回答 1

0

这个问题比较老了。但由于7.0.0facebook 插件版本描述的流程是可能的。

有关更多信息,请查看官方文档

于 2015-10-07T14:13:15.410 回答