【问题标题】:MD5 GetHash work only in Unity Editor?MD5 GetHash 只能在 Unity 编辑器中工作?
【发布时间】:2015-05-05 14:08:53
【问题描述】:

我正在制作一个 Android 游戏,有代码可以将高分信息发送到我的 Web 的 PHP 代码。

奇怪的是,这在 unity-editor 中运行良好,但在我制作 .apk 并在真正的 android 手机上运行后,它不起作用并出现错误。

错误如下(Eclipse logcat 已知)

05-05 22:56:47.997: I/Unity(20561): NullReferenceException: 对象 引用未设置为对象 05-05 22:56:47.997 的实例: I/Unity(20561):在 HighScore.GetHash (System.String usedString) [0x00000] in :0 05-05 22:56:47.997: I/Unity(20561): 在 HighScore+c__Iterator14.MoveNext () [0x00000] 中 :0

代码是,首次运行高分时注册昵称的功能。

 public IEnumerator RegisterName(string name)
{ 
    string finalResult = "";
    string tables = "";
    for (int m = 0; m < difficultyModes.Length; m++)
    {//Create a list of tables to send 
        if (m < difficultyModes.Length - 1)
        {
            tables += difficultyModes[m].databaseName + " ";
        }
        else
        {
            tables += difficultyModes[m].databaseName;
        }
    } 
    WWWForm rsFm = new WWWForm();
    rsFm.AddField("name", name);
    rsFm.AddField("tables", tables);
    rsFm.AddField("hash", GetHash(name));
    WWW rs = new WWW(registerUserURL, rsFm);
    yield return rs;
    Debug.Log(rs.text + " : " + difficultyModes[modeIndex].displayName);
    finalResult = rs.text;
    if (finalResult.Equals("Already Used"))
    {
        runningHsServer = 0;
        status = "Name Already Used";
        TitleScript.Instance.PopupMsgWithNoBack("Name is already taken.", true);
        yield break;
    }
    else if (finalResult.Equals("Registration Complete"))
    {//We Registered Now Update Score
        PlayerPrefs.SetInt("nameRegistered", 1);
        PlayerPrefs.SetString("registeredName", name);
        TitleScript.Instance.PopupMsgWithNoBack("Name registered!", true);
        TitleScript.Instance.PopupNo();
        NewUI.Instance.myNick.text = name;
        myNickName = name;
        NewUI.Instance.OpenHigh();
    }
    else
    {
        runningHsServer = 0;
        status = finalResult; yield break;
    }

}

和 GetHash 部分。 (这里会出现NullReference错误)

string GetHash(string usedString)
{ //Create a Hash to send to server
    MD5 md5 = MD5.Create();
    //byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey);     // this wrong because can't receive korean character
    byte[] bytes = Encoding.UTF8.GetBytes(usedString + secretKey);
    byte[] hash = md5.ComputeHash(bytes);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();
}

【问题讨论】:

  • 您是否尝试过在GetHash 方法中添加空检查?

标签: c# android unity3d md5


【解决方案1】:

使用MD5CryptoServiceProvider 而不是MD5.Create()

var md5 = new MD5CryptoServiceProvider();

当剥离级别设置为 Micro mscorlib 时,MD5.Create() 不会在 Unity Android 上返回对象,但“new MD5CryptoServiceProvider()”会。

【讨论】:

    【解决方案2】:

    所以这是因为统一构建设置的剥离级别设置。

    最初我将大多数剥离设置(使用 micro mscorlib)设置为更小的 .apk 文件大小,但这会阻止一些代码功能。

    所以我把它设置回禁用,上面的功能在安卓手机上也可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多