Using .NET 4.6 librarieS in unity3d 2017

18-4-2017: Update post for Unity 2017

For a long time it has been a lot of hassle to use existing .NET libraries in Unity3D. Unity3D only supported .NET 2.0/3.5, so more recent libraries (4.0 and up) could not be used without recompiling/rewriting for .NET 2.0/3.5.

Also new C# language constructs (C# 6) could not be used by game developers  Sad smile

At the moment Unity3D is upgrading the Mono version to 4.6. The first beta’s of Unity 2017 with .NET 4.6 support are available for download so let’s take a test drive.

Installation Unity 2017.1.0b1: https://unity3d.com/unity/beta (Released: April 11, 2017)

In the Unity 2017.1.0b1 editor .NET 4.6 is selectable at Edit –> Project settings –> Player –> Other settings –> Configuration

net46

After selecting ‘Experimental (.NET 4.6 Equivalent) Unity has to be restarted  Sad smile 

For the test I’ve created a sample project (https://github.com/bertt/dotnetstandard_and_unity3d) that does the following:

. get a location (latitude, longitude)

. Determine the OpenStreetMap tile on that location using .NET Standard library Tilebelt

. Download and display the tile on a Unity3D Plane GameObject.

Code sample:

===================================================================

void Start()
{
    var coords = new double[] { -84.72, 11.17, -5.62, 61.60 };
    var parent = Tilebelt.BboxToTile(coords);
    StartCoroutine(requestTile(parent));
}

private IEnumerator requestTile(Tile t)
{
    var url = tile2url(t);
    var handler = new DownloadHandlerBuffer();
    var http = new UnityWebRequest(url);
    http.downloadHandler = handler;
    yield return http.Send();

    if (!http.isNetworkError)
    {
        var texture = new Texture2D(10, 5);
        texture.LoadImage(http.downloadHandler.data);
        var tileGO = GameObject.CreatePrimitive(PrimitiveType.Plane);
        var renderer = tileGO.GetComponent<MeshRenderer>();
        renderer.material.mainTexture = texture;
    }
}

private string tile2url(Tile tile)
{
    return $"
https://b.tile.openstreetmap.org/{tile.Z}/{tile.X}/{tile.Y}.png";
}

 

===================================================================

The Tilebelt library (https://github.com/bertt/tilebelt-cs) is a small library with some utility functions for working with the OpenStreetMap tiling scheme. It is compiled for .NET Standard 1.1 with the following dependencies:

  • System.Collections (>= 4.3.0)

  • System.Runtime.Extensions (>= 4.3.0)

  • System.Resources.ResourceManager (>= 4.3.0)

  • System.Runtime (>= 4.3.0)

The dependencies we could get from NuGet or they could be found in the folder C:\Program Files\Unity 2017.1.0b1\Editor\Data\MonoBleedingEdge\lib\mono\unity_aot\Facades

The Tilebelt.dll assembly and dependencies we have to copy manually to the assets folder, unfortunately the NuGet support is not so great yet.

Here a screenshot of the application build for Windows:

image

This is just a small example of using a .NET Standard library in Unity3D. But the concept is working already,now we should be able to use other standard .NET libraries as well Smile

Advertisements

One thought on “Using .NET 4.6 librarieS in unity3d 2017

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s