Initialize HealthBridge correctly
HealthBridge initialization is platform-specific. Android player builds require
the HealthBridgeInitializer MonoBehaviour because Health Connect needs the
current Android Activity and lifecycle.
Editor and iOS
Section titled “Editor and iOS”using HealthBridge;using UnityEngine;
public sealed class HealthBridgeBootstrap : MonoBehaviour{ private async void Start() { bool ready = await HealthBridgeService.AutoInitializeAsync( HealthBridgeSettings.Instance);
if (!ready) Debug.LogWarning("HealthBridge is not ready."); }}On iOS, you may call PrewarmAsync before a user-facing permission flow:
await HealthBridgeService.PrewarmAsync(HealthBridgeSettings.Instance);Prewarming prepares HealthKit native work. It does not display the HealthKit authorization sheet.
Android
Section titled “Android”- Add a GameObject to the first scene that uses health features.
- Add
HealthBridgeInitializer. - Assign the intended
HealthBridgeSettingsasset. - Enable Initialize On Awake, or call
await initializer.InitializeAsync()from your own flow. - Keep the initializer alive for the Android lifecycle it owns.
HealthBridgeService.AutoInitializeAsync intentionally returns false in an
Android player. Do not use it as an Android fallback.
Cross-platform component pattern
Section titled “Cross-platform component pattern”using System.Threading.Tasks;using HealthBridge;using UnityEngine;
public sealed class HealthBridgeBootstrap : MonoBehaviour{ [SerializeField] private HealthBridgeInitializer initializer;
public async Task<bool> InitializeAsync() { if (initializer != null) return await initializer.InitializeAsync();
return await HealthBridgeService.AutoInitializeAsync( HealthBridgeSettings.Instance); }}Lifecycle rules
Section titled “Lifecycle rules”- Never call
.Resultor.Wait()on HealthBridge tasks from the Unity main thread. - Guard operations with
HealthBridgeService.IsReady. - Subscribe to static events in
OnEnableand unsubscribe inOnDisable. - Treat initialization failure as recoverable and keep unrelated gameplay usable.
Verify
Section titled “Verify”In Play Mode, initialization should succeed with the editor provider. On
Android, verify the initializer component—not AutoInitializeAsync—owns the
request.
