How do I Edit Moga Plugin to Work with Unity 5

I tried the plug in with Unity 4 as a test and it worked successfully but I get a message when I import into Unity 5 “this plugin is using obsolete APIs”

I got this warning as well but just clicked the “ok” (or whatever it’s called) button and had Unity do its auto-update procedure for updating assets.

After this I deleted the Windows Phone stuff in the Assets/Plugins folder as it was throwing a bunch of errors and I only care about supporting Android for my current project. I expect the issues with Windows Phone are fixable if they matter to you. At this point I was able to build and run the test scene successfully on an Android device but the controller wasn’t being recognized.

The first issue I had was that I needed to installed the Moga Pivot app and sync the controller to the device. Once I did this…still no joy. So I dug into a bunch of Google results and eventually found this.

I won’t completely regurgitate the post here but the gist is that APIs on the Android side of things changed which broke Moga support in Unity. Unfortunately the code in question is inside a compiled jar and would be a pain to update, however, the article describes a fix on the C# side that fixed everything for me and got the Moga working with my Android device in a Unity app.

The solution is to replace the init() method in Moga_Controller.cs with the following:

`public bool init()
{
//return mController.Call(“init”);
bool mIsBound = mController.Get(“mIsBound”);

    // We're going to manually initialize the moga controller due to MOGA's method not working on Lollipop.
    Debug.Log("MOGA manual lollipop initialization!");

    if (!mIsBound)
    {
        AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", "com.bda.controller.IControllerService");

        // Set the intent to be explicit to fix Moga not supporting lollipop.
        intent.Call<AndroidJavaObject>("setPackage", "com.bda.pivot.mogapgp");

        AndroidJavaObject mContext = mController.Get<AndroidJavaObject>("mContext");

        mContext.Call<AndroidJavaObject>("startService", intent);

        bool result = mContext.Call<bool>("bindService", intent, mController.Get<AndroidJavaObject>("mServiceConnection"), 1);
        mController.Set("mIsBound", true);
        mIsBound = true;
        Debug.Log("MOGA initialization succeeded!");
    }

    return mIsBound;
}`

For reference I’m using Unity 5.2.0f3 Personal (on a Mac). My controller is the Moga Pocket. My Android device is a Samsung Galaxy S5 running Android 5.0.