Advertisement
AnomalousUnderdog

Dynamic compiling in Unity 3d

Oct 8th, 2012
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //using Mono.CSharp;
  5. using Microsoft.CSharp;
  6.  
  7. using System;
  8. using System.Reflection;
  9. using System.CodeDom.Compiler;
  10.  
  11. // echo 'export PATH=/Applications/Unity/Unity.app/Contents/Frameworks/Mono/bin:$PATH' >> ~/.profile
  12. //
  13.  
  14. public class Compiler : MonoBehaviour
  15. {
  16. public static bool CompileCSharpCode(string sourceFile, string exeFile)
  17. {
  18.     CSharpCodeProvider provider = new CSharpCodeProvider();
  19.  
  20.     // Build the parameters for source compilation.
  21.     CompilerParameters cp = new CompilerParameters();
  22.  
  23.     // Add an assembly reference.
  24.     cp.ReferencedAssemblies.Add( "System.dll" );
  25.     //cp.ReferencedAssemblies.Add( "/Applications/Unity/Unity.app/Contents/Frameworks/Managed/UnityEngine.dll" );
  26.     cp.ReferencedAssemblies.Add( "C:/Program Files (x86)/Unity/Editor/Data/Managed/UnityEngine.dll" );
  27.  
  28.     // Generate an executable instead of
  29.     // a class library.
  30.     cp.GenerateExecutable = false;
  31.  
  32.     // Set the assembly file name to generate.
  33.     cp.OutputAssembly = exeFile;
  34.  
  35.     // Save the assembly as a physical file.
  36.     cp.GenerateInMemory = false;
  37.  
  38.     // Invoke compilation.
  39.     CompilerResults cr = provider.CompileAssemblyFromSource(cp, sourceFile);
  40.  
  41.    if (cr.Errors.Count > 0)
  42.    {
  43.        // Display compilation errors.
  44.         Debug.LogError("Errors building " + sourceFile + " into " + cr.PathToAssembly);
  45.         foreach (CompilerError ce in cr.Errors)
  46.         {
  47.             Debug.LogError(ce);
  48.         }
  49.     }
  50.     else
  51.     {
  52.         Debug.Log("Source " + sourceFile + " built into " + cr.PathToAssembly + " successfully.");
  53.     }
  54.  
  55.     // Return the results of compilation.
  56.     if (cr.Errors.Count > 0)
  57.     {
  58.         return false;
  59.     }
  60.     else
  61.     {
  62.         return true;
  63.     }
  64. }
  65.     void Start()
  66.     {
  67.         Debug.Log(Mono.CSharp.Evaluator.Evaluate("1+2;"));
  68.         bool success = CompileCSharpCode("class Test { public Test(){ UnityEngine.Debug.Log(\"Test made\"); } public void TestMethod(){ UnityEngine.Debug.Log(\"Test called\"); } }", "./test.dll");
  69.        
  70.         if (success)
  71.         {
  72.             /*AppDomainSetup ads = new AppDomainSetup();
  73.             ads.ShadowCopyFiles = "true";
  74.             AppDomain.CurrentDomain.SetShadowCopyFiles();
  75.             AppDomain newDomain = AppDomain.CreateDomain("newDomain");
  76.             byte[] rawAssembly = loadFile("/Users/Ferds/test.dll");
  77.             Assembly assembly = newDomain.Load(rawAssembly, null);*/
  78.            
  79.            
  80.             Assembly assembly = Assembly.LoadFrom("./test.dll");
  81.            
  82.             object test = assembly.CreateInstance("Test");
  83.             test.GetType().GetMethod("TestMethod").Invoke(test, null);
  84.            
  85.             assembly = null;
  86.            
  87.            
  88.             //AppDomain.Unload(newDomain);
  89.             //newDomain = null;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement