public class Data
{
public static int n = 0;
public static GameObject go = null;
}
Mono.CSharp.Evaluator.Init(new string[] {} );
Assembly a = Assembly.GetExecutingAssembly();
Mono.CSharp.Evaluator.ReferenceAssembly(a);
Mono.CSharp.Evaluator.Run("Data.n=Data.n+5;");
Debug.Log(Data.n); //5
public class Data
{
public static int n = 4;
public static GameObject go = null;
public static void getGO(string str)
{
Data.go=GameObject.FindGameObjectWithTag(str);
}
}
Debug.Log(Mono.CSharp.Evaluator.Run("Data.getGO(\"Player\");"));
Debug.Log(Data.go);
Well, it seems that you have to prepare all functions. i wanted to avoid that. i donât understand how the evaluator works and why you can not access the classes. what i want to accomplish is simple one liners to have on array of answers
Your C# example with the GameObject does actually work if you also reference the UnityEngine assembly. Here is an example for referencing all loaded assemblies:
Mono.CSharp.Evaluator.Init(new string[] {} );
foreach(System.Reflection.Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
Mono.CSharp.Evaluator.ReferenceAssembly(assembly);
I realize I am necro-ing this thread, but I did get it working after some digging and figured it was worth sharing since this thread shows up high in search:
using UnityEngine;
using System.Collections;
using Mono.CSharp;
using System;
public class EvalManager : MonoBehaviour
{
public static EvalManager instance;
string cmd = "typeof(GameObject);";
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 (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 ("Result:"+result);
return result;
}
public int DrawGUI (int y)
{
cmd = GUI.TextField (new Rect (85, y, 400, 60), 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);
}
y += 60;
return y;
}
void OnGUI() {
int y = Screen.height - 60;
y = EvalManager.instance.DrawGUI(y);
}
}
@pakfront , can you explain what steps are needed to make that code compile? I did discover that I need to go into Player Settings and change the API Compatibility Level to â.NET 2.0â to avoid an error on Mono.CSharp itself.
But, even after doing that, Iâm still getting this error:
Other sources on the net seem to suggest that I need to somehow include the gmcs.exe in my project. But I donât know how to do that in Unity. Can anyone point me in the right direction?
I cannot explain to you why it cannot compile as my knowledge in Javascript are limited, but I can tell you that we had the same error on our side, and to fix it we removed all the â#pragma strictâ in any other javascript files to allow the compilation to happen.
Thatâs really not a fix so much as âallowing bad things to happenâ- you REALLY need to find an alternative. Also, this thread is really quite freakishly old.