Coder Social home page Coder Social logo

ok-unity3d-sdk's Introduction

Odnoklassniki Unity SDK

This SDK allows you to connect your Unity Android and iOS with Odnoklassniki.

Note: This version Odnoklassniki Unity SDK only supports Unity 5.X+. Check unity4x branch for older version support

Application Requirements

An application registered within OK platform should have:

  1. Target platform checked (like ANDROID or IOS)
  2. EXTERNAL platform checked
  3. Client OAUTH checkbox checked
  4. A VALUABLE_ACCESS permission being checked or requested
  5. (recommended) LONG_ACCESS_TOKEN permission requested from api-support in order to be able to use tokens with long ttl
  6. A redirect url configured in application settings including okauth://ok12345 where 12345 is your application id on OK platform

Setup

  1. Fill in application parameters in OdnoklassnikiSettings.asset
  2. Add the following entries to your android.manifest
  • within 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="INSERT_OK_APPLICATION_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>

where INSERT_OK_APPLICATION_ID should be in format: ok1234567890

  • within manifest tag
<activity android:name="ru.odnoklassniki.unity.auth.AppAuthorization"/>

Initialization

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

Authorization

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

There are 2 types of authorization based on whether native Odnoklassniki application (AppStore / Google Play) is installed: authorization via native application & authorization via webview.

Once authorized, you will receive an access token lasting for 30 minutes

OK.AccessToken

If you authorized via native Odnoklassniki application, you will also receive refresh token lasting for 30 days, which can be used to revalidate access token

OK.IsRefreshTokenValid

You need to take care of refreshing the access token

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

Using widgets

Invite Widget

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

Suggest Widget

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

Photo Upload Widget

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

Publish Widget

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

Handling errors

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
    }
}

F.A.Q.

Is there a convenient way to see if Odnoklassniki widget is shown?

OKWidgets.HasActiveWidget() method does that.

How can I select specific canvas for widgets if I am using multiple canvases?

You can explicitly specify canvas to use with OKWidgets.canvas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.