0

miserable assembly reference

Unity is having trouble finding my DLL. I have a DLL that I'm able to successfully load in visual studio by adding a reference, but I'm still getting errors in Unity.

The DLL is in .NETframework 4.6.1, and I've tried upgrading the unity .NETframework to 4.x, installing the relevant SDK in the visual studio installer, using different versions of unity (2022, 2020, 2019), all no good.

I know the DLL works because I've been able to compile it outside of unity, but my goal for this project is to have it running in unity without hacky IPC.

Here are a few references to other things I've tried that don't work: A reference to the dll could not be added .dll file not accessible https://answers.unity.com/questions/458300/how-to-use-a-external-dll.html

edit1: So I've tried arranging the DLL as described, but that doesn't solve the issue. I think it must be an issue of "compatible binary" is there an easy way to check that?

more details: the DLL I'm having trouble referencing is a wrapper for another DLL, translating between C# and C++, might that be the issue?

please save me stack overflow.

DLLs in the project, .net DLL referencing c++ DLL

make punk
  • 1
  • 2
  • By _"native"_ I assume you mean _pre-compiled .NET assemblies?_ .NET isn't _"native"_ code. You can make C plugins for Unity, which are native. – MickyD Jun 01 '22 at 06:46

2 Answers2

1

To use plugins/pre-compiled assemblies in Unity (native or .NET), you must place the .DLL(s) in the Assets/Plugins folder. You can use child folders for x86 ir x64. I like to include a vendor name too. Tell me more....

e.g.

Assets
|--Plugins
   |--MickyD
      |--MickyD.SoM.Contracts.dll

Assuming it is a compatible binary, Unity will make the contents of the plug-in available to scripts that you define in Unity.

All you need do is to add the appropriate using at the top of your Unity script.

e.g.

using MickyD.SoM.Contracts;
using UnityEngine;
.
.
.
namespace MickyD.FleetDefender.Vehicles
{
    public class PlaneGadget
    {
        private Airspeed _airspeed; // Type defined in pre-compiled assembly
    }
}

If you are using ASMDEFs in your project with overridden includes then you might need to reference the DLL.

See also

MickyD
  • 14,343
  • 6
  • 43
  • 67
  • So I've tried arranging the DLL as described, but that doesn't solve the issue. I think from your post it must be an issue of "compatible binary". – make punk Jun 01 '22 at 20:47
  • @makepunk _"it must be an issue of 'compatible binary'"_ - [Unity supports the **.NET Framework 4.x** up to **.NET Framework 4.8** as well as **.NET Standard 2.1**] (https://docs.unity3d.com/Manual/dotnetProfileSupport.html) so your .NET Framework 4.x DLL theoretically should work unless of course it has dependent DLLs outside of the above. If you have any .NET Core libraries ensure they are .NET Standard 2.1 or lower which is the equivalent of .NET Core library 3.0 or lower. – MickyD Jun 02 '22 at 04:17
  • @makepunk when you drag the DLLs into **Assets\Plugins** does it appear at least in [The Project Window](https://docs.unity3d.com/Manual/ProjectView.html). Tell me that before trying to use it in your Unity scripts and I can help you more – MickyD Jun 02 '22 at 04:18
  • The dlls do appear in the project window, but I think the issue is that the DLL does have dependents outside of .netframework. The DLL I'm trying to reference (CLRWrapper) references sFoundation20 which I believe is in c++. – make punk Jun 02 '22 at 20:21
  • @makepunk ah ok, be sure to copy those too. [Possibly relevant](https://forum.qt.io/topic/96091/need-help-compiling-library). please take note of _["Looks like is solved isn't it? As for , that would be a different question, consider posting it separately."](https://meta.stackoverflow.com/a/253829/585968)_. Consider accepting this answer as I did say _"you must place the .DLL(s) in the Assets/Plugins"_. Thanks – MickyD Jun 03 '22 at 03:49
1

This is a small case of Unity's C# script compiling dll. Hope it helps you.

Unity's C# script compiles dll.

First create a DllTest scene.

enter image description here

Create a C# script that will be compiled into a dll, named TestLog, and add a method printout to judge whether the method is successfully called. If it is successful, the console will output "Call TestLog succeeded".

  public void ShirlLog()
  {
      UnityEngine.Debug.Log("TestLog was called successfully")
  }

Create a Text script to test the success of calling the dll.

   public class Test : MonoBehaviour
 {
     private TestLog t;

     void Start()
   {
     //  Add TestLog script component
      t = GameObject.FInd("Main Camera").GetComponent<TestLog>();
      if(t == null)
      {
         t = GameObject.FInd("Main Camera").AddComponent<TestLog>();
      }
      // Call the ShirlLog method of the TestLog script
      TestLog l = new TestLog();
      l.ShirlLog();
   }

 }

Mount the Text script for the camera

enter image description here

The preparations are completed, and then the dll file is compiled.

enter image description here

enter image description here

Click to generate an .asmdef file, change it to the name you want, and change it to TestLog here. At this time, the dll file will be generated in the following directory.

enter image description here

Cut the file under Plugins.

enter image description here

Delete the C# script compiled into dll and prepare for testing

Running the project found that the TestLog script was automatically mounted on the Main Camera object; the ShirlLog of the TestLog script was successfully called, and the output "TestLog was called successfully" was output.

That's because the Test script provides two tests.

The first is to test mount the C# script in the dll to the game object:

 private TestLog t;
 t = GameObject.Find("Main Camera").GetComponent<TestLog>();
 if(t == null)
 {
     t = GameObject.Find("Main Camera").AddComponent<TestLog>();
 }

The second test is to test the success of calling a method in a class compiled into a dll.

 private TestLog t;
 TestLog l = new TestLog();
 l.ShirlLog();
Housheng-MSFT
  • 263
  • 1
  • 7
  • That is a [Assembly Definition Asset (ASMDEF)](https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html#reference-precompiled-assembly) **not** a DLL that is created external to Unity. The OP clearly states _"I know the DLL works because I've been able to compile it outside of unity...The DLL is in .NETframework 4.6.1,"_ - the first clearly stating it is external and the .NET Framework reference is only something you need to worry about for pre-compiled, external DLLs. There's no such setting in ASMDEFs. The latter must be made in Unity too so there is no external – MickyD Jun 01 '22 at 08:26
  • @MickyD Sorry, I misunderstood the meaning of op – Housheng-MSFT Jun 01 '22 at 08:31
  • Not a problem good sir. Don't get me wrong, ASMDEFs have their place too. I use them everywhere where I don't have external .NET DLLs of my own. Generally you commit the ASMDEF to source control, the resulting DLL is incidental – MickyD Jun 01 '22 at 08:36
  • @MickyD Thanks for your reminder. I have gained a lot – Housheng-MSFT Jun 01 '22 at 09:06
  • Thanks for this really detailed answer! I'm sorry it doesn't quite address my issues. – make punk Jun 01 '22 at 20:46