[英]Compile run-time C# Script with existing type
我不太確定自己選對了標題。 但是在這里我將嘗試解釋細節我想做什么。
目前,我正在為自己的游戲開發簡單的2D游戲引擎。 假設我有4個班級:
Scene
和SceneObject
: Scene
類中有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.