Skip to content

Store API Integratıon

serdarakay edited this page Jun 20, 2025 · 1 revision

🧩 Using GameDistribution Store API in Unity WebGL

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.


✅ 1. Unity Setup

Ensure your Unity project is targeting WebGL platform.

  1. Create a GameObject in your scene named GameDistribution.
  2. Attach the GameDistribution.cs script to it.
  3. Set your GAME_KEY in 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>

⚙️ 2. Example Plugin Code

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:

Consume a Product

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.


🧪 3. SDK Events in Unity

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

📊 4. Some Supported Store Actions

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.


📖 Related Docs

🔗 GameDistribution HTML5 SDK - Store API Usage