Unity3D is not loading this .NET library, with no obvious reason why - c#

I'm trying to get a third party SDK working with Unity. Here's a download of the files in said SDK: https://dl.dropboxusercontent.com/u/11217331/VSDK.zip
The "_DotNET.dll" is a .NET wrapper for the unmanaged code in the CPP dll. For some reason, Unity doesn't load the .NET dll whatever I try:
I put both DLLs in the Assets/Plugins folder
I am using Unity Pro 4.5.0f6, on Windows 8.1
The SDK "_DotNET.dll" is x86 .NET 2.0, and seems to be completely compatible with Unity's version of Mono
I tried putting both DLLs in the Program Files/Unity/Editor folder, still doesn't load them
I do not have the source to these DLLs so I can't make any changes to them, but everything points at Unity being weird here
There are no errors in the editor log or anywhere that give me a hint to why Unity just completely ignores these DLLs
If you can get this C# script working in Unity by loading these DLLs, please tell me your secrets:
using UnityEngine;
using System.Collections;
using ViconDataStreamSDK;
public class test : MonoBehaviour {
ViconDataStreamSDK.DotNET.Client e;
void Start () {
bool isConnected = e.IsConnected().Connected;
Debug.Log("Is it connected?: " + isConnected.ToString());
}
void Update () {
}
}

