-
Notifications
You must be signed in to change notification settings - Fork 8
Store API Integratıon
You can integrate the GameDistribution Store API in your Unity WebGL projects using JavaScript interoperability. This allows you to access virtual items, currencies, cart actions, and inventory features provided by the GameDistribution Store system.
Ensure your Unity project is targeting WebGL platform.
- Create a GameObject in your scene named
GameDistribution. - Attach the
GameDistribution.csscript to it. - Set your
GAME_KEYin the inspector (You can find it on your GameDistribution dashboard).
In your index.html (or template.html inside WebGLTemplate), include the GameDistribution SDK:
<script type="text/javascript">
window["GD_OPTIONS"] = {
gameId: "your-game-id",
onEvent: function (event) {
console.log("GD Event:", event);
}
};
</script>
<script src="https://html5.api.gamedistribution.com/main.js"></script>We have prepared a Unity plugin that uses DllImport to bridge between Unity C# and the GameDistribution HTML5 SDK.
Here's an example of how the plugin handles store API calls:
public void ExecuteStoreAction()
{
var obj = new ActionData<PayloadData>();
var data = new PayloadData();
obj.actionName = "api.consume";
data.sku = "ss-enhance-shield-5";
data.quantity = 3;
obj.payload = data;
GameDistribution.Instance.ExecuteStoreAction(JsonUtility.ToJson(obj));
}✅ Note: Don't forget to run the game in a WebGL build in a browser. Store actions will not work in the Unity Editor.
GameDistribution plugin includes callback methods for major SDK events:
GameDistribution.OnEvent += OnEvent;
GameDistribution.OnPauseGame += OnPauseGame;
GameDistribution.OnResumeGame += OnResumeGame;
GameDistribution.OnRewardedVideoSuccess += OnRewardedVideoSuccess;You can then define these handlers in your GameManager script:
void OnPauseGame()
{
// Pause your game
}
void OnResumeGame()
{
// Resume your game
}
void OnEvent(string eventData)
{
Debug.Log("Received SDK Event: " + eventData);
}You can invoke the following store actions via your Unity plugin:
// Open Store UI
gdsdk.executeStoreAction({action:'ui.open'})
// Close Store UI
gdsdk.executeStoreAction({action:'ui.close'})
// Get inventory items
gdsdk.executeStoreAction({action:'api.inventoryItems'})
// Buy a product
gdsdk.executeStoreAction({action:'api.buyProduct', payload:{sku:'item-sku', quantity:1}})
// Consume an item
gdsdk.executeStoreAction({action:'api.consume', payload:{sku:'item-sku', quantity:1}})These actions are wrapped in your plugin's ExecuteStoreAction(string json) method.