Search Unity

Question Help needed to import XBee C# code library

Discussion in 'Scripting' started by m456arcus, Sep 24, 2021.

  1. m456arcus

    m456arcus

    Joined:
    Oct 6, 2020
    Posts:
    3
    I've been trying to import a C# library (not sure if it would be called a library or API, I'll stick with library for now) into a Unity project and have been hit with a number of code errors. I've tried a couple different ways which I will try to detail, but here is the library in question:

    https://github.com/digidotcom/xbee-csharp

    XBee is a line of RF modules from Digi which allow wireless communication between devices. I am in the process of learning C# and playing around with an Android app which would connect to one of these XBee modules. Unfortunately the documentation linked on that page only covers the Xamarin side of the library.

    It would be a great help if someone more experienced than I could download the library and show me how you'd use it in a Unity project. Essentially (my understanding is), the namespaces and classes within need to be accessible, so that I can interact with these XBee RF modules from within my own scripts. Here's what I've tried so far (separately, i.e. cleared out one solution before trying the next):

    1. Copied the contents into the Assets folder. Resulted in a large number of code errors.

    2. Copied just the XBeeLibrary.Core folder into the Assets folder, and later changed to Assets/Plugins. I believe that only this Core folder is needed. Unity complains that there are duplicate .dll files: SRP.dll (x2) and XBeeLibrary.Core.dll (x2). When one of each is renamed or deleted, Unity shows many errors of missing references, etc., regardless of which is renamed/deleted. I believe when I did this and edited my scripts in Visual Studio, I actually could reference XBee namespaces and use its classes, but Unity would not build.

    3. Following instructions here: https://docs.unity3d.com/Manual/UsingDLL.html I opened the library in Visual Studio (opened XBeeLibrary.Core.csproj), added references to all the Unity .dll files, built out XBeeLibrary.Core.dll, and added only this to Assets/Plugins. In the Project browser I can select the .dll as a plugin, but there is no foldout triangle with all the library classes beneath as is shown in the above link. I am not sure if the intention is that the .dll contains the whole library, or if it is just a reference/pointer file.

    I'm sure I tried more than that but it took hours and now I can't quite remember. I looked into Unity's core files through the Solution Explorer and tried to add a reference through there, but I think that would involve adding this library to Unity's core engine, rather than just to this project, and I'm sure that's not smart. Didn't get far with that. Also, I know the author of the library said that it is built for .NET 2.0 (https://github.com/digidotcom/xbee-csharp/issues/3), which is what Unity's API compatibility level is set to.

    If I need to bring in individual .cs files from the library into the project's Assets folder, I can do that as I likely won't need them all, but I think that would require bringing in all their dependencies which would probably be tricky.

    Please let me know if I can provide any more information. I sincerely appreciate any help you can provide, and I will try to be quick to reply. Thank you!

    Marcus
     
    Last edited: Sep 24, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    First find out if it is compatible with the specific level of C# language syntax and the .NET library supported by Unity.

    If you are unable to determine this from the package, you may need to start digging into the specifics of the errors, see if they give you any clues. The library may also require additional libraries to function, and those should also be specified in the XBee library.

    Dependencies are dependencies, either via DLL or via C#, those requirements won't change meaningfully.
     
  3. m456arcus

    m456arcus

    Joined:
    Oct 6, 2020
    Posts:
    3
    Thanks for your quick reply Kurt, I've just added to the original post that the library is built for .NET 2.0. Not sure about the specific C# language syntax, I didn't know there were different types.

    Before looking at specific errors, in general, what is the correct way to bring this C# library into a Unity project?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Up to you really, DLL or source. I like source because invariably there are errors / bugs in the library and it is nice to be able to instrument and modify the source trivially to gain insight into or to even solve your issues.

    The most-ideal way to bring stuff in is to get it from the Unity asset store as a .unitypackage or else nowadays as a Unity Package Manager download, and have someone else already vet that it works with Unity. That's the hard part you still have to do.

    Unfortunately there's no easy way to look at docs (which are often out of date) and reason if something WILL work, whereas you can often reason that something WON'T work based on requirements. Software kinda needs to work 100% before it can work at all.
     
  5. m456arcus

    m456arcus

    Joined:
    Oct 6, 2020
    Posts:
    3
    Understood and thanks very much for your help so far Kurt. Bear with me as I work my way through.

    So I copied the XBeeLibrary.Core folder into Assets/Plugins. The XBeeLibrary.Core.dll file was not present so I opened XBeeLibrary.Core.csproj in Visual Studio and built it (did I need to add references before building?). Here are the console errors in Unity:

    Multiple precompiled assemblies with the same name XBeeLibrary.Core.dll included or the current platform. Only one assembly with the same name is allowed per platform. (Assets/Plugins/XBeeLibrary.Core/bin/Debug/netstandard2.0/XBeeLibrary.Core.dll)
    Another of the same but with:
    (Assets/Plugins/XBeeLibrary.Core/obj/Debug/netstandard2.0/XBeeLibrary.Core.dll)
    Do I delete one of these, and if so, which one?

    Same thing with:
    (Assets/Plugins/XBeeLibrary.Core/bin/Debug/netstandard2.0/SRP.dll) and
    (Assets/Plugins/XBeeLibrary.Core/libs/SRP.dll)

    There is also:
    Assembly 'Assets/Plugins/XBeeLibrary.Core/bin/Debug/netstandard2.0/XBeeLibrary.Core.dll' will not be loaded due to errors:
    Unable to resolve reference 'Common.Logging.Core'. Is the assembly missing or incompatible with the current platform?

    And if I rename/delete the duplicates, I get many errors for missing type/namespaces "Common" and "ILog" - do you know if these are part of .NET or are they specific to this XBee library?
     
  6. adielfernandez

    adielfernandez

    Joined:
    Apr 26, 2021
    Posts:
    4
    Hey Marcus! I'm running into a very similar situation (pretty much the exact same error messages as you), except with a different library I'm working with (Spinnaker SDK for FLIR cameras).

    Were you able to resolve the errors and get the XBee library working?