0

So I have a project in Unity that is supposed to be able to load and instantiate classes from an assembly exported from another program. It goes like this:

  1. Necessary files are exported as DLL from Unity project
  2. Other program loads this DLL and uses it to make a new class (i.e. calling methods in the original Unity app), which it then compiles to another DLL
  3. Original Unity app loads this DLL and runs the newly created class

However, when I get to step 3, its like functions in the original Unity project are duplicated and I get conflicts. I guess this makes sense because when you compile the DLL it has to resolve all references.

I then tried doing it so the original Unity project would take the code generated by the other program (text, as opposed to the dll) and compile it at runtime. This worked on Windows, but the problem is that this is an Android app and when I build to the phone, I get the following exception using CSharpCodeProvider to compile:

2019/08/29 14:40:45.424 20040 20069 Error Unity DirectoryNotFoundException: Could not find a part of the path "/tmp/a6icqx2c.tmp".
2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x00164] in <7d97106330684add86d080ecf65bfe69>:0 
2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in <7d97106330684add86d080ecf65bfe69>:0 
2019/08/29 14:40:45.424 20040 20069 Error Unity at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in <7d97106330684add86d080ecf65bfe69>:0 
2019/08/29 14:40:45.424 20040 20069 Error Unity at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
2019/08/29 14:40:45.424 20040 20069 Error Unity at System.CodeDom.Compiler.TempFileCollection.EnsureTempNameCreated () [0x00076] in <0079a30f96a047348857e1cecc6c638a>:0 
2019/08/29 14:40:45.424 20040 20069 Error Unity at System.Co

Unfortunately, the rest is cut off. I tried using Roslyn and got a DirectoryNotFoundException as well. Does anyone know either how I can fix this exception, or make the DLL file in the other app so that it will call the methods correctly in the original app?

  • I know nothing about Unity, but why is Roslyn involved at all here? – Paulo Morgado Aug 31 '19 at 20:29
  • Roslyn was not original involved, I only tried it to see if it would fix the issue. The original code (and one I would like to use) simply uses the CSharpCodeProvider. – Tiernan Watson Sep 01 '19 at 08:35
  • I'm still failing to understand why you need to generate code. – Paulo Morgado Sep 01 '19 at 16:44
  • The user is allowed to create custom scripts through a visual scripter (which is the other app), so that program generates the code and then it needs to be compiled so it can be run by the main app. :) – Tiernan Watson Sep 01 '19 at 16:59

1 Answers1

0

So I managed to come up with a solution using dependency injection.

Basically, I separated out the shared code files (i.e. the DLL that is exported by the original Unity app), into a separate project and instead of having concrete classes I created some interfaces. For example, one of these interfaces is "IFunctionLibrary", which contains signatures for the necessary functions that can be used. The main app then implements this. Then the app that is used to build the custom scripts knows about what functions that will be implemented by the concrete library.

References are stored in the concrete exported class to objects that satisfy the interfaces and upon instantiation, the the main app injects its implementation of the interfaces into it.

Essentially both apps now share the library. This means I don't have to run the compiler on the phone, which is better to be honest.

Hopefully this can help someone in the future. Sorry I can't show much actual code, but it is quite private.