playMaker

Author Topic: Initialize Unity Gaming Services  (Read 3567 times)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Initialize Unity Gaming Services
« on: April 08, 2023, 07:08:12 PM »
So I'm trying to initialize Unity Gaming Services and am writing my own action for this. For some reason it doesn't seem to work. Any help?

This is my action:
Code: [Select]
using UnityEngine;
using System;
using Unity.Services.Core;
using Unity.Services.Core.Environments;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Unity Engine")]
[Tooltip("Initializes the Unity Gaming Services (UGS) and sends an event when finished.")]
public class InitializeUGS : FsmStateAction
{
        [Tooltip("Event to send once the Unity Gaming Services are initialized.")]
public FsmEvent initializedEvent;

        public ServicesInitializationState initializationState;

public override void Reset()
{
initializedEvent = null;
}

public override void OnEnter()
{
InitializeUGSAsync();
}

        public override void OnUpdate()
        {
            if (initializationState == ServicesInitializationState.Initialized)
            {
                Debug.Log("Unity Gaming Services IS " + initializationState);
                Fsm.Event(initializedEvent);
            }

            if (initializationState == ServicesInitializationState.Initializing)
            {
                Debug.Log("Unity Gaming Services is " + initializationState);
            }

            if (initializationState == ServicesInitializationState.Uninitialized)
            {
                Debug.Log("Unity Gaming Services is " + initializationState);
            }
        }

        async void InitializeUGSAsync()
        {
            try
            {
                var options = new InitializationOptions()
                    .SetEnvironmentName("production");

                await UnityServices.InitializeAsync(options);
            }

            catch (Exception exception)
            {
                Debug.Log("An error occured during initializing the Unity Gaming Services. Reason: " + exception);
            }
           
        }
    }
}

The part on the OnUpdate method is new and I did that to check if the Service is actually initialized. Before I thought it was initialized, but for some reason at a further step that I'm doing (checking for privacy consent) it doesn't work.

Therefore all the debug part which continuously returns "Uninitialized" in the console.

Am I initializing wrong or am I checking wrong for the current state? One of both it must be...

Edit:
I also thought that the await keyword would pretty much... well... wait, until the service has been initialized. So most probably I'm checking wrong for the current state?
« Last Edit: April 08, 2023, 07:38:48 PM by Christoph »

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Re: Initialize Unity Gaming Services
« Reply #1 on: April 08, 2023, 07:50:06 PM »
ok, ChatGPT to the rescue. Got me correct my action to:

Code: [Select]
using UnityEngine;
using System;
using Unity.Services.Core;
using Unity.Services.Core.Environments;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Unity Engine")]
[Tooltip("Initializes the Unity Gaming Services (UGS) and sends an event when finished.")]
public class InitializeUGS : FsmStateAction
{
        [Tooltip("Event to send once the Unity Gaming Services are initialized.")]
public FsmEvent initializedEvent;

public override void Reset()
{
initializedEvent = null;
}

public override void OnEnter()
{
InitializeUGSAsync();
}

        async void InitializeUGSAsync()
        {
            try
            {
                var options = new InitializationOptions()
                    .SetEnvironmentName("production");

                await UnityServices.InitializeAsync(options);
            }

            catch (Exception exception)
            {
                Debug.Log("An error occured during initializing the Unity Gaming Services. Reason: " + exception);
            }
           
        }

        public override void OnUpdate()
        {
            if (UnityServices.State == ServicesInitializationState.Initialized)
            {
                Debug.Log("Unity Gaming Services is " + UnityServices.State);
                Fsm.Event(initializedEvent);
            }
            else if (UnityServices.State == ServicesInitializationState.Initializing)
            {
                Debug.Log("Unity Gaming Services is " + UnityServices.State);
            }
            else if (UnityServices.State == ServicesInitializationState.Uninitialized)
            {
                Debug.Log("Unity Gaming Services is " + UnityServices.State);
            }
        }
    }
}

Now it works and seems it's actually initializing correctly. My debug code was wrong.  :P