How can I reference Grasshopper.dll assembly in Unity?

Hi,

I’m trying to integrate Rhino.Inside® into a Unity3D application. I have used the Sample project. But I found I can’t use Grasshopper API if I just copied the “Grasshopper.dll” into Unity Asset folder along with “RhinoCommon” dll. The “Grasshopper.dll” can not be loaded. I had following message:


Assembly 'Assets/Standard Assets/RhinoInside/Grasshopper.dll' will not be loaded due to errors:
Unable to resolve reference 'Microsoft.VisualBasic'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Eto'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Mono.Cecil'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'GH_Util'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'QWhale.Editor'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'QWhale.Syntax.Parsers'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Yak'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Eto.Wpf'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Rhino.UI'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'QWhale.Common'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'QWhale.Syntax'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.

I also managed to copy all the files above or use a csc.rsp file, but it doesn’t work at all.

Moved to the Serengeti > Inside category.

I have been experimenting with this and have concluded, along with @kike that this is not a good use case for Rhino.Inside. I went through all of the same attempts as you:

  • Copying the Grasshopper.dll into the project assets folder
  • Copying other dependencies into the folder
  • attempting to use the csc.rsp
  • Using assembly resolving
  • etc…

The idea for all of these attempts, unfortunately, are based on bad assumptions that I had about how Unity dealt with .Net dlls, and by being blinded by the awesome Sample that @kike posted with the help of @LongNguyen.

I go into further detail in this post: https://github.com/mcneel/rhino.inside/issues/121#issuecomment-509997598

The reason this is not a good use case for Rhino.Inside is that the potential solutions which involve Rhino.Inside (described in the link) circumvent the primary benefit of Rhino.Inside, namely having RhinoCommon and Grasshopper’s APIs next to the Unity API.

There are other ‘established’ solutions that we’ve seen in the past, including using networking technology to pass data to Unity. There are a few projects under development from 3rd party devs that address this workflow, including the Speckle client for Unity, and Rhino.Compute + RESTHopper.

1 Like

Thank you for the answer. I have give up the Unity as application.

Though Rhino.Inside.Unity is not a good use case for now, RhinoCommon.Inside.Unity (or RhinoCore.Inside.Unity) is still a great options. Having the abilities to perform advanced modelling operations (especially NURBS) inside Unity app domain is still a big win.

1 Like

Definitely!
It’s also not as bad as when I wrote before. There might be ways to detect when Rhino is running inside an app with a different runtime (as is the case with Unity and Mono). @stevebaer might have some ideas.

@LongNguyen, thanks to @stevebaer, we have a method of calling into the GH dll which Rhino loads. You can check out Sample 2 to see an example of utilizing this functionality.

Specifically, it uses

2 Likes

Dear @fraguada
What is minimally need to load RhinoCommon to Unity?
I tried to reference RhinoCommon.dll from the Rhino7Wip.

Point3d works but PointCloud class gives me this error:
DllNotFoundException: rhcommon_c
Rhino.Geometry.PointCloud…ctor () (at :0)

Hi Petras.

You can try to download the sample Unity project here. It has all the instructions you need:

If you want to set up a brand new Unity project manually. There are two main requirement for using RhinoCommon:

  1. The RhinoCommon.dll has to be copied into the Asset folder of your Unity project
  2. Create a C# Script in your Unity project that tells Unity about the main Rhino System folder (in ProgramFiles), because RhinoCommon.dll requires other dlls in that Rhino System folder to function correctly. You can see a sample of this C# script in the sample Unity project above

As you already noticed, using Point3d was working fine for you because Point3d was defined as a struct in RhinoCommon.dll and this struct does not depend on any other classes from other dlls in the Rhino System folder. So it was working fine (same is true for Vector3d and some other lightweight data types that do not depends on the main Rhino System infrastructure). But other things like Mesh, Brep, PointCloud, etc depends on other dlls (typically developed using C++) inside Rhino System so they won’t work unless you tell Unity about Rhino System

Bests

Long

5 Likes

Thanks this works. I copied RhinoWIP RhinoCommon.dll and the Unity.cs

1 Like

Great! Thanks. Will have a look when I can find some time for myself!

1 Like

Dear @fraguada, @LongNguyen, @stevebaer,

I have just updated Rhino WIP, and there is the problem with Unity and RhinoCommon.
Before it worked fine.

I can reference RhinoCommon as before for calling Rhino datastructures in unity.
The problem:
If I load any .dll that references RhinoCommon from RhinoWIP, that .dll cannot be loaded to Unity.

Is it possible to replicate this error in your computer?

I really need to solve this issue…

I tested simply C# class library with this code that breaks the worfklow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestUnityRhino
{
    public static class Class1
    {
        public static Rhino.Geometry.Mesh GetMesh() {
            Rhino.Geometry.Mesh mesh = new Rhino.Geometry.Mesh();
            return mesh;
        }

     

    }
}

Och :man_facepalming: , when I updated the RhinoWIP, the RhinoCommon.dll in Unity was copied from the last build.
When unity was searching for the Rhino directory it gave the error with mis-matching versions.

You cannot see anywhere this error, until you write some code with rhino in unity in not compiled code.

Hi everyone here!

I’d like to know if I could use RhinoCommon.dll during the runtime in Unity, to use some of this features in the game mode.

Thank you!

To enable RhinoCommon at runtime, if I remember correctly, you need to use the Unity attribute RuntimeInitializeOnLoadMethod (instead of InitializeOnLoad) for the static class Startup (defined inside Assets/Standard Assets/RhinoInside/Unity.cs)

Thank you very much @LongNguyen! I’ll try it!