加载画布Facebook.dll没有Unity Integration(或其他桥接选项)



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

有关设置的信息:

  • 统一 4.6.1
  • Unity Facebook SDK v6.1.0
  • 我正在使用OS X进行构建。在野生动物园中测试游戏。

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

使用"是"以使用 Facebook Unity SDK

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

我面临的第一个问题是缺少CanvasFacebook.dllAuthToken.unityhash文件。我通过从这个链接(dll)和这个(unityhash)下载它们来解决这个问题。并将它们放在我服务器上的适当路径(rsrc/unity/lib/sdk_6.1/CanvasFacebook.dllrsrc/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 Integration时,此代码有效。但是当我禁用 Unity 集成并在 facebook 内运行应用程序时 - 提到的代码执行并失败(没有任何错误或消息)。我怎么知道它失败了?好吧,它没有更进一步。简单检查 - 在var fb = ... ;后立即添加Debug.Log("Got/Added CanvasFacebook component");,并查看未调用Log方法。顺便说一句,我有一个日志处理程序可以调用类似 Application.ExternalCall("console.log", message) 的东西,但有一些漂亮的东西。

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

var assembly = 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;

呼叫有效,但fb null.所以我在 sdk IFacebook.dll上使用ILSpy检查FBComponentFactory类。这是类定义:

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

所以现在我认为问题在于添加组件。但它不会抛出任何错误(好吧,我无法使用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());不会执行。没有错误,没有例外。执行器(或它如何调用)只是从此方法中移出并继续。我可以玩我的游戏,但脸书插件不起作用。

再说一遍。以前的代码不会引发任何异常(至少,我无法使用 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.

问题

对不起这么长的帖子。但这是我的问题:

  • 有谁知道这里到底发生了什么?
  • CanvasFacebook dll 是否有可能损坏?
  • 我找不到有关AddComponent这种行为的任何信息。有没有人经历过类似的事情?

主要问题:

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

提前谢谢。

这个问题相当古老。但是由于Facebook插件的第7.0.0版描述的流程是可能的。

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

最新更新