Hello, I need to parse some code during a dialog. working: Code (csharp): 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 not working: Code (csharp): Mono.CSharp.Evaluator.Run("using UnityEngine;"); Debug.Log(Mono.CSharp.Evaluator.Run("Data.go=GameObject.FindGameObjectWithTag(\"Player\");")); //true Debug.Log(Data.go); //null What's wrong with it?
Code (csharp): 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 ["answer1","answer2","answer3"]; and one array of actions [ "GameObject.Find(\"Door1\").getComponent<DoorScript>.open();", "GameObject.Find(\"Door2\").getComponent<DoorScript>.open();", "GameObject.Find(\"GUI\").getComponent<GUIScript>.sale.open();" ] I can live with that for the moment. You will need the .dll to make use of the evaluator.
the c# evaluator is real pain and useless. I created a new JS File in the Plugins Folder. Plugins/Evil.js Code (csharp): public static function uate (string:String):Object { eval(string); } in c# Evil.uate("GameObject.Find(\"Door1\").GetComponent(\"Door\").open();Debug.Log(\"do not do this!!!\");"); that works like a charm
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: Code (csharp): 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: Code (csharp): 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?
This seems clever — but when I try it, I get this cryptic error: My google-fu is not strong enough to decipher this one. Any ideas what I'm doing wrong?
Hi JoeStrout, 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.