Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- //using Mono.CSharp;
- using Microsoft.CSharp;
- using System;
- using System.Reflection;
- using System.CodeDom.Compiler;
- // echo 'export PATH=/Applications/Unity/Unity.app/Contents/Frameworks/Mono/bin:$PATH' >> ~/.profile
- //
- public class Compiler : MonoBehaviour
- {
- public static bool CompileCSharpCode(string sourceFile, string exeFile)
- {
- CSharpCodeProvider provider = new CSharpCodeProvider();
- // Build the parameters for source compilation.
- CompilerParameters cp = new CompilerParameters();
- // Add an assembly reference.
- cp.ReferencedAssemblies.Add( "System.dll" );
- //cp.ReferencedAssemblies.Add( "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll" );
- cp.ReferencedAssemblies.Add( "C:/Program Files (x86)/Unity/Editor/Data/Managed/UnityEngine.dll" );
- // Generate an executable instead of
- // a class library.
- cp.GenerateExecutable = false;
- // Set the assembly file name to generate.
- cp.OutputAssembly = exeFile;
- // Save the assembly as a physical file.
- cp.GenerateInMemory = false;
- // Invoke compilation.
- CompilerResults cr = provider.CompileAssemblyFromSource(cp, sourceFile);
- if (cr.Errors.Count > 0)
- {
- // Display compilation errors.
- Debug.LogError("Errors building " + sourceFile + " into " + cr.PathToAssembly);
- foreach (CompilerError ce in cr.Errors)
- {
- Debug.LogError(ce);
- }
- }
- else
- {
- Debug.Log("Source " + sourceFile + " built into " + cr.PathToAssembly + " successfully.");
- }
- // Return the results of compilation.
- if (cr.Errors.Count > 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- void Start()
- {
- Debug.Log(Mono.CSharp.Evaluator.Evaluate("1+2;"));
- bool success = CompileCSharpCode("class Test { public Test(){ UnityEngine.Debug.Log(\"Test made\"); } public void TestMethod(){ UnityEngine.Debug.Log(\"Test called\"); } }", "./test.dll");
- if (success)
- {
- /*AppDomainSetup ads = new AppDomainSetup();
- ads.ShadowCopyFiles = "true";
- AppDomain.CurrentDomain.SetShadowCopyFiles();
- AppDomain newDomain = AppDomain.CreateDomain("newDomain");
- byte[] rawAssembly = loadFile("/Users/Ferds/test.dll");
- Assembly assembly = newDomain.Load(rawAssembly, null);*/
- Assembly assembly = Assembly.LoadFrom("./test.dll");
- object test = assembly.CreateInstance("Test");
- test.GetType().GetMethod("TestMethod").Invoke(test, null);
- assembly = null;
- //AppDomain.Unload(newDomain);
- //newDomain = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement