码迷,mamicode.com
首页 > 编程语言 > 详细

【Roslyn C#】Runtime环境Unity读取字符串代码

时间:2021-06-18 19:54:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:private   world   arp   deb   art   sha   src   compile   from   

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");
    }
    
}

 

【Roslyn C#】Runtime环境Unity读取字符串代码

标签:private   world   arp   deb   art   sha   src   compile   from   

原文地址:https://www.cnblogs.com/sanyejun/p/14899043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!