1

I have a fresh Unity project and following the instructions in Using .NET 4.x in Unity, i've downloaded Microsoft.ML.Probabilistic.Compiler NuGet package from the gallery and copied the DLL to Assets/Plugins. After this Unity outputs an error message

Assembly 'Assets/Plugins/Microsoft.ML.Probabilistic.Compiler.dll' will not be loaded due to errors: Unable to resolve reference 'Microsoft.ML.Probabilistic'. Is the assembly missing or incompatible with the current platform?

I downloaded and copied the missing DLL and every following missing DLL dependency

  • Microsoft.ML.Probabilistic.dll
  • System.CodeDom.dll
  • Microsoft.CodeAnalysis.CSharp.dll
  • Microsoft.CodeAnalysis.dll
  • System.Collections.Immutable.dll
  • System.Reflection.Metadata.dll

Unity showed no more errors after adding the System.Reflection.Metadata.dll.

When i add

using Microsoft.ML.Probabilistic;
using Microsoft.ML.Probabilistic.Compiler;

to a script, the project builds fine in both Unity and Visual Studio Community, but when i attempt to use the libraries, namely the Microsoft.ML.Probabilistic.Compiler.Variable<T> class

using UnityEngine;
using Microsoft.ML.Probabilistic;
using Microsoft.ML.Probabilistic.Compiler;

public class Main : MonoBehaviour {
    void Start () {
        Variable<bool> firstCoin = Variable.Bernoulli (0.5);
    }
}

both Unity and Visual Studio fail with

Assets/Scripts/Main.cs(7,3): error CS0246: The type or namespace name 'Variable<>' could not be found (are you missing a using directive or an assembly reference?)

and

Assets/Scripts/Main.cs(7,30): error CS0103: The name 'Variable' does not exist in the current context

Adding -r:Microsoft.ML.Probabilistic.Compiler.dll to Assets/mcs.rsp file generates an error

Microsoft (R) Visual C# Compiler version 2.9.1.65535 (9d34608e) Copyright (C) Microsoft Corporation. All rights reserved.

error CS0006: Metadata file 'Microsoft.ML.Probabilistic.Compiler.dll' could not be found

The project configuration is

  • Unity 2018.3.10f1
  • Visual Studio for Mac Community
  • Build Settings/Platform: iOS
  • Player Settings/Scripting Runtime Version: .NET 4.x Equivalent

it makes no difference whether i set

  • Player Settings/Api Compatibility Level: .NET 4.x

or

  • Player Settings/Api Compatibility Level: .NET Standard 2.0

Other settings are left to their defaults.

I haven't added the DLLs to the vs-project in Visual Studio as the guide says

Visual Studio regenerates .csproj and .sln files for Unity projects each time they're opened. As a result, you cannot add assembly references directly in Visual Studio because they'll be lost upon reopening the project. Instead, a special text file named mcs.rsp must be used:

Create a new text file named mcs.rsp in your Unity project's root Assets directory.

On the first line in the empty text file, enter: -r:System.Net.Http.dll and then save the file. You can replace "System.Net.Http.dll" with any included assembly that might be missing a reference.

Restart the Unity editor.

Matti Jokipii
  • 561
  • 1
  • 6
  • 20

1 Answers1

1

Variable<bool> Isn't part of either Probabilistic or Probabilistic.Compiler

Instead you need to include using Microsoft.ML.Probabilistic.Models;, of which Variable<T> is part.

using UnityEngine;
using Microsoft.ML.Probabilistic.Models;//we need Probabilistic.Models

public class SomeScript : MonoBehaviour
{
    void Start()
    {
        Variable<bool> firstCoin = Variable.Bernoulli(0.5);
    }
}

Tested this with unity 2018.3 and 2019.1, both with .net 4.x runtime, and it builds fine (Although on Windows, it shouldn't make a difference).

... and copied the DLL to Assets/Plugins. After this Unity outputs an error message

I personally didn't have to do this to get it to work. I downloaded the package from NuGet straight into the default directory (project/Packages/) using Install-Package Microsoft.ML.Probabilistic.Compiler -Version 0.3.1810.501. It also didn't require me to download any additional DLL's.

Remy
  • 4,843
  • 5
  • 30
  • 60
  • Yes, yes, that's it! Thank you so much! Unfortunately Infer.NET model builder doesn't work with IL2CPP AOT compiler on iOS. ATM i see this impossible. Nonetheless your answer solved the problem i thought i had with the installation and Infer.NET does work in the Unity editor. – Matti Jokipii May 18 '19 at 08:51