Показать сообщение отдельно
Старый 06.02.2017, 16:31   #9
Evgen
Разработчик
 
Аватар для Evgen
 
Регистрация: 12.01.2011
Адрес: Moscow
Сообщений: 419
Написано 68 полезных сообщений
(для 100 пользователей)
Ответ: Динамическая подгрузка скриптов C# eval

Есть некий Mono.CSharp.Evaluator позволяет выполнять C# код. Вот примерчик. Штука интересная, на android пока не пробовал.

using UnityEngine;
using System.Collections;
using Mono.CSharp;
using System;

public class EvalManager : MonoBehaviour
{
    public static EvalManager instance;
    string cmd = "for(int i=0;i<50;i++){\r\n" +
        "GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);\r\n" +
        "cube.transform.position = new Vector3(Random.Range(0,90), Random.Range(0,90), 2);\r\n" +
        "cube.transform.rotation = Quaternion.Euler(Random.Range(0,90),Random.Range(0,90),Random.Range(0,90));\r\n" +
        "}\r\n" +
        "Log.Print(\"Test\",true);";
    
    void Awake ()
    {
        instance = this;
    }
    
    void Start ()
    {
        int cnt = 0;
        while (cnt < 2) {
            // this needs to be run twice, as the references fail the first time through
            foreach (System.Reflection.Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
                if (assembly == null) {
                    Debug.Log ("Null Assembly");
                    continue;
                }
                Debug.Log (assembly);
                try {
                    Mono.CSharp.Evaluator.ReferenceAssembly (assembly);
                } catch (NullReferenceException e) {
                    Debug.Log ("Bad Assembly");
                }
            }
            Mono.CSharp.Evaluator.Evaluate ("1+2;");
            cnt++;
        }
        
        Mono.CSharp.Evaluator.Run("using UnityEngine;");
        Debug.Log (Mono.CSharp.Evaluator.GetUsing ());
    }
    
    public object Run (string cmd)
    {
        //Run executes the code, and returns true if it was succesful and false if it did not parse
        // for example GameObject.Find("MyGameObject").transform.Translate( new Vector3(0, 2, 0));
        Debug.Log ("Run:"+cmd);
        object result = Mono.CSharp.Evaluator.Run (cmd);
        Debug.Log ("Run Result:"+result);
        return result;
    }
    
    public object Eval (string cmd)
    {
        // Evaluate requires a value as the last statement;
        // So you can do "typeof(GameObject);", but not "var a = typeof(GameObject);"
        Debug.Log ("Eval:"+cmd);
        object result = Mono.CSharp.Evaluator.Evaluate (cmd);
        Debug.Log ("Eval Result:"+result);
        return result;
    }
    
       void OnGUI() {
        int y = Screen.height - 120;
        cmd = GUI.TextArea (new Rect (90, y, 500, 120), cmd);
        if (GUI.Button (new Rect (10, y, 80, 20), "Eval:")) {
            Eval (cmd);
        }
        if (GUI.Button (new Rect (10, y+20, 80, 20), "Run:")) {
            Run (cmd);
        }
    }
}

Последний раз редактировалось Evgen, 06.02.2017 в 20:17.
(Offline)
 
Ответить с цитированием