我不确定自己被选为正确的头衔。 但在这里,我将尝试解释我想要做的细节。
目前我正在为自己的游戏开发简单的2D游戏引擎。 假设我有4个班级:
Scene
和SceneObject
:SceneObject[] Objects
类中有Scene
变量,表示场景中的对象(SceneObject可以是文本,图像,按钮等。对于文本它将是SceneObject.Text,对于图像,它将是SceneObject.Image等。)ContentManager
:处理SceneObject.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