如何修复 Unity 4.6 中的程序集安全错误

How to fix assembly security error in Unity 4.6

我在尝试使用 Facebook Unity SDK 时遇到错误:

Could not securely load assembly from https://integrated-plugin-canvas-rsrc.fbsbx.com/rsrc/unity/lib/sdk_4.0/CanvasFacebook.dll UnityEngine.Debug:LogError(Object) FbDebug:Error(String) c__Iterator3:MoveNext() (at Assets/Ultimate GUI Kit/Facebook/Scripts/FB.cs:337)

我知道通常您只需将 if unity 4.6 添加到 FB.cs。但是在我开始使用 class 的模板中没有这一行。那么我该如何解决呢?这是我的 FB.cs 文件中 RemoteFacebookLoader class 的定义:

public abstract class RemoteFacebookLoader: MonoBehaviour {
    public delegate void LoadedDllCallback(IFacebook fb);

    private const string facebookNamespace = "Facebook.";

    private const int maxRetryLoadCount = 3;
    private static int retryLoadCount = 0;

    public static IEnumerator LoadFacebookClass(string className, LoadedDllCallback callback) {
#if UNITY_EDITOR || UNITY_WEBPLAYER
        var url = string.Format(IntegratedPluginCanvasLocation.DllUrl, className);
        var www = new WWW(url);
        FbDebug.Log("loading dll: " + url);
        yield
        return www;

        if (www.error != null) {
            FbDebug.Error(www.error);
            if (retryLoadCount < maxRetryLoadCount) {
                ++retryLoadCount;
#else 
                var www = new WWW("");
                yield
                return www;#endif#
                if UNITY_WEBPLAYER FBComponentFactory.AddComponent < CanvasFacebookLoader > ();#endif#
                if UNITY_EDITOR || UNITY_WEBPLAYER
            }
            www.Dispose();
            yield
            break;
        }

        var assembly = Security.LoadAndVerifyAssembly(www.bytes, );
        if (assembly == null) {
            FbDebug.Error("Could not securely load assembly from " + url);
            www.Dispose();
            yield
            break;
        }

        var facebookClass = assembly.GetType(facebookNamespace + className);
        if (facebookClass == null) {
            FbDebug.Error(className + " not found in assembly!");
            www.Dispose();
            yield
            break;
        }

        // load the Facebook component into the gameobject
        // using the "as" cast so it'll null if it fails to cast, instead of exception
        var fb = typeof(FBComponentFactory)
            .GetMethod("GetComponent")
            .MakeGenericMethod(facebookClass)
            .Invoke(null, new object[] {
            IfNotExist.AddNew
        }) as IFacebook;

        if (fb == null) {
            FbDebug.Error(className + " couldn't be created.");
            www.Dispose();
            yield
            break;
        }

        callback(fb);
        www.Dispose();

#endif
    }

    protected abstract string className {
        get;
    }

    IEnumerator Start() {
        var loader = LoadFacebookClass(className, OnDllLoaded);
        while (loader.MoveNext()) {
            yield
            return loader.Current;
        }
        Destroy(this);
    }

    private void OnDllLoaded(IFacebook fb) {
        FB.facebook = fb;
        FB.OnDllLoaded();
    }
}

更新

看来我使用的是旧版 Facebook SDK (v4.0)。所以我更新了 FB.cs 文件,现在我又遇到了另一个错误。他们说了一些关于 OGACTIONTYPE 的事情,并且 unity 无法从 IFacebook 程序集加载这种类型。查看统一控制台的screenshot

实际上它说:

