Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference the ServiceStack library in a Unity C# script?

I've got to make a Unity script to import and export some 3D models. I'm trying to reference Servicestack.Redis from my script so I can talk to redis. It compiles fine but unity won't load the library.

I've copied the dll's from Build/Release/MonoDevelop/SericeStack.Redis.zip into my assets folder within unity, (is that correct?) I just got ServiceStack by cloning https://github.com/ServiceStack/ServiceStack.Redis

When Unity attempts to load the script it says

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 

My code so far is this. It's an editor script. It just makes a window with a button and when the button is clicked it attempts to connect to redis on localhost and get a key

using UnityEngine;
using UnityEditor;
using System.Collections;
using ServiceStack.Redis;

public class MyWindow : EditorWindow
{

    // Add menu item named "My Window" to the Window menu
    [MenuItem("Window/My Window")]
    public static void ShowWindow()
    {
        //Show existing window instance. If one doesn't exist, make one.
        EditorWindow.GetWindow(typeof(MyWindow));
    }

    void OnGUI()
    {
        if (GUILayout.Button("Press to Rotate"))
        {
            ProcessAsset();
        }
    }

    void ProcessAsset()
    {
        using (var client = new RedisClient("localhost"))
        {
            client.Get ("hello");
        }
    }
}

I'm probably just not referencing the library correctly. I'm fairly new to compiled languages.

like image 906
Nathan Avatar asked Nov 03 '22 12:11

Nathan


1 Answers

I had a bug similar to that. But it was without any custom dlls. The reason was because I used Func inside my code. Mono do not have support for Func. I tried to download the source code for the dlls and they use quite a lot of Func. So it's probably the reason you get that error.

like image 79
Lange Avatar answered Nov 08 '22 03:11

Lange