Use MessagePack with unity in DLL - unity3d

Simple question,
I have made a dll that uses MessagePack (from nuget). Unfortunately it seems you cannot just drop the MessagePack dll directly into unity as it generates a bunch of errors. I was trying to find the best way to add message pack binaries to unity when I noticed that there is a unitypackage file that adds all the source code to the unity project. This, however is useless to me as my external library (my dll) contains a reference to MessagePack.
In the documentation, the author suggests using a shared project for the messages. If I use the attributes in the MessagePack namespace in my external library, then I need to have a reference to MessagePack.dll. Dropping the dll into my unity project generates all kinds of errors... Sort of a catch 22...
Any idea how I should go about this?

Related

How to include Unity3d libs in a non-Unity3d project?

I'm developing a C# library that will be used either as a plugin in some Unity3d projects and also used in non-Unity3d projects.
I need to use some Unity3d classes (such as UnityEngine.Matrix4x4) and standardize some functions to use valid types agnostically to whether the project is an Unity project or not.
So, as the title says: what is the right way to include Unity3d libraries in a non-Unity3d library project? Should I just include reference to the local Unity3d binaries (like UnityEngine.dll) in my project? If so, which is the right folder to look for these binaries (they appear in some different folders in the unity installation folder)?
If you are not actually running the Unity Engine for rendering or handling input it may be the case of forcing a round peg into a square hole, both technically and legally.
The most elegant solution would likely be to write your library in C# using more agnostic libraries. For example the Matrix4x4 class you mentioned has an equivalent in System.Numerics. Alongside the library all thats needed is a light wrapper converting a System.Numerics.Matrix4x4 to a UnityEngine.Matrix4x4. Sometimes the Unity devs themselves do stuff like this, for example the Unity.Mathematics.float3 struct which works better in ECS land than a standard Vector3.
FYI if you're looking for how some particular system works, check out the C# Reference GitHub Repository, for example the Matrix4x4 struct. Just be aware that 'copying and pasting' Unity source code is not allowed.

TypeLoadException after loading .NET 4.5 C# DLL into Unity 2018.2.5

I want to be able to feed the camera frames from a webcam into Unity. I made a .NET 4.5 C# DLL using MediaFrameReader and event listeners. Here is some other user's implementation for accessing the Hololens camera frames: Hololens - Access Camera Frames.
When I import the DLL into my 2018.2.5 Unity project, it gives me the following error:
Unloading broken assembly "....", this assembly can cause crashes in the runtime
TypeLoadException: Could not find method due to a type load error
The C# plugin built successfully many times on Visual Studio. Also, I have properly set the Api Compatibility Level (in player settings) to .NET 4.x. What could be the fix to this?
All the other SO answers related to this I have already taken a look, but does not seem to help the problem. Thanks for all the help.
EDIT: https://issuetracker.unity3d.com/issues/unity-fails-to-load-net-4-dot-6-assemblies-with-typeloadexception is the most relatable post, but had no solution.
I have few possible solutions for you to explore.
DLLs issues:
Workaround:
Comment your UWP code (the part that uses the DLL), then build it in UNITY without the DLL. In the generated UWP solution, install the package from nuget or manually import the dll, then uncomment your code and finish your development. This is a short-term solution.It is going to be annoying as you re-build your solution many times and have to comment/uncomment then re-add dlls and so on.
Other possible solutions:
Failed to run reference rewriter with command error with unity error when adding a DLL to the assets folder
Your exact need
From your description, you really do not need everything in the link you referenced (Hololens - Access Camera Frames). You need much simpler version. I recently created MediaCapture solution for HoloLens as a workaround because PhotoCapture in Unity is not working in the HoloLens and everything is working without any additional DLLs. I will post for you few links to see if it may help you:
MediaCapture Unity & HoloLens: https://github.com/MSAlshair/HoloLensMediaCapture
This maybe a good start for you. You can combine it with your original reference. Use this project as starting point to make sure your project is building correct, then use the necessary code from the other resource that you posted to accomplish the task that you desire. You may need to download Unity 2018.2.12f1 because I didn't test it in 2018.2.5
MediaCapture & PhotoCapture: Hololens font camera
Good Luck!

Open DLL in Unity3D

I wanted to use ZXing library and to check some demos first. Website says:
The following demo clients are available:
-Unity3D and Vuforia demo (demonstrates encoding of barcodes and decoding of images from a camera with Unity3D)
I downloaded it, and it just a bunch of DLL files:
How do I run it in unity? This is supposed to be some sort of sample or demo, because demos for other platforms, like windows, contain .EXE files.
You should try the whole demo project.
https://zxingnet.svn.codeplex.com/svn/trunk/Clients/UnityDemo/
I think that will make the use of the libraries much clearer.
You do not "run it" anywhere, as they are just libraries.
The purpose of a DLL is to hold code that you can reference, dlls are not executables
You can reference these libraries from within your project and then see the referrence on ZXing library ( just google for it and the Git will come up ) and you can then use methods,classes etc from within those dlls.

Why can't I debug into UnityEngine.UI code?

I add the UnityEngine.UI.dll and UnityEditor.UI.dll to my assets folder with their mdb files . also i add the both project to my current project. i am sure all the unity engine ugui code build success, because i debug log in the event system, and it print message. when i want to step into the event system class, i always failed . I find unity will load the code from a build path ,rather than i original code. Why does it do this ? if i want to debug unity engine ugui code, what should i do ?
To debug code, you need two things:
Symbols, the list of all functions, classes, variables used throughout the module. When using C++ symbols are stored in special .pdb files on Windows (and you obviously are using Windows since you are talking about dlls). Symbols in C# (.NET in general) are stored in the .dll itself. Having symbols will let you see the name of functions on the call stack and possibly some variables but nothing more.
Source code of the module.
U3D's source code is proprietary - you need to spend a good amount of money to receive it. And if I am guess to - UnityEngine.UI.dll is a C++ module with stripped (removed) symbols removed.
Thus you have neither, so you can't debug U3D's code at all.
Why would you need to that anyway? If you want to see how the internals of a big game engine work, there are plenty of other options (for example UE4 and Lumberyard). If you are struggling with a problem and you'd like to be able to solve it through debugging...well though luck. Your best bet would be to ask in unity community.

Find out why Xcode has decided to link to a particular library

I'm using the Unity 3D engine to build an iPhone app, and when I go to generate my Xcode project for compilation, it includes a few fairly large libraries: Mono.Security.dll.s, System.dll.s, System.Core.dll.s, etc.
I don't know if this question is really an Xcode question or a Unity question, but I'm trying to figure out why each of those libraries is being linked - which functions / classes are being referenced - ideally so that I can rewrite my code to remove as many of the dependencies as possible. Does anybody know a way to find this information out?
Are you using any external assemblies? If so you should get the source code for them and check what they are including. Sometimes it's possible to disable stuff in external assemblies to remove unneeded dependencies.
Go into the "project settings->player" menu in Unity and make sure that stripping level is set as high as possible. Stripping will attempt to swap System.Core with mscorlib, which doesn't include stuff like Linq.
A way to find out why a particular assembly is being included is to open up the references section of your MonoDevelop solution, and double-click on an assembly. This will open the assembly browser and you'll be able to get an idea of the namespaces that depend on a particular assembly.
This DLL stuff is added to project by Unity3D, Xcode has nothing to do with this. What version of Unity3D do you use? Try to tweak Optimization options in project settings (Inspector), especially 'Stripping'.