c# - Unity3D iOS 64 位的 IL2CPP 编译器运行时错误

标签 c# ios unity-game-engine mono chess

我有一个 Unity3D 移动国际象棋应用程序,我正在使用 Unity 3D 4.6.5f1 从 32 位移植到 64 位。它使用 OpenGLS2.0、.NET 2.0 库,并且正在生成通用二进制文件。

我收到一个运行时错误,在调试器中显示以下内容:

 NullReferenceException: A null value was found where an object instance was required.
  at <PrivateImplementationDetails>..ctor () [0x00000] in <filename unknown>:0 
  at ValilScriptObject.Update () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Dictionary`2+ShimEnumerator[Boo.Lang.Runtime.DynamicDispatching.DispatcherKey,Boo.Lang.Runtime.DynamicDispatching.Dispatcher].get_Current () [0x00000] in <filename unknown>:0 
System.Collections.Generic.ShimEnumerator:get_Current()

(文件名:目前在 il2cpp 行上不可用:4294967295)

它使用 Mono 2.0 编译得很好,但是当我将它移植到 64 位通用二进制文件的 IL2CPP 时,它就会抛出错误。

它引用的 Update() 函数似乎没问题。

void Update () {
    if(Request.Length>0)
    {
        string answ="";
        Answer=Engine1.GetNextMove(Request, null, Deep);
        Request="";
        if(Answer.Length>0) answ=Answer.Substring(0,2)+"-"+Answer.Substring(2,2);
        if(Answer.Length>4) answ+="="+(Answer.Substring(4,1)).ToUpper();
        ((TextMesh)GetComponent(typeof(TextMesh))).text=answ;

        //Application.ExternalCall("JSAnswer", answ);

        (GameObject.Find("Script2")).SendMessage("EngineAnswer",answ);
    }

}

它只是使用 Valil Chess Engine(用 C# 编写)来获得适当的答案(下一步)。它在 Mono 2.0 中工作正常,但在 IL2CPP 中失败。有什么想法吗?

最佳答案

我终于找到答案了。当 ChessEngine 初始化时,会初始化一个 ChessEngine.OpeningBook 类。我干脆把这门课全部拿出来,它就像一个魅力。该类看起来像:

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
//using Valil.Chess.Engine.Properties;

namespace Valil.Chess.Engine
{
    public sealed partial class ChessEngine
    {
        // hashtable with the board hash as the key and a list of moves for this board configuration as the value
        public static Dictionary<int, List<short>> book;

        // helps choose a move when the list contains more than one
        private Random random;

        /// <summary>
        /// Initializes the opening book.
        /// </summary>
        private void InitializeOpeningBook()
        {
            // initialize the random generator
            random = new Random(unchecked((int)DateTime.Now.Ticks));

            int Settings_Default_OpeningBookSize = 2755;
            //int Settings_Default_OpeningBookByteSize = 16530;

            //Assembly assembly = Assembly.GetExecutingAssembly();
            //String[] ss = assembly.GetManifestResourceNames();

            // THERE IS NO FILE & MANIFEST ASSEMBLY IN UNITY3D FOR FREE...
            // SO, CLASS ObookMem IS AS OPENING BOOK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            Stream readstream = Assembly.GetExecutingAssembly().GetManifestResourceStream("valil_silverlightchess.book.bin");

            // the "book.bin" file is a binary file with this pattern: int,short,int,short etc.
            // a 4-byte int represent a board hash, the following 2-byte short is a move (the first byte represents the starting square, the second one the ending square)

            // read "book.bin" and put the values in the hashtable
            try
            {

                using (BinaryReader br = new BinaryReader( readstream ))

//                using (BinaryReader br = new BinaryReader(new BufferedStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Valil.Chess.Engine.book.bin"), Settings.Default.OpeningBookByteSize)))
//                using (BinaryReader br = new BinaryReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("book.bin")))


                {
                    book = new Dictionary<int, List<short>>(Settings_Default_OpeningBookSize);

                    for (int i = 0; i < Settings_Default_OpeningBookSize; i++)
                    {
                        int hash = br.ReadInt32();
                        short move = br.ReadInt16();

                        // if the hashtable already contains this hash, add the move to the list
                        // otherwise create a new list and add the pair to the hashtable
                        if (book.ContainsKey(hash))
                        {
                            book[hash].Add(move);
                        }
                        else
                        {
                            List<short> list = new List<short>(1);
                            list.Add(move);
                            book.Add(hash, list);
                        }
                    }
                }
            }
            catch
            {
            }
        }
    }
}

我认为 IL2CPP 编译器不喜欢 System.Reflection 以及我的 Dictionary 和 List 类型。

关于c# - Unity3D iOS 64 位的 IL2CPP 编译器运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30244908/

相关文章:

c# - 如何使用 C# 获取 XML 文档的所有子标记名称?

c# - 将子高度或宽度设置为与android中的 "match_parent"相同的父控件

c# - 将 RGB 字符串转换为 Color32

unity-game-engine - Unity3D : Recalculating collisions after Physics2D. IgnoreLayerCollision()?

ios - 为什么收到推送通知后launchOptions为nil?

unity-game-engine - 如何在运行时操纵地形的形状区域 - Unity 3D

c# - 使用 Neo4j .Net 驱动程序的结果

c# - 添加EventHandler += EventHandler有什么作用?

ios - 为 OpenGL ES/Cocos2d 优化纹理的方法

ios - 我应该遵循 iOS 数据存储指南吗?