How can I use .NET framework 4.5 in Unity 5 - c#

I'm working with unity3D 5. And I wanna import external my KinectDB.dll (includes my sql server db functions with entityframework. And it was complied with .NET 4.5 framework) class library to unity. But unity console gives me an error like below. I searched for this problem many hours. But I can't reach to complete solution. How can I solve this? What are your advices? Thanks in advance.
Hint : I created an example project with .NET 3.5 framework. And I import it to unity. Unity doesnt give me an error in this case.
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
Missing method .ctor in assembly -------\Assets\KinectDB.dll, type System.Runtime.Versioning.TargetFrameworkAttribute
The class System.Runtime.Versioning.TargetFrameworkAttribute could not be loaded, used in KinectDB
Can't find custom attr constructor image: Assets\KinectDB.dll mtoken: 0x0a00000e

You can only wait till Unity updates its bundled Mono to 4.x. It is already on their roadmap. With Microsoft licenses Mono runtime from LGPL to MIT and Unity joined .NET Foundation, it is likely that such a platform upgrade might come sooner.
http://unity3d.com/unity/roadmap

Yeah. It all starts with reading the documentation - and realizing that Unity uses a quite outdated version of .NET. You CAN NOT use 4.5 libraries in Unity. What you CAN do is run them in a separate program and communicate with this program via network etc. - but that is a LOT of work.
Once in the future Unity will upgrade. But they have not yet.

Related

Importing PDFSharp Library to Unity

