samedi 22 octobre 2022

How to Add a UnityEngine Assembly Reference Into a Code Snippet Invoked Using System.Reflection

I'm trying to add c# code compilation and execution at runtime to my unity game so that I can implement modding support. It's based around this code example so far:

https://gist.github.com/fearofcode/3abb41d0b1a60194765f1fdd81da5269. However, adding "using UnityEngine;" to the executed code in the string throws the following error:

error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?

I assumed you had to add an assembly reference to the "references" variable so that the code could find the correct namespace, but both adding "UnityEngine.dll" or just "UnityEngine" to "references" and adding all of the used assemblies in the running script itsself via "AppDomain.CurrentDomain.GetAssemblies()" throws the following error for each unity related assembly:

error CS0006: Metadata file `name of the file' could not be found.

With the GetAssemblies() method 12 other assemblies are accepted and used like you would expect, but all of the unity specific ones throw errors.

Am I doing the right thing, but just in a wrong way? Or is there a different way to reference the required assemblies/namespaces that I just can't find?





Aucun commentaire:

Enregistrer un commentaire