Using .NET 4.6 librarieS in Unity3d 2018

 

16-1-2018: Updated post for Unity 2018

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 – .NET Standard 2.0. The first beta’s of Unity 2018 with .NET 4.6 support are available for download so let’s take a test drive.

Installation Unity 2018.1.0b2: https://unity3d.com/unity/beta (Released: Released: January 10, 2018)

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

unity_standard.png

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 Tilebelt.dll assembly we have to copy manually to the assets folder, unfortunately the NuGet support is not so great yet. Note: In previous Unity3D versions(2017), we had to copy the dependencies also).

Here a screenshot of the application build for Windows:

image

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

Advertisements

2 thoughts on “Using .NET 4.6 librarieS in Unity3d 2018

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s