【问题标题】:How to fix assembly security error in Unity 4.6如何修复 Unity 4.6 中的程序集安全错误
【发布时间】:2015-03-30 00:33:37
【问题描述】:

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

无法从 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() (在资产/终极 GUI Kit/Facebook/Scripts/FB.cs:337)

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

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)​​:错误 CS0246: 找不到类型或命名空间名称“OGActionType”。你是 缺少 using 指令或程序集引用?

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

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

【问题讨论】:

  • 已解决,感谢 d12frosted

标签: c# facebook unity3d


【解决方案1】:

您的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(它将是一个统一的资产包)并将其导入到项目中
  • 配置 facebook 插件(例如在 facebook 开发者页面上的 facebook 应用设置中使用正确的应用 ID)

【讨论】:

  • 感谢您的回复,我导入了一个集成了 facebook 的模板,就是这样......我尝试再次导入 facebook,但随后我收到有关 OGactiontype 的错误。“资产/终极 GUI 工具包/ Facebook/Scripts/FB.cs(212,13)​​:错误 CS0246:找不到类型或命名空间名称“OGActionType”。您是否缺少 using 指令或程序集引用?”哦,逗号在那里,因为如果 Unity4.6 部分没有更改,我尝试使用在此站点上的另一个答案中找到的密钥来更改它,但该密钥很可能已过时......
  • 我链接了我的新 FB.cs,现在我收到有关 OGACTIONTYPE 的错误。但我尝试自己导入 FB.cs...我将尝试再次导入所有 facebook。编辑:在将 facebook 导入到最新版本后,我遇到了一堆错误。 gyazo.com/46e5ffd57a6b038140f7857f4033132c
  • 见上面我忘了给你加标签
  • 我有一个问题要问你。您是否刚刚更新了 FB.cs 文件或整个资产包?因为看起来 IFacebook.dll 缺少某些类型。
  • 很可能您只需要删除旧的 Facebook 文件夹并导入新版本的资产(检查所有文件)
猜你喜欢
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 2018-10-17
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多