zoukankan      html  css  js  c++  java
  • 【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");
        }
        
    }
  • 相关阅读:
    程序员掌握的单词汇总
    Django--基础补充
    C#入门概述
    python常用模块
    软件开发经验之谈
    p7.BTC-挖矿总结
    p6.BTC-挖矿难度
    p5.BTC-网络
    p4.BTC-实现
    p3.BTC-协议
  • 原文地址:https://www.cnblogs.com/sanyejun/p/14899043.html
Copyright © 2011-2022 走看看