Assets/Ultimate GUI Kit/Facebook/Scripts/FB.cs(212,13): error CS0246: The type or namespace name `OGActionType' could not be found. Are you missing a using directive or an assembly reference?

这是我刚刚加载的 FB.cs 文件中 RemoteFacebookLoader class 的定义:

public abstract class RemoteFacebookLoader : MonoBehaviour
{
    public delegate void LoadedDllCallback(IFacebook fb);

    private const string facebookNamespace = "Facebook.";

    private const int maxRetryLoadCount = 3;
    private static int retryLoadCount = 0;

    public static IEnumerator LoadFacebookClass(string className, LoadedDllCallback callback)
    {
        var url = string.Format(IntegratedPluginCanvasLocation.DllUrl, className);
        var www = new WWW(url);
        FbDebug.Log("loading dll: " + url);
        yield return www;

        if (www.error != null)
        {
            FbDebug.Error(www.error);
            if (retryLoadCount < maxRetryLoadCount)
            {
                ++retryLoadCount;
#if UNITY_WEBPLAYER
                FBComponentFactory.AddComponent<CanvasFacebookLoader>();
#endif
            }
            www.Dispose();
            yield break;
        }

#if !UNITY_WINRT
#if UNITY_4_5 || UNITY_4_6 || UNITY_5_0
        var authTokenWww = new WWW(IntegratedPluginCanvasLocation.KeyUrl);
        yield return authTokenWww;
        if (authTokenWww.error != null)
        {
            FbDebug.Error("Cannot load from " + IntegratedPluginCanvasLocation.KeyUrl + ": " + authTokenWww.error);
            authTokenWww.Dispose();
            yield break;
        }
        var assembly = Security.LoadAndVerifyAssembly(www.bytes, authTokenWww.text);
#else
        var assembly = Security.LoadAndVerifyAssembly(www.bytes);
#endif
        if (assembly == null)
        {
            FbDebug.Error("Could not securely load assembly from " + url);
            www.Dispose();
            yield break;
        }

        var facebookClass = assembly.GetType(facebookNamespace + className);
        if (facebookClass == null)
        {
            FbDebug.Error(className + " not found in assembly!");
            www.Dispose();
            yield break;
        }

        // load the Facebook component into the gameobject
        // using the "as" cast so it'll null if it fails to cast, instead of exception
        var fb = typeof(FBComponentFactory)
                .GetMethod("GetComponent")
                .MakeGenericMethod(facebookClass)
                .Invoke(null, new object[] { IfNotExist.AddNew }) as IFacebook;

        if (fb == null)
        {
            FbDebug.Error(className + " couldn't be created.");
            www.Dispose();
            yield break;
        }

        callback(fb);
#endif
        www.Dispose();
    }

    protected abstract string className { get; }

    IEnumerator Start()
    {
        var loader = LoadFacebookClass(className, OnDllLoaded);
        while (loader.MoveNext())
        {
            yield return loader.Current;
        }
        Destroy(this);
    }

    private void OnDllLoaded(IFacebook fb)
    {
        FB.facebook = fb;
        FB.OnDllLoaded();
    }
}

看起来你的 FB.cs 已经过时了。对于 Unity 4.6,您应该使用最新版本的 Facebook Unity SDK。您可以在 official page.

下载

另一个可能的问题 - 您修改了这个文件。除非你真的知道你在做什么,否则你不应该这样做。

回到你的问题。

var assembly = Security.LoadAndVerifyAssembly(www.bytes,);
                                                       ^

此代码已损坏。 , 符号在 www.bytes 之后做什么?

同样对于 Unity 4.6,它应该在尝试加载程序集之前下载 unityhash 文件。所以实际上这个调用应该是这样的:

var assembly = Security.LoadAndVerifyAssembly(www.bytes, hashValue);

在我的 Unity SDK 版本中,我有类似的东西

#if UNITY_4_5 || UNITY_4_6 || UNITY_5_0
var authTokenWww = new WWW(IntegratedPluginCanvasLocation.KeyUrl);
yield return authTokenWww;
if (authTokenWww.error != null)
{
    Debug.LogError("Cannot load from " + IntegratedPluginCanvasLocation.KeyUrl + ": " + authTokenWww.error);
    authTokenWww.Dispose();
    yield break;
}
var assembly = Security.LoadAndVerifyAssembly(www.bytes, authTokenWww.text);
#else
var assembly = Security.LoadAndVerifyAssembly(www.bytes);
#endif

更新

我们在聊天中稍微讨论了一个问题后,我们找到了现有问题的解决方案。我正在为那些可能 运行 处于类似情况的人更新我的问题。

所以为了解决OP所做的问题:

  • 删除了 所有 与 facebook unity sdk 相关的文件(我的意思是,所有,不仅仅是 FB.cs,而是整个 Facebook文件夹!)
  • 下载新版sdk(会是unity资源包)并导入到项目中
  • 配置 facebook 插件(例如在 facebook 开发者页面上的 facebook 应用程序设置中使用正确的应用程序 ID)