Home > Net >  How do you include pre-compiled .NET DLLs in Unity
How do you include pre-compiled .NET DLLs in Unity

Time:06-01

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();

CodePudding user response:

To use plugins/pre-compiled assemblies in Unity (native or .NET), you must place the .DLL(s) in the Assets/Plugins. 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

  • Related