【Roslyn C#】Runtime環境Unity讀取字串程式碼
阿新 • • 發佈:2021-06-18
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 stringsource = "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 compilerdomain = 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"); } }