Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. using Microsoft.CSharp;
  2. using System;
  3. using System.CodeDom.Compiler;
  4. using System.Reflection;
  5. using System.Text;
  6. using UnityEngine;
  7.  
  8. public class RuntimeCompileTest : MonoBehaviour
  9. {
  10. void Start()
  11. {
  12. var assembly = Compile(@"
  13. using UnityEngine;
  14.  
  15. public class RuntimeCompiled : MonoBehaviour
  16. {
  17. public static RuntimeCompiled AddYourselfTo(GameObject host)
  18. {
  19. return host.AddComponent<RuntimeCompiled>();
  20. }
  21.  
  22. void Start()
  23. {
  24. Debug.Log(""The runtime compiled component was successfully attached to"" + gameObject.name);
  25. }
  26. }");
  27.  
  28. var runtimeType = assembly.GetType("RuntimeCompiled");
  29. var method = runtimeType.GetMethod("AddYourselfTo");
  30. var del = (Func<GameObject, MonoBehaviour>)
  31. Delegate.CreateDelegate(
  32. typeof(Func<GameObject, MonoBehaviour>),
  33. method
  34. );
  35.  
  36. // We ask the compiled method to add its component to this.gameObject
  37. var addedComponent = del.Invoke(gameObject);
  38.  
  39. // The delegate pre-bakes the reflection, so repeated calls don't
  40. // cost us every time, as long as we keep re-using the delegate.
  41. }
  42.  
  43. public static Assembly Compile(string source)
  44. {
  45. // Replace this Compiler.CSharpCodeProvider wth aeroson's version
  46. // if you're targeting non-Windows platforms:
  47. var provider = new CSharpCodeProvider();
  48. var param = new CompilerParameters();
  49.  
  50. // Add ALL of the assembly references
  51. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  52. {
  53. param.ReferencedAssemblies.Add(assembly.Location);
  54. }
  55.  
  56. // Or, uncomment just the assemblies you need...
  57.  
  58. // System namespace for common types like collections.
  59. //param.ReferencedAssemblies.Add("System.dll");
  60.  
  61. // This contains methods from the Unity namespaces:
  62. //param.ReferencedAssemblies.Add("UnityEngines.dll");
  63.  
  64. // This assembly contains runtime C# code from your Assets folders:
  65. // (If you're using editor scripts, they may be in another assembly)
  66. //param.ReferencedAssemblies.Add("CSharp.dll");
  67.  
  68.  
  69. // Generate a dll in memory
  70. param.GenerateExecutable = false;
  71. param.GenerateInMemory = true;
  72.  
  73. // Compile the source
  74. var result = provider.CompileAssemblyFromSource(param, source);
  75.  
  76. if (result.Errors.Count > 0) {
  77. var msg = new StringBuilder();
  78. foreach (CompilerError error in result.Errors) {
  79. msg.AppendFormat("Error ({0}): {1}n",
  80. error.ErrorNumber, error.ErrorText);
  81. }
  82. throw new Exception(msg.ToString());
  83. }
  84.  
  85. // Return the assembly
  86. return result.CompiledAssembly;
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement