Unity SDK

Github repository: ok-unity3d-sdk

Unity SDK allows to deeply integrate Android and iOS applications that are using Unity with OK

Before using Unity SDK you need to be sure everything is configured correctly for your app:

  • iOS or/and Android platform is added to your app’s platforms list;
  • client authorization is enabled;
  • redirect_uri okauth://ok{APP_ID} or ok{APP_ID}://authorize (depending on selected platform) is added to redirect_uri’s list;
  • application has all required API permissions. It is recommended to have LONG_ACCESS_TOKEN and VALUABLE_ACCESS permissions enabled.

Installation

  1. Depending on Unity version (4 or 5) вyou need to copy all required DLL- and meta-files and delete all other files (odnoklassniki_unity4.dll, odnoklassniki_unity4.dll.meta / odnoklassiki_unity5.dll, odnoklassniki_unity5.dll.meta from Assets/Plugins). Make sure that DLL’s GUID is unchangeable, because in case of has been changed enabled prebuffs could be broken. Make sure that this must be done before opening a project in Unity.
  2. Fill application properties in OdnoklassnikiSettings.asset file.
  3. Add following text to android.manifest file:
  • with application tag
<activity android:name="ru.odnoklassniki.unity.OKAndroidPlugin"
    android:label="@string/app_name"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    <intent-filter>
        <data android:scheme="okauth" android:host="{APP_ID}" />
    </intent-filter>
    <meta-data android:name="android.app.lib_name" android:value="unity" />
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<activity android:name="ru.odnoklassniki.unity.webview.OKWVActivity"
    android:label="@string/app_name"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize"
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    <meta-data android:name="android.app.lib_name" android:value="unity" />
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
  • with manifest tag
<activity android:name="ru.odnoklassniki.unity.auth.AppAuthorization"/>

Initialisation

OK.Init(success =>
{
    if (success) {
        //Proceed to authorization
    }
});

Authorization

OK.Auth(success =>
{
    if (success) {
        //Authorization successful, you can now use Odnoklassniki API
    }
});

Two ways to authorize in app via OK are possible at the moment: with installed OK app (Android / iOS) or with WebView.

After authorization process is finished user’s session token can be used for a limited time (30 minutes without LONG_ACCESS_TOKEN permission, 30 days when this permission is enabled):

OK.AccessToken

If authorization was proceeded through official OK application, you will get refresh_token that allows you to get a new access_token for up to 30 days:

OK.IsRefreshTokenValid

To get a working access_token you cab use this code:

if (OK.isInitialized && OK.AccessTokenExpiresAt < DateTime.Now) {
    if (OK.IsRefreshTokenValid) {
        OK.RefreshAccessToken(success =>
        {
            //Token refreshed
        });
    } else {
        OK.RefreshOAuth(success => {
            //Token refreshed
        }
    }
}

Widgets

*Invite widget (OpenInviteDialog)

OK.OpenInviteDialog(response => {
    //Will be called after Invite API call
}, "Invite Message");

Suggest widget (OpenSuggestDialog)

OK.OpenSuggestDialog(response => {
    //Will be called after Suggest API call
}, "Suggest Message");

Photo upload widget (OpenPhotoDialog)

OK.OpenPhotoDialog(response => {
    //Will be called after Upload API call
}, texture, "Description");

Publish widget (OpenPublishDialog)

OK.OpenPublishDialog(response => {
    //Will be called after Publish API call
    }, new List<OKMedia>()
    {
        OKMedia.Photo(texture),
        OKMedia.Text("Description")
    });

Error handling

response => {
    if (response.Object != null && response.Object.ContainsKey("error_code")) {
        string errorCode = response.Object["error_code"].ToString();
        string errorMessage = response.Object["error_msg"].ToString();
        //Debug.Log or show alert?
    } else {
        //Success
    }
}