繁体   English   中英

使用现有类型编译运行时C#脚本

[英]Compile run-time C# Script with existing type

我不太确定自己选对了标题。 但是在这里我将尝试解释细节我想做什么。

目前,我正在为自己的游戏开发简单的2D游戏引擎。 假设我有4个班级:

  • SceneSceneObjectScene类中有SceneObject[] Objects变量,它们表示场景中的对象(SceneObject可以是文本,图像,按钮等。对于文本,它将是SceneObject.Text,对于图像,它将是SceneObject.Image等。上)。
  • ContentManager :处理SceneObject.Image图像源的SceneObject.Image ,所有Image都存储在此类中(“ Image”是System.Drawing.Image类型)
  • Log :简单类记录

我想要的是从C#脚本加载SceneObject ,但是在该脚本中,我希望允许脚本编写者与ContentManager (以及我可能在项目中创建的所有类,例如Log类)进行交互。

这里是要在运行时编译的C#代码示例(假设其名为“ ExampleScript.cs”):

using System;
using System.Collections.Generic;
using System.Text;

using Engine.Graphics;
using Engine.UI;

namespace Engine
{
    public class Example : Scene
    {
        public SceneObject.Button BtnExample;
        public SceneObject.Text LblExample;
        public Example()
        {
            // This will throw an exception if Example.png isn't exist in the ContentManager
            BtnExample = new SceneObject.Button(ContentManager.GetImage("Example.png"));
            LblExample = new SceneObject.Text("Hello World!");

            // This is the plus for C# Run-Time Code
            // Allow writer to add customization for each SceneObject
            BtnExample.Rotate(10);
            BtnExample.Color = Color.White;
            // and so on
            // I believe complicated effect / customization can be done easier than loading from plain text file (and it's somehow look cooler LOL)

            // Add it to "Objects" variable
            Add(LblExample, BtnExample);
            Log.WriteLine("SceneLoaded!");
        }
    }
}

然后我像这样编译并获取所有SceneObject

try
{
    Script SceneScript = new Script("ExampleScript.cs");
    Scene Example = SceneScript.Compile();
    SceneObject[] Objects = Example.Objects;
}
catch (Exception ex)
{
    // Compile Error here
    // Something like "'Example.png' isn't exist in content manager"
}

那我该怎么办呢?

我试图在Google上进行搜索,但发现的结果就像在运行时编译控制台应用程序一样,这对我来说还不够:\\

提前致谢! :D

我建议看一下CS-Script库。 特别是,请看一下有关“类型共享”模式的文档,我认为它可以完全满足您的期望(尽管您需要使用其运行时库来完成此工作)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM