WiimoteLib & Unity ?


I'd like to know if it should be possible to use WiimoteLib, because I tried unsuccessfully.

I have the Unity standard edition, and WiimoteLib is a C# .NET dll.

I made this script to test :

using UnityEngine;
using System.Collections;
using WiimoteLib;

public class NewBehaviourScript : MonoBehaviour 

private Wiimote wm;

// Use this for initialization
void Start () {
	wm = new Wiimote();
	try { 
	catch ( System.Exception e )
		Debug.Log( e.ToString() );
	wm.SetLEDs( true, true, true, false );

And anyhow it always return me these exceptions :

System.IO.IOException: Invalid handle. at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync, Boolean noBuffering) [0x00000] at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync) [0x00000] at System.IO.FileStream..ctor (Microsoft.Win32.SafeHandles.SafeFileHandle handle, FileAccess access, Int32 bufferSize, Boolean isAsync) [0x00000] at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (Microsoft.Win32.SafeHandles.SafeFileHandle,System.IO.FileAccess,int,bool) at WiimoteLib.Wiimote.OpenWiimoteDeviceHandle (System.String devicePath) [0x00000] at WiimoteLib.Wiimote.WiimoteFound (System.String devicePath) [0x00000] at WiimoteLib.Wiimote.FindWiimote (WiimoteLib.WiimoteFoundDelegate wiimoteFound) [0x00000] at WiimoteLib.Wiimote.Connect () [0x00000] at NewBehaviourScript.Start () [0x0000b] in D:\Sonny_Unity_Temp\Assets\Test\NewBehaviourScript.cs:14 UnityEngine.Debug:Log(Object) NewBehaviourScript:Start() (at Assets\Test\NewBehaviourScript.cs:18)


Was Win32.SafeHandles the issue? The fact that Unity takes control over HID devices is OK, but for this particular case its just bad. I tried Uniwii, but behavior is still buggy and the API is still limited, which is a pity. Named Pipes doesn't work, so I reckon the only possible solution would be SQLlite, but that is not really elegant. Oh, well if it works it works.

Hi Guys,

I wrote Brian Peek, and his oppinion from the data available in this thread:

""" Reading through that thread, it appears Unity takes over HID devices and there's some kind of incompatibility with SafeHandles. Without a major rewrite of the library, there would be no way to get that to work. It may not be possible at all.


Brian """

Can someone inside Unity confirm this?

Kind regards

Jesper Taxbl

Not a direct answer to your question (never tried the WiimoteLib), but there is the UniWii plugin for accessing the WiiMote in Unity. However, that is a native C++ plugin, so you won't be able to use it in web players.

Just guessing, but all those [0x00000]'s look like something's not initialized correctly to me. Haven't used or heard of WiimoteLib, but if it's a .net dll it should work.

Yep I thought the same thing all these zeros don't look good to me. Yeah it's a .NET dll, I'll try to check if my code is really good. Thks

I checked and the code is good. But I saw that the source code of WiimoteLib and it is using Microsoft.Win32.SafeHandles classes, do you think this is it the problem ? Maybe I should modify the library to use generic classes and be able to be compiled on Mono ?

Mono doesn’t support the concept of safe handles well yet and that’s the root of the problem. Another library which interfaces using native C like functions in a DLL might be a better option to use actually.

Problem you get is that Monodevelop version of FileStream is not finished even today so you don’t have Microsoft.NET FileSteam 100% functionality as in Wiimotelib. Uniwii is not free, you need paid version of Unity and with .dll you are stuck with Win. Go here for pure C# version OS not dependent