Search Unity

Question How to use UnityEngine namespace in non-Unity library?

Discussion in 'Scripting' started by ptr0x, Nov 28, 2020.

  1. ptr0x

    ptr0x

    Joined:
    Dec 9, 2013
    Posts:
    54
    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.

    I followed the instructions here, however the UnityEngine.dll seems to be hollow. I can't access any of the names in this binary, my project can only see the namespaces declared in the dll but doesn't see any of the real types.

    Let's take the class UnityEngine.Matrix4x4 which is the one I need to use at the moment. How can I have my non-Unity library to use this type?
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    You simply can't use any of those types outside of Unity and you are not allowed either. Though the actual technical issue is that alot of methods of the Matrix4x4 struct is defined in native code in the Unity C++ core. So you simply can not use the UnityEngine.dll without the Unity engine.

    So if you create an actual Unity plugin you can simply compile your library and add the various UnityEngine.dll modules as references. Though as I said the resulting dll would only work inside a Unity project.

    For example those are all the externally defined methods and properties of the Matrix4x4 struct, while this is the pure managed part of that struct. Please note that the code that is provided in this repository has a special license that is "for reference only". So you are not really allowed to use the code provided anywhere. It's there for reference so you better understand how the managed part of Unity works in detail.

    Though if you need your dll to be Unity independent, just write your own structs. I actually once wrote my own Matrix5x5 and Matrix3x3 struct. Most methods are quite trivial. The ones that could cause some trouble would be Inverse and maybe GetDeterminant. There are several ways how to invert a matrix. For smaller matrices using the cofactor matrix. Though for larger matrices (4x4 and larger) this is quite expensive and generally not recommended. Using gaussian elimination is usually faster in those cases.

    If you don't want to write your own basic math types, just try to find some that are completely written in managed code and are not bound to other frameworks. A quick google search brought up this.

    ps:
    The managed plugins documentation you mentioned above is not really up to date. If you use the Untiy hub all the modules that made up the old UnityEngine.dll are located at
    C:\Program Files\Unity\Hub\Editor\YOUR_UNITY_VERSION\Editor\Data\Managed\UnityEngine\


    The most important module is
    UnityEngine.CoreModule.dll
     
    Last edited: Nov 29, 2020
    ProudPumPkin99 and ptr0x like this.
  3. ptr0x

    ptr0x

    Joined:
    Dec 9, 2013
    Posts:
    54
    Okay, I'll study the viability to keep developing wrappers for those types I need (like Matrix4x4).

    Really appreciated your answer, thanks!
     
    ProudPumPkin99 likes this.