UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[IAP] StoreController Connection & Lifecycle Management Revealed

Solution

project architectureoptimizationadministration

Unity 2021.3.x - Unity 6.3.x

Published Fri, Mar 13

Issue

 Developers using Unity IAP 5.0 often encounter uncertainty regarding the StoreController.Connect method, specifically around the necessity of exception handling, the behavior of connection tasks, and whether OnStoreDisconnected facilitates automatic reconnection.

In Unity IAP 5.0, StoreController.Connect handles errors internally, requiring developers to implement manual reconnection logic within the OnStoreDisconnected callback.

Explanation

Avoid wrapping StoreController.Connect in try/catch blocks. The Unity IAP library manages internal exceptions and communicates failures through the IStoreListener.OnInitializeFailed callback instead of throwing standard errors.

  1. Initiate the store link by calling StoreController.Connect.
  2. Handle IStoreListener.OnInitializeFailed to capture errors occurring during the initial handshake.
  3. Implement OnStoreDisconnected to detect when a previously established connection is lost.

Manual reconnection logic must be implemented by the developer. When OnStoreDisconnected is triggered, your script should evaluate the network state and re-invoke StoreController.Connect if the session needs to be restored.

Additional Tips

  • Verify that UnityServices are fully initialized before attempting to call StoreController.Connect.
  • Differentiate between InitializationFailureReason values to determine if an error is transient or requires user account changes.
  • Use a retry counter or exponential back-off when re-invoking StoreController.Connect to prevent excessive API calls during prolonged outages.

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.