1. 程式人生 > 其它 >【Roslyn C#】Runtime環境Unity讀取字串程式碼

【Roslyn C#】Runtime環境Unity讀取字串程式碼

Roslyn C#

下載地址:https://files-cdn.cnblogs.com/files/sanyejun/RoslynC_RuntimeCompiler.zip

使用示例

using System.Collections;
using System.Collections.Generic;
using RoslynCSharp;
using UnityEngine;

public class Example : MonoBehaviour
{
    private ScriptDomain domain = null;
    //建立字串程式碼
    private string
source = "using UnityEngine;" + "class Test : MonoBehaviour" + "{" + " void SayHello()" + " {" + " Debug.Log(\"Hello World\");" + " }" + "}"; // Start is called before the first frame update void Start() { // Create the domain - We are using C# code so we need the compiler
domain = ScriptDomain.CreateDomain("MyTestDomain", true); // Compile and load the source code ScriptType type = domain. CompileAndLoadMainSource(source); // We need to pass a game object because 'Test' inherits from MonoBehaviour ScriptProxy proxy = type.CreateInstance(gameObject);
// 呼叫字串的 SayHello 方法 proxy.Call("SayHello"); } }