Ok, it seems I can load it by going back one Unity version, to 4.3.4f1. No idea why, culprit must be somewhere in the Unity 4.5 patch notes (http://unity3d.com/unity/whats-new/unity-4.5).

You will probably need to put the source code for the C# plugin into the unity and see what actual errors it gives you.

Related

Wrong reference of Newtonsoft assembly for class library in another program

I have a class library in .NET Framework 4.7.2 and I use a reference to Newtonsoft.Json Version 12... something.
If I run the app as a windows application (there is a winform) everything works just fine. However, it is actually a class library that is being called by another program as a plugin. Inside that program I get however this error
Which translates basically to "couldn't find the Assembly...". There Version=12.0.0.0 is begin referenced and the dll in that particular folder (the plugin folder) says version 12.0.3.23909
I tried to clean the solution, delete every reference to newtonsoft I found etc. but the result is always identical. What am I missing?
Sorry if this question has been answered before, I searched dozens of previous questions, but I can't seem to understand what is going on appart from "redo everything", which doesn't work here.
UPDATE
The output bin is as follows
If I change the program to a windows application and run it by calling the exe, it works, but not via the other program where it should run as a plugin. Interestingly, the core dll is being opened and the baseform works, but not newtonsoft.
Change at least one of the versions. I mean:
Change the version of Newtonsoft of your class library to -> 12.0.3.23909 and then build the class library and use it as a plugin.
Or change the Newtonsoft version of your project to 12.0.0 and then use your class lib as a plugin inside it.
Or:
You can carry Newtonsoft (12.0.0) with your class library and load both of Newtonsft and your class library as a plugin. (this works too but you should be aware of Dll conflict between 12.0.0 and 12.0.3)

C# Do Roslyn work on Android (with Xamarin)?

I recently use Roslyn to compile and execute code at the runtime of a game application. Thank to some useful ressource such as this web site and Vendettamit answer, I manage to code a program on a C# Console Net Core project on window 10 which execute this code located in a txt file at the root of the application.
using System;
namespace Test15
{
public class Program
{
public static int Main()
{
System.Console.WriteLine("Hello World from external Dll !");
System.Console.WriteLine("And it work !");
return 10;
}
}
}
I'm not going to share code because it is really similar to Vendettamit answer, and this program work well on a C# Console Net Core project on window 10.
So next step, I try to make this program work with the C# Monogame Framework on a Android project which use Xamarin.
First problem : when trying to add the nugget package "Microsoft.CodeAnalysis" which seem necessary for Roslyn, I have the 2 following error :
Unable to resolve reference 'Humanizer', referenced by `Microsoft.CodeAnalysis.CSharp.Workspaces`.
Add NuGet package or assembly reference for 'Humanizer', or remove the reference to
'Microsoft.CodeAnalysis.CSharp.Workspaces'. Game2
Unable to resolve reference 'SQLitePCLRaw.core', referenced by `Microsoft.CodeAnalysis.Workspaces`.
Add NuGet package or assembly reference for 'SQLitePCLRaw.core', or remove the reference to
'Microsoft.CodeAnalysis.Workspaces'. Game2
(Translated from French)
When replacing the "Microsoft.CodeAnalysis" nugget package by "microsoft.CodeAnalysis.CSharp", both errors disappears.
However at the runtime, adding the MetadataReference don't work.
For instance in the Console project I use :
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);
To add a MetadataReference to mscorlib.dll, but on an Android project, it crash because typeof(object).GetTypeInfo().Assembly.Location return "mscorlib.dll" instead of somethings like "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.9\System.Private.CoreLib.dll", because we are on Android I guess. (same with other MetadataReference )
So when calling the method CSharpCompilation.Create(), the "reference" argument is an array of null MetadataReference and it crash.
Based on what I read, I think (but I'm not sure) that Roslyn can't work on android because of the missing location of System.Object.
So can someone can confirm (or invalidate) if there is a way to work with Roslyn on Android ?
(Bonus point if you know some other way to compile/execute or "interpret" C# code on any device)
Thank for reading and stay safe.

What is causing the 'WrongThreadException' in Unity 3D?

I want to integrate a C++ project in Unity. Unity provides a mechanism called I2CPP (Intermediate Language To C++) which allows to add C++ code to your unity project. I've created a simple C++ class and header in a "Blank App (Universal Windows C++/CX)" project in Visual Studio.
// header
namespace SomeNamespace {
public ref class MyRuntimeClass sealed
{
public:
// Constructor
MyRuntimeClass();
// Method to check if initialized
bool IsClassInitialized();
private:
bool _isRuntimeInitialized = false;
};
}
and
// implementation
using SomeNamespace;
MyRuntimeClass::MyRuntimeClass()
{
_isRuntimeInitialized = true;
}
bool MyRuntimeClass::IsClassInitialized()
{
return _isRuntimeInitialized;
};
I've made in Unity a simple project and made the necessary changes in the Player settings outlined in the documentation. I also added a cube as a game object and attached to the cube a script which uses my C++ code, i.e.
#if ENABLE_WINMD_SUPPORT
using SomeNamespace;
#endif
public class RuntimeSampleUnity : MonoBehaviour
{
#if ENABLE_WINMD_SUPPORT
private MyRuntimeClass _myRuntimeClass;
#endif
// Default MonoBehaviour method
void Start()
{
#if ENABLE_WINMD_SUPPORT
// New instance of runtime class
_myRuntimeClass = new MyRuntimeClass();
// Check to see if we initialized C++ runtime component
var isInit = _myRuntimeClass.IsClassInitialized();
Debug.LogFormat("MyRuntimeClass: {0}", isInit);
#endif
}
}
In a final step, I've added the winmd file from the C++ project to my assets in Unity. The project builds fine, but when I run the project I get an Platform.WrongThreadException: The application called an interface that was marshalled for a different thread. What is causing this exception (and how do I fix it)?
EDIT: To elaborate a bit why I'm doing what I'm doing: Microsoft provides a project which shows how to integrate OpenCV (C++) to HoloLens-based projects. While it provides a UWP project which mixes OpenCV and C#, it doesn't show how to integrate this particular project into Unity. Somebody actually made this possible via I2CPP. While I ran into issues using his Visual Studio 2017 based project into Visual Studio 2019, I've tried to make a minimal example to understand how it (basically) works.
I think you're misunderstanding the concept of IL2CPP.
IL2CPP (Intermediate Language To C++) is a Unity-developed scripting
backend which you can use as an alternative to Mono when building
projects for various platforms. When building a project using IL2CPP,
Unity converts IL code from scripts and assemblies to C++, before
creating a native binary file (.exe, apk, .xap, for example) for your
chosen platform. Some of the uses for IL2CPP include increasing the
performance, security, and platform compatibility of your Unity
projects.
To use C++ you have to write a Native Plugin.

When installing Firebase on Unity, I get an error "Unloading broken assembly"

I am planning to introduce Firebase to add a push notification function to the game I made with Unity.
I pasted the application-specific google-services.json in the Asset folder,
I installed FirebaseMessaging.unitypackage of SDK downloaded from here, but I get an error.
Console
Unloading broken assembly Assets/Firebase/Plugins/Firebase.App.dll, this assembly can cause crashes in the runtime
Unloading broken assembly Assets/Firebase/Plugins/Firebase.Messaging.dll, this assembly can cause crashes in the runtime
Unloading broken assembly Assets/Firebase/Plugins/Firebase.Platform.dll, this assembly can cause crashes in the runtime
Generation of the Firebase Android resource file google-services.xml from Assets/google-services.json failed.
If you have not included a valid Firebase Android resources in your app it will fail to initialize.
C:/UnityProjects/Test/Assets..\Assets\Firebase\Editor\generate_xml_from_google_services_json.exe -i "Assets/google-services.json" -l.
Microsoft.VC90.CRT.manifest could not be extracted!
You can start to diagnose this issue by executing "C:/UnityProjects/Test/Assets..\Assets\Firebase\Editor\generate_xml_from_google_services_json.exe -i "Assets/google-services.json" -l." from the command line.
UnityEngine.Debug:LogError(Object)
Firebase.Editor.GenerateXmlFromGoogleServicesJson:RunResourceGenerator(String, String, Boolean) (at Z:/tmp/tmp.CeTbzghE2x/firebase/app/client/unity/editor/src/GenerateXmlFromGoogleServicesJson.cs:508)
Firebase.Editor.GenerateXmlFromGoogleServicesJson:ReadBundleIds(String) (at Z:/tmp/tmp.CeTbzghE2x/firebase/app/client/unity/editor/src/GenerateXmlFromGoogleServicesJson.cs:369)
Firebase.Editor.GenerateXmlFromGoogleServicesJson:UpdateConfigFileDirectory() (at Z:/tmp/tmp.CeTbzghE2x/firebase/app/client/unity/editor/src/GenerateXmlFromGoogleServicesJson.cs:261)
Firebase.Editor.GenerateXmlFromGoogleServicesJson:CheckConfiguration() (at Z:/tmp/tmp.CeTbzghE2x/firebase/app/client/unity/editor/src/GenerateXmlFromGoogleServicesJson.cs:223)
Firebase.Editor.GenerateXmlFromGoogleServicesJson:.cctor() (at Z:/tmp/tmp.CeTbzghE2x/firebase/app/client/unity/editor/src/GenerateXmlFromGoogleServicesJson.cs:83)
UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes()
I'd like to add this;
public void Start() {
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
but due to errors, I get errors on the OnTokenReceived and OnMessageReceived parts.
Do these have problems with google-services.json created with Firebase for applications?
Anyone please solve me.
Check your Project Settings > IOS. Make sure the .NET is 4.X not 3.
https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html
I was facing the same error and find an answer from the Firebase's Github. Here is the post from the Github:
All "Unloading the assembly" does is unload the DLL from the app
domain (i.e Unity's process) then disable the platform targeting
options. So what you'll need to do is select the unloaded DLL(s) and
in the plugin inspector tick the platform check boxes to re-enable
them for the appropriate platform.
We typically follow the pattern to enable target platforms:
Firebase/*.dll : Target Android, Editor, Standalone - with the
exception of FIrebase.Database.dll Firebase/Firebase.Database.dll
: Target Android-only Firebase/iOS/*.dll : Target iOS
Firebase/Mono/Firebase.Database.dll : Target Editor, Standalone
Here is the link: https://github.com/firebase/quickstart-unity/issues/256
Hope it helps.
I ran into this problem with a freshly created project. The project was targeting Android and I was loading the dotnet4 version of the assembly. I switched to the dotnet3 version and it fixed the issue. I noticed that the Script Runtime Version targets .NET 3.5 Equivalent by default. I'm brand new to Unity, so I'm not sure if that's the issue.
Whenever Firebase.Editor.GenerateXMLFromGoogleServicesJson.* is missing, it's because Unity won't load it in, or has been instructed not to. Check this by clicking on Assets/Firebase/Editor/Firebase.Editor and make sure the "Editor" checkbox is on. If you changed it, hit apply, close Unity, reopen it. That may throw some errors as it figures itself out. Close, reopen it and all should be well. Binding and unbinding dll's has some complexity to it, so I always close Unity after messing with assembly dependencies.

Undeclared Identifier error Unity IOS to Xcode

I've a Unity 5.3.1 iOS project that also uses the new multiplayer network, UNet. Scary stuff. When I build and run the project, I get the following error in Xcode:
Use of undeclared identifier `IL2CPP_RAISE_MANAGED_EXCEPTION`
The project runs fine in the Unity Editor. It's in a huge Bulk_Generics_10.cpp
script that deals with System.Comparison1<UnityEngine.Networking.NetworkSystem.PeerInfoPlayer
Here is the block of code that the error is found.
// System.Void
System.Linq.Enumerable/<CreateDistinctIterator>c__Iterator3`1<System.Object>::Reset()
extern TypeInfo*
NotSupportedException_t1382227673_0_il2cpp_TypeInfo_var; extern const
uint32_t
U3CCreateDistinctIteratorU3Ec__Iterator3_1_Reset_m_1278777949_0_MetadataUsageId;
extern "C" void
U3CCreateDistinctIteratorU3Ec__Iterator3_1_Reset_m_1278777949_0_gshared
(U3CCreateDistinctIteratorU3Ec__Iterator3_1_t1454147488_0 * __this,
const MethodInfo* method) { static bool s_Il2CppMethodIntialized; if
(!s_Il2CppMethodIntialized) { il2cpp_codegen_initialize_method
(U3CCreateDistinctIteratorU3Ec__Iterator3_1_Reset_m_1278777949_0_MetadataUsageId);
s_Il2CppMethodIntialized = true; } {
NotSupportedException_t1382227673_0 * L_0 =
(NotSupportedException_t1382227673_0 *)il2cpp_codegen_object_new
(NotSupportedException_t1382227673_0_il2cpp_TypeInfo_var);
NotSupportedException__ctor_m149930845_0(L_0, /*hidden
argument*/NULL); IL2CPP_RAISE_MANAGED_EXCEPTION(L_0); } }
Looks like Unity did not hand it off cleanly to Xcode. I'm wondering if there's some adjustment I need to make in Unity.
How can I correct this error?
This is a new problem with the build packages for different platforms being optional in the installer. If you install a platform and then upgrade your version of Unity but don't install that platform on the second install the old version of the platform will remain and cause problems.
My solution was to totally wipe out my Unity3d folder and reinstall with the correct platforms.
related thread: http://forum.unity3d.com/threads/il2cpp_raise_managed_exception-undeclared-identifier.382377/
As far as I remember System.Linq is supported by iOS.
Here is a thread also exists regarding this,
Most of Linq extension methods from Linq for Collections are not working with IEnumerables on iOS since they require AOT runtime compiler which is not supported.
However there is a Linq for iOS library on Asset Store that is similar to Linq but doesn't require a runtime compiler. So you can use it on iOS.
Source: http://forum.unity3d.com/threads/linq-on-ios.84147/

Categories

Resources