c# to compile xml string as code during runtime

I`m looking for a way to compile a xml string as code during runtime. Below is the makeup of the xml. The delimiter inbetween the elements leaderless is what I am looking to run. The reason why we are doing this is because we want our game to be open to the player, to the point where he can write in his own code.

I have looked at Microsoft.CSharp, but unity says CSharp does not exist in the namespace Microsoft. I dont see why unitys own compiler in the console says that when VB and Mono say otherwise. I even tried Mono.CSharp, same error. I believe it has to do with missing system.dlls, but Im not sure if thats by design or if I need to find them. I am still using the free version of unity till I can afford Pro if my current version is an issue. Or if there is another way to compile the below xml string as code during runtime.

I have seen some forums saying Unity got rid of certain compiler support, and even a source dated a year ago saying Unity would be adding Mono compiler as a service, but I dont know if thats any reason why I haven`t seen a viable means to do what I need.

<Behavior>
  <Peon>
    <Leaderless>
      @"
      #using System;
      #using System.Collections.Generic;
      #using UnityEngine;
      
      static public class SkyDroneLeaderless : MonoBehaviour
      {
          public string wow = ""Success!"";            
          
          static public void Success()
          {
              Debug.Log(wow);
          }
      }"
    </Leaderless>
  </Peon>
</Behavior>

1 Answer

1

Well, depending on the platform you want to build for, Unity has a different sets of the default libraries (especially for the webplayer). Also it depends on your Player settings which comparibility level you’ve choosen.

Finally you should check the MonoCompatibility page to see what classes and what members of those classes are supported at which comparibility level.

If you have a pure managed code library you can include it without any problems by simply putting the DLL into your Assets folder (plugins folder recommended). However if you don’t have Unity pro or you build for the webplayer you can’t use mixed mode or native DLLs.

I never really took a deep look at the mono compiler, But if it’s written entirely in managed code you should be able to include it, if the libraries Unity provides aren’t enough.