Advertisement
Guest User

Remote config

a guest
Aug 18th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Unity.Services.RemoteConfig;
  5. using Unity.Services.Authentication;
  6. using Unity.Services.Core;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9.  
  10. public class remoteconfig : MonoBehaviour
  11. {
  12.     public static remoteconfig Instance { get; private set; }
  13.     public struct userAttributes { }
  14.     public struct appAttributes { }
  15.     public bool isMaintenance = false;
  16.     public int test = 1;
  17.  
  18.     async Task InitializeRemoteConfigAsync()
  19.     {
  20.         await UnityServices.InitializeAsync();
  21.  
  22.         if (!AuthenticationService.Instance.IsSignedIn)
  23.         {
  24.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
  25.         }
  26.     }
  27.  
  28.     void Awake()
  29.     {
  30.         if (Instance == null)
  31.         {
  32.             Instance = this;
  33.         }
  34.         else
  35.         {
  36.             Destroy(gameObject);
  37.         }
  38.     }
  39.     async void Start()
  40.     {
  41.  
  42.         if (Utilities.CheckForInternetConnection())
  43.         {
  44.             await InitializeRemoteConfigAsync();
  45.         }
  46.         RemoteConfigService.Instance.FetchConfigs<userAttributes, appAttributes>(new userAttributes(), new appAttributes() { });
  47.         RemoteConfigService.Instance.SetEnvironmentID("");
  48.         RemoteConfigService.Instance.FetchCompleted += setMaintenance;
  49.     }
  50.  
  51.     void setMaintenance(ConfigResponse configResponse)
  52.     {
  53.         switch (configResponse.requestOrigin)
  54.         {
  55.             case ConfigOrigin.Default:
  56.                 //Debug.Log("No settings loaded this session; using default values.");
  57.                 break;
  58.             case ConfigOrigin.Cached:
  59.                 //Debug.Log("No settings loaded this session; using cached values from a previous session.");
  60.                 break;
  61.             case ConfigOrigin.Remote:
  62.                 //Debug.Log("New settings loaded this session; update values accordingly.");
  63.                 isMaintenance = RemoteConfigService.Instance.appConfig.GetBool("maintenance");
  64.                 test = RemoteConfigService.Instance.appConfig.GetInt("test");
  65.                 //Debug.Log(test);
  66.                 Scene currentScene = SceneManager.GetActiveScene();
  67.                 string sceneName = currentScene.name;
  68.                 //Debug.Log(sceneName);
  69.                 if (isMaintenance == true && sceneName != "Maintenance")
  70.                 {
  71.                     SceneManager.LoadScene("Maintenance");
  72.                 }
  73.                 else if (isMaintenance == false && sceneName == "Maintenance")
  74.                 {
  75.                     SceneManager.LoadScene("Menu");
  76.                 }
  77.                 break;
  78.         }
  79.     }
  80.     private void Update()
  81.     {
  82.         RemoteConfigService.Instance.FetchConfigs<userAttributes, appAttributes>(new userAttributes(), new appAttributes() { });
  83.         RemoteConfigService.Instance.SetEnvironmentID("");
  84.         RemoteConfigService.Instance.FetchCompleted += setMaintenance;
  85.     }
  86.     /*private void OnDestroy()
  87.     {
  88.         RemoteConfigService.Instance.FetchCompleted -= setMaintenance;
  89.     }*/
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement