示例#1
0
        /// <summary>
        /// Main run method.
        /// This causes any modified code to be recompiled and executed on the mouse crawler.
        /// </summary>
        public void RunCrawler()
        {
            // Get the C# code from the input field
            string cSharpSource = runCrawlerInput.text;

            // Dont recompile the same code
            if (activeCSharpSource != cSharpSource || activeCrawlerScript == null)
            {
                // Remove any other scripts
                StopCrawler();

                try
                {
                    // Compile code
                    ScriptType type = domain.CompileAndLoadMainSource(cSharpSource, ScriptSecurityMode.UseSettings, assemblyReferences);

                    // Check for null
                    if (type == null)
                    {
                        if (domain.RoslynCompilerService.LastCompileResult.Success == false)
                        {
                            throw new Exception("Maze crawler code contained errors. Please fix and try again");
                        }
                        else if (domain.SecurityResult.IsSecurityVerified == false)
                        {
                            throw new Exception("Maze crawler code failed code security verification");
                        }
                        else
                        {
                            throw new Exception("Maze crawler code does not define a class. You must include one class definition of any name that inherits from 'RoslynCSharp.Example.MazeCrawler'");
                        }
                    }

                    // Check for base class
                    if (type.IsSubTypeOf <MazeCrawler>() == false)
                    {
                        throw new Exception("Maze crawler code must define a single type that inherits from 'RoslynCSharp.Example.MazeCrawler'");
                    }



                    // Create an instance
                    activeCrawlerScript = type.CreateInstance(mazeMouse);
                    activeCSharpSource  = cSharpSource;

                    // Set speed value
                    activeCrawlerScript.Fields["breadcrumbPrefab"] = breadcrumbPrefab;
                    activeCrawlerScript.Fields["moveSpeed"]        = mouseSpeed;
                }
                catch (Exception e)
                {
                    // Show the code editor window
                    codeEditorWindow.SetActive(true);
                    throw e;
                }
            }
            else
            {
                // Get the maze crawler instance
                MazeCrawler mazeCrawler = activeCrawlerScript.GetInstanceAs <MazeCrawler>(false);

                // Call the restart method
                mazeCrawler.Restart();
            }
        }