VK Unity SDK - tools wich uses VK API, implements many features to integrate your application with VK, works under Unity and created for developers who wants to quickly integrate applications. (it uses .NET 4.6).
- Android
- WebGL
For using VK Unity SDK you must install Unity since 2017.1 version. Download the latest version from official website. You also need to download VK Unity SDK. Basic unity application included in .unitypackage file. You can start it from Unity. This package consists of:
- VK.Unity library which implemented tools for management API methods.
- VK.Unity.Editor library which provides interaction with Unity Editor.
- Open sans font
- Plugins for correct interaction between platforms
- Executable .cs file
When you run the .unitypackage file, Unity will ask you to import mentioned files, you might agree this. Header menu will be include VK item, inside this item you can see Edit settings button and when you clicked on it, settings menu will be visible for you. Inside this menu you can set API Version
and Application ID
.
MainMenu scene exists Inside VKSDK/Examples folder, you need to move this scene into your project. Also source code of the project you can find inside Scripts folder. Elements of user interface will be created automatically in programming code for MainMenu scene therefore you can just build application for platform.
Application ID
from Unity Editor which you set must be equal with IFrame application id. Our methods available only inside VK application page.
Field | Available on Android | Available on WebGL |
---|---|---|
IsLoggedIn | + | - |
IsInitialized | + | + |
OnAccessTokenChanged | + | - |
AppId | + | + |
SDKVersion | + | + |
AccessToken | + | - |
UserId | + | - |
Function | Available on Android | Available on WebGL |
---|---|---|
AddCallback | - | + |
API | + | + |
GetExtraData | + | + |
Init | + | + |
Login | + | - |
Logout | + | - |
RemoveCallback | - | + |
public static bool IsLoggedIn
This field contains true
if method Login executed successfully and false
if not.
Example:
Enable GUI when a script is enabled. It will be done if Login successfully finished.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
void Start() {
GUI.enabled = VKSDK.IsLoggedIn;
}
}
public static bool IsInitialized
Like a IsLoggedIn this filed contains true
if Init method successfully finished.
Example:
Getting friends list using API method of sdk if initialization complete successfully. If you have not called Init method before, then will be warning message.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
protected void FriendsFromVK() {
if (VKSDK.IsInitialized) {
VKSDK.API("friends.get", new Dictionary<string, string>() { { "order", "name" } } , (result) =>
{
Debug.Log("Your VK friends: " + result.ToString());
});
}
else
{
Debug.Warn("You must to run VKSDK.Init() first");
}
}
}
public static Action<AccessToken> OnAccessTokenChanged
public static long AppId
This field contains application id you set so in unity editor. Example: For example you can create link to your VK IFrame application.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
private string AppLink() {
return "https://vk.com/app" + VKSDK.AppId;
}
}
public static string SDKVersion
SDKVersion contains current version of VK Unity SDK. Example: Method writes to console information about developer and used software.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
private static readonly string STARTING_YEAR = "2018";
private string Credits() {
Debug.Log("Application created using VK Unity SDK v" + VKSDK.SDKVersion);
Debug.Log("Supporting by me since " + STARTING_YEAR);
}
}
public static AccessToken AccessToken
Returns you current AccessToken
Object. This object contains fields TokenString
, ExpiresIn
, UserId
. Some platforms uses access tokens to identify your application, this object stores your current access token. More about access tokens here.
Example:
Our method show information about requested access token.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
private string AccessTokenInfo(AccessToken at) {
Debug.Log("Token info:");
Debug.Log("Owner`s userId: " + at.UserId);
Debug.Log("Lifetime to: " + at.ExpiresIn);
}
}
public static int UserId
UserId contains UserId from current AccessToken
object. Look at AccessToken to see example.
public static void AddCallback(string eventName, Action<APICallResponse> callback = null)
Look at the VK JavaScript SDK it's supports adding callbacks and removing callbacks. List of available events here. Supporting WebGL in Unity SDK based on our JavaScript SDK. This method adding your function as callback of one of some events, and call it when event occurs.
Example:
Here created function, wich adds callback on onSettingsChanged
, stores callback id and gives info about event on console.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
private string callbackListenerId = "";
#if UNITY_WEBGL
private void AddCallback()
{
VKSDK.AddCallback("onSettingsChanged", (result) => {
Debug.Log("Event onSettingsChanged with result: " + result.ToString());
callbackListenerId = result.callbackId;
});
}
#endif
}
public static void API(string method, IDictionary<string, string> queryParams, Action<APICallResponse> callback = null)
This method make request to VK API. Full list of methods available here. Example: You may check IsInitialized which using API method.
public static string GetExtraData(string key)
public static void Init(VKInitParams initParams, Action initializedCallback = null)
public static void Init(Action initializedCallback = null)
This method initialize your application and makes available every SDK methods to execute. Example:
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
void Start() {
VKSDK.Init();
}
}
public static void Login(IEnumerable<Scope> scope, Action<AuthResponse> callback = null)
public static void Logout()
public static void RemoveCallback(string eventName, string callbackId)
Example: This function removes callback which has been added earlier by AddCallback function using callback id.
using UnityEngine;
using VK.Unity;
public class ExampleClass : MonoBehaviour {
private string callbackListenerId = "1";
#if UNITY_WEBGL
private void AddCallback()
{
VKSDK.RemoveCallback("onSettingsChanged", callbackListenerId);
}
#endif
}