Please see bottom for latest update.
I've looked elsewhere on the web and there doesn't seem to be a great deal of information regarding the use of PDFSharp in Unity.
PDFSharp looked like a good, simple solution to get my app to create pdf files, so I downloaded the library into my project's assets folder to get started.
This seemed to work at first, as the desired syntax was now available in my scripts in MonoDevelop. When I tried the run the app, however, it failed to compile and displayed the following error:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
Being relatively new to Unity and completely new to any form of library extensions/file creation, I'm not sure what to do to resolve this. There's an awful lot of files in the PDFSharp folder to go through manually trying to find the root of the problem.
Does anyone have experience using PDFSharp with Unity, has encountered a similar problem and could help me solve it? I would be very grateful for any help.
Using the latest version of Unity with MonoDevelop and PDFSharp 1.31.
Using PDFSharp 1.32 threw up rather a few more errors: http://pastebin.com/PLUdZRbp
One link I looked at, but couldn't make much sense of: http://forum.pdfsharp.net/viewtopic.php?f=4&t=1206
Update: It turns out that I'd downloaded the source code instead of the assembly, so I downloaded that instead. The result was a lot better, but now it's throwing a TypeLoadException:
TypeLoadException: Could not load type 'PdfSharp.Charting.Renderers.RendererParameters' from assembly 'PdfSharp.Charting-WPF, Version=1.32.2608.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb'.
UnityEditor.DockArea:OnGUI()
It also appears that where I've used the line PdfDocument pdf = new PdfDocument(); in my script, it's throwing this error:
Assets/Scripts/Exporter.cs(18,17): error CS0433: The imported type `PdfSharp.Pdf.PdfDocument' is defined multiple times
Note: The TypeLoadException error, when the project is played with any scripts which use that library removed, actually causes Unity to crash to desktop.

Mac OS X, C#/Mono - GeckoFX "System.DllNotFoundException: xul"

I have an application which utilizes GeckoFX/xulrunner 22.
This program works great in Windows but no matter what I do I cannot get it to run in Linux nor Mac.
The primary focus is Mac though.
I've tried swapping out different versions of Windows, Linux & Mac xulrunner run-times as well as different initialization strings to try including both the Windows, Linux and Mac versions (of course only one at a time) like:
Gecko.Xpcom.Initialize("lib/xulrunner");
Gecko.Xpcom.Initialize("lib/XUL.framework/Versions/Current");
I have also tried prefixing with "./" as well as using code to obtain the current working directory to prepend to the path.
I've also tried copying over a dozen different versions of xul.dll into the executable folder and subdirectories within it.
No matter what I think to try, or search this site/google to try nothing has worked.
This is failing on the Initialize() call.
The program/class names are only starred out so this is not the first thing popping up in google when people search for the program.
Although my biggest concern is getting this running on Mac, I will also mention due to other posts on Stack Overflow and alike that on my Linux system, I have tried having various versions of Firefox installed and the path pointed to as per other peoples' instructions. It always shows the same error as on Mac.
Error follows: Unhandled Exception:
System.DllNotFoundException: xul
at (wrapper managed-to-native) Gecko.Xpcom:Alloc (intptr)
at Gecko.nsAString..ctor () [0x00000] in :0
at Gecko.nsAString..ctor (System.String value) [0x00000] in :0
at Gecko.Xpcom.Initialize (System.String binDirectory) [0x00000] in :0
at **********.**********.CreateBrowsers () [0x00000] in :0
at **********.**********.**********_Shown (System.Object sender, System.EventArgs e) [0x00000] in :0
at System.Windows.Forms.Form.OnShown (System.EventArgs e) [0x00000] in :0
at System.Windows.Forms.Form.SetVisibleCore (Boolean value) [0x00000] in :0
at System.Windows.Forms.Control.set_Visible (Boolean value) [0x00000] in :0
at (wrapper remoting-invoke-with-check) System.Windows.Forms.Control:set_Visible (bool)
at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000] in
:0
at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext context) [0x00000] in
:0
at System.Windows.Forms.Application.Run (System.Windows.Forms.Form mainForm) [0x00000] in :0
at **********.Program.Main () [0x00000] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException: xul
at (wrapper managed-to-native) Gecko.Xpcom:Alloc (intptr)
at Gecko.nsAString..ctor () [0x00000] in :0
at Gecko.nsAString..ctor (System.String value) [0x00000] in :0
at Gecko.Xpcom.Initialize (System.String binDirectory) [0x00000] in :0
at **********.**********.CreateBrowsers () [0x00000] in :0
at **********.**********.**********_Shown (System.Object sender, System.EventArgs e) [0x00000] in :0
at System.Windows.Forms.Form.OnShown (System.EventArgs e) [0x00000] in :0
at System.Windows.Forms.Form.SetVisibleCore (Boolean value) [0x00000] in :0
at System.Windows.Forms.Control.set_Visible (Boolean value) [0x00000] in :0
at (wrapper remoting-invoke-with-check) System.Windows.Forms.Control:set_Visible (bool)
at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000] in
:0
at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext context) [0x00000] in
:0
at System.Windows.Forms.Application.Run (System.Windows.Forms.Form mainForm) [0x00000] in :0
at **********.Program.Main () [0x00000] in :0
Firstly, to my knowledge no one has ever run geckofx on OSX.
The problem you are seeing is probably easy fixable but before getting geckofx to work on mac you will likely hit more problems.
Geckofx's architecture on Windows is simple. The GeckoWebBrowser control is a winforms based control, which underneath are win32 controls and gecko/firefox on windows also uses win32 controls.
However on Linux things are a little more complicated as gecko/firefox uses GTK while the mono winform implementation is based upon X11. Hence geckofx has some fairly complex code to make that all work. (Which is why one needs to use a different build of geckofx on Linux (one with the GTK flag defined).
So firstly on OSX you have to determine what UI toolkit does mono uses for winforms, is it X11, carbon or something else? Then you have to determine which toolkit Firefox uses on OSX, carbon, cocoa or something else? If they happen to match then its probably worth proceeding with the windows build of geckofx. If they are different then you will probably have to modify geckofx itself to deal with this issue.
Now on to the DllNotFoundException.
The PInvoke/DllImport calls are probably looking for "xul.dll". This file could be named something different in OSX's firefox (eg. libxul.so) so a dllmap config file is needed to map xul.dll to the correct file.
Also setting these two environment variables, MONO_LOG_LEVEL="debug" and MONO_LOG_MASK="dll" should provide you with useful debug information.
Additionally one can use an absolute path for Gecko.Xpcom.Initialize to avoid any confusion about finding the path.

Trying to connect Unity3d with a DLL that uses SQL Server Compact Missing or incorrect header for method CompileQueryPlan?

I'm working on a Unity3d project that involves Databases,
I wanted the other developer to work on the server's and produce a DLL for me to use in Unity
I tried to work directly in Unity with SqlServerCe but it always stated that it's missing libs
So I managed to load the Backend's DLL successfully in Unity3D (Using v3.5 .Net Client Profile, in both the VS and Unity's Mono Develop)
It initializes the SQL components successfully (reader ... etc)
But when I execute the reader the DLL returns this exception as a string (I handled that when an exception occurs it's returned as a string)
The Exception:
System.InvalidProgramException: Missing or incorrect header for method CompileQueryPlan
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand (CommandBehavior behavior, System.String method, ResultSetOptions options) [0x00000] in <filename unknown>:0
at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader (CommandBehavior behavior) [0x00000] in <filename unknown>:0
at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Data.SqlServerCe.SqlCeCommand:ExecuteReader ()
at AbuEl3orrefDatabaseBackend.DBHandler.PrepareAllQuestions () [0x00000] in <filename unknown>:0
Well I finally found the solution,
I was on Web Platform, as it doesn't support SQL Server Compact, so I switched to Desktop Platform from the Build Settings.

System.NullReferenceException in Linux Mono using Fast Colored Text Box

I am creating a small GUI in WinForms and am currently working on implementing a syntax highlighter. I found a library that runs great in windows and provides a dll for usage. However, the library seems to always crash in Linux. Here is the error I'm getting:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at FastColoredTextBoxNS.FastColoredTextBox..ctor () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) FastColoredTextBoxNS.FastColoredTextBox:.ctor ()
at FDE.FGUI..ctor () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) FDE.FGUI:.ctor ()
at FDE.FGUI.Main () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at FastColoredTextBoxNS.FastColoredTextBox..ctor () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) FastColoredTextBoxNS.FastColoredTextBox:.ctor ()
at FDE.FGUI..ctor () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) FDE.FGUI:.ctor ()
at FDE.FGUI.Main () [0x00000] in <filename unknown>:0
From the error message, it seems that Mono isn't seeing the dll given the <filename unknown> error during the constructor. Admittedly, The library I'm using (FastColoredTextBox) does not pass MoMA as it has a fair amount of P/Invokes. However, some people seemed to say they were able to get it running by avoiding the conflicting methods.
Here's the clincher, though. I decided to try to run the GUI through WINE and completely bypass Mono. Since it ran fine it Windows, I thought this would fix the error. However, the exact same error message pops up in Wine. I even have a shared folder between Windows and my Ubuntu live machine. The same exact executable will run on perfect Windows, but crash with this error in Linux. At this point, after hours of researching, I'm stumped.
Is there a certain way a dll has to be loaded in Mono or does the error lie in Mono/Wine incompatibilities with WinForms?
I looked at the source to the library. Assuming I've found the right library, it's calling the Win32 API, which isn't going to work on stock Mono.
Running under Wine should work better, but Wine is only partially implementing the Win32 API.
You may need to find a cross-platform replacement.

OpenCV + Unity3D integration

I am a unity developer trying my hands on opencv for the first time. My initial goal is to run the camera and detect blobs via opencv in unity3d. I am new to OpenCV and am trying to integrate it in Unity3D (on Windows 8 with Unity 4.3.2 and on a mac with Unity 4.2.1f). I followed this thread. But I am getting the following error as soon as I add a new C# script. And the moment I delete this script, the error goes (this script is Unity generated C# script).
Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0
at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
I couldn't find much about Unity and OpenCV integration. It would be great if you could help me out with this error and point me to a recent tutorial to learn more.
Thanks in advance!
We recently had to deal with the same problem, I'll post some generic information that would solve your problem and help other people.
OpenCV library and your OpenCV project must be compiled as static libraries (see OpenCV as a static library).
OpenCV library and your OpenCV project must be compiled for both 32bit and 64bit architectures.
The 32bit version will be used inside the editor (since the Unity3D editor supports 32bit architectures only), the 64 bit version for production.
The compiled OpenCV project must be copied inside the Asset > Plugins folder, the OpenCV library must be copied inside the Assets folder.
To use your OpenCV project inside a C# script, follow this code example:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
public class PluginImport : MonoBehaviour {
//Lets make our calls from the Plugin
[DllImport ("OpenCVProject")]
private static extern int openCVFunction();
void Start () {
openCVFunction();
}
}
pay attention to the using directives!
Other sources of information:
Unity Manual:Plugins

Categories

Resources