To access the SDK, you'll need to have the DLL in your project as an asset, and import it with this statement:
- Code: Select all
import PlayerIOClient;
Now you're ready to access all of Player.IO's functionality. The syntax for calling the library is the same in both languages. Ex:
- Code: Select all
PlayerIOClient.PlayerIO.UnityInit(this);
The big difficulty with Unityscript is calling methods which require callbacks. Most of the pages I saw online suggested using Javascript's "Function" type, but I could never get that to work (though if you have gotten this to work I'd love to hear about it!). Additionally, I don't believe there is any way to code an anonymous inline delegate like you can in C#.
Instead, I found that the best way to create delegates is to go straight to the .Net type and declare them as such:
- Code: Select all
var pioConnectSuccessCallback : System.Delegate;
var pioConnectErrorCallback : System.Delegate;
In order to actually populate these with data that points to our callback functions, we need to call System.Delegate.CreateDelegate():
- Code: Select all
pioConnectSuccessCallback = System.Delegate.CreateDelegate(typeof(PlayerIOClient.Callback.<PlayerIOClient.Client>), this, "PlayerIOConnectSuccess");
pioConnectErrorCallback = System.Delegate.CreateDelegate(typeof(PlayerIOClient.Callback.<PlayerIOClient.PlayerIOError>), this, "PlayerIOConnectFail");
The first argument is the type of the method which will establish our delegate's signature. Using typeof() was the best way I could find to do this. The second argument is the object the method will be called on. So long as your callbacks are in the same class as your Player.IO calls, "this" should be sufficient. The third argument is the name of the method you want to call. Use the name of your callback function with no parentheses or parameters.
Now you can create the actual callback functions themselves:
- Code: Select all
function PlayerIOConnectSuccess(client : Client)
{
Debug.Log("Successfully connected to Player.IO at " + Time.realtimeSinceStartup);
}
function PlayerIOConnectFail(error : PlayerIOError)
{
Debug.Log("Error connecting: " + error.ToString());
}
Utilizing these callbacks is as easy as passing the delegates as arguments to the Player.IO methods (note last two arguments):
- Code: Select all
PlayerIOClient.PlayerIO.Connect(pioGameId, pioConnectionId, pioUserId, pioAuth, pioPartnerId, pioConnectSuccessCallback, pioConnectErrorCallback);
Good luck and happy coding!