- Home /
c# to compile xml string as code during runtime
Im 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 don
t see why unity`s 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 I`m not sure if that`s 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 don`t know if that`s 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>
A few hints:
Since your class is a static class it doesn't make much sense to derive it from $$anonymous$$onoBehaviour.
The using statement doesn't have a "#" infront
A string is a string, no matter where you got it from, so X$$anonymous$$LString (in this context) makes not much sense like a file-string (when you read the string out of a file) or http-string (because you got it from a webrequest). X$$anonymous$$LString usually referes to a string containing X$$anonymous$$L so the term is confusing here and the whole X$$anonymous$$L thing is not relevant to the question.
If you plan to actually compile $$anonymous$$onoBehaviour classes prepare for a lot of trouble since Unity usually need to know about the classes beforehand. There are ways to dynamically load compiled assemblies but it's a bit tricky to get it work ;)
Answer by Bunny83 · Dec 17, 2013 at 01:26 AM
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.
Id vote you up, but I lack the reputation. Anyways, looking at what you said helped put some of the things I was seeing into perspective earlier. Looking at the [$$anonymous$$onoCompatibility page][1] helped me see what someone meant by changing the .net setting with something else somewhat similar. $$anonymous$$ine was set to .net 2.0 subset, which does not handle $$anonymous$$icrosoft.Csharp, as the $$anonymous$$ono page shows it being in red. Setting it to .NET 2.0 in the player settings changed unity
s console error to nothing.
Now to go from here, and see what`s next on the list of things I live to fix on the way to the end goal ;p
[1]: http://docs.unity3d.com/410/Documentation/ScriptReference/$$anonymous$$onoCompatibility.html
Your answer
Follow this Question
Related Questions
Reading data from xml. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Read AND Write to XML at runtime 1 Answer
Questions about XML save/load 0 Answers