C# managed dll imports but can't use in script

Hello all,

Here is the issue.

I am working on a senior project for my BS in CSET. I have built a prototype AI decision engine. It uses 2 class libraries built in .net 4 with C#. The premise is to send requests to the engine from an external system like unity and receive actions back.

I have recompiled the External Interface library using .net 3.5.1 and the RX added references Tasks and ConcurrentQueues. This compiles fine in both VS 2008 and Mono which came with Unity.

My problem is accessing the assembly from a script. I can create the script with using External_Interface_Lib and both VS and Mono see the classes in the library and let me reference them. I can build the scripts without errors. However when I go back to unity, I have a compile error.

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.TypeLoadException: Could not load type ‘External_Interface_Lib.External_Interface’ from assembly ‘AI_External_Interface_2008, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’.

With the stack trace of:

at (wrapper managed-to-native) System.MonoType:GetMethodsByName (string,System.Reflection.BindingFlags,bool,System.Type)

at System.MonoType.GetMethods (BindingFlags bindingAttr) [0x00000] in :0

at Mono.CSharp.MemberCache.AddMethods (BindingFlags bf, System.Type type) [0x00000] in :0

at Mono.CSharp.MemberCache.AddMethods (System.Type type) [0x00000] in :0

at Mono.CSharp.MemberCache…ctor (IMemberContainer container) [0x00000] in :0

at Mono.CSharp.TypeHandle…ctor (System.Type type) [0x00000] in :0

at Mono.CSharp.TypeHandle.GetTypeHandle (System.Type t) [0x00000] in :0

at Mono.CSharp.TypeHandle.GetMemberCache (System.Type t) [0x00000] in :0

at Mono.CSharp.TypeManager.MemberLookup_FindMembers (System.Type t, MemberTypes mt, BindingFlags bf, System.String name, System.Boolean used_cache) [0x00000] in :0

at Mono.CSharp.TypeManager.RealMemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in :0

at Mono.CSharp.TypeManager.MemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in :0

at Mono.CSharp.Expression.MemberLookup (Mono.CSharp.CompilerContext ctx, System.Type container_type, System.Type qualifier_type, System.Type queried_type, System.String name, MemberTypes mt, BindingFlags bf, Location loc) [0x00000] in :0

at Mono.CSharp.Expression.MemberLookupFinal (Mono.CSharp.ResolveContext ec, System.Type qualifier_type, System.Type queried_type, System.String name, MemberTypes mt, BindingFlags bf, Location loc) [0x00000] in :0

at Mono.CSharp.New.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0

at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in :0

at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0

at Mono.CSharp.Assign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0

at Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0

at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in :0

at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0

at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in :0

at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in :0

at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in :0

at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in :0

Not sure what is going on here. I’ve searched the forums for help but don’t see anything. My script is pretty basic.

using UnityEngine;
using System.Collections;
using External_Interface_Lib;

public class AI_Engine_Interface : MonoBehaviour{

// Use this for initialization
void Start () {

External_Interface extInt = new External_Interface();

extInt.StartReceiving();
extInt.StartSending();
}

// Update is called once per frame
void Update () {
}
}

Anyone have any ideas?

show the prototypes for startreceiving and startsending please

Does your compiled assembly have references to some other 3rd party assembly? Thats what the initial error implies.
Don’t forget that assemblies in .NET don’t transport any external frameworks and libraries along them, every library you use through “using” must be moved along. In this case the External_Interface_Lib.dll which you must move into your unity project as well

The prototypes are simple, they are:

public void StartSending()

public void StartReceiving()

The included libraries are all system libraries. There are 4 classes in this library, 3 have:

using System;
using System.Text;

and the main External_Interface class has:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Thanks for your help.

Easiest way to solve it is: Drop the source of your library into unity and see where it fails, that way you will know what part of the library is exactly offending.

Unity does not support the full mono runtime, even less if you target anything but windows / osx standalone.

Aside of the threading all of these should work fine on most platforms, threading might be limited / disabled depending on platforms, the System.Net part has limitations / requirements on webplayer targeting.

Actually the using System.Threading.Tasks; is not part of the default .net 3.5.1 libraries. I had to add it with the reactive extensions.

So under references, it has:

System.CoreEx
System.Reactive
System.Threading

However, the solution compiles in both VS 2008 and Mono, so I’m not sure what Unity is complaining about.

Thanks

I tried adding using System.Threading.Tasks to the script and it is not there.

Is there a way to add a reference in the script? If I could add the reactive core library for System.Threading, I could use Tasks.

I tried, but I didn’t see that option…

if the editor claims its not there, then unity commonly does not support it on its own. you would need to copy the framework dll into your unity project along all other missing frameworks and see if that works.

It might or might not, its important to keep in mind for example that unity isn’t threadsafe and the whole unityengine namespace is a no go within any threading / async operation outside the unity mainprocess (ie anything but BeginInvoke or Coroutine)

Well I hope it will work, I’ll try it.

The only part of my library that Unity will really be interacting with is a thread safe concurrent queue, so I’m hoping this won’t be a problem.

This is mainly for asynchronous network calls, if I can make calls out to my AI Engine without my library, that may be the way to go.

Thanks for you help.

Under Windows Assemblies, I find the new resources I am looking for, but they can not be copied or moved.

So, how do you use the Unity framework to make external network calls? Do I need to write a network bind, call, wait for receive from a game object? I suppose that could work. How does unity deal with blocking on wait for receive? Does the whole system wait for the receive function to finish before continuing?