Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Failing to get remote config data

Discussion in 'Unity Remote Config' started by Clicksurfer, Nov 23, 2020.

  1. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    Hi there,

    I recently migrated from Unity Remote Settings to Unity Remote Config in order to avoid the config limit.

    I only have the two initial environments (Release and Development), and to both of them I imported settings from my Remote Settings as per Unity's own guide. Neither have Rules as I want this config to be available to all players.
    I was able to Pull the config to Unity through the Remote Config window.

    For some reason, however, I am unable to pull the data from Unity Config. The request is sent and I believe it returns successfully, but it's as if none of my configs are present - when I try to get an int, for example, I always have 0 returned. From what I understand this is the behavior when a key cannot be found.

    I wrote this class to manage interactions with Remote Config. This class is attached to an otherwise empty gameobject in the start scene:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.RemoteConfig;
    5. using System;
    6.  
    7. public class RemoteConfigManager : MonoBehaviour
    8. {
    9.     public static RemoteConfigManager RemoteConfig;
    10.  
    11.     private void Awake()
    12.     {
    13.         if (RemoteConfig != null && RemoteConfig != this)
    14.         {
    15.             Destroy(this.gameObject);
    16.             return;
    17.         }
    18.         RemoteConfig = this;
    19.         DontDestroyOnLoad(this);
    20.  
    21.         ConfigManager.FetchCompleted += ApplyRemoteSettings;
    22.     }
    23.  
    24.     void ApplyRemoteSettings(ConfigResponse configResponse)
    25.     {
    26.         // Conditionally update settings, depending on the response's origin:
    27.         switch (configResponse.requestOrigin)
    28.         {
    29.             case ConfigOrigin.Default:
    30.                 Debug.Log("No settings loaded this session; using default values.");
    31.                 break;
    32.             case ConfigOrigin.Cached:
    33.                 Debug.Log("No settings loaded this session; using cached values from a previous session.");
    34.                 break;
    35.             case ConfigOrigin.Remote:
    36.                 Debug.Log("New settings loaded this session; update values accordingly.");
    37.                 break;
    38.         }
    39.     }
    40.  
    41.     public int GetInt(string key, int default_value)
    42.     {
    43.         try
    44.         {
    45.             return ConfigManager.appConfig.GetInt(key);
    46.         }
    47.         catch
    48.         {
    49.             return default_value;
    50.         }
    51.     }
    52.  
    53.     public float GetFloat(string key, float default_value)
    54.     {
    55.         try
    56.         {
    57.             return ConfigManager.appConfig.GetFloat(key);
    58.         }
    59.         catch
    60.         {
    61.             return default_value;
    62.         }
    63.     }
    64.  
    65.     public string GetString(string key, string default_value)
    66.     {
    67.         try
    68.         {
    69.             return ConfigManager.appConfig.GetString(key);
    70.         }
    71.         catch
    72.         {
    73.             return default_value;
    74.         }
    75.     }
    76.  
    77.     public bool GetBool(string key, bool default_value)
    78.     {
    79.         try
    80.         {
    81.             return ConfigManager.appConfig.GetBool(key);
    82.         }
    83.         catch
    84.         {
    85.             return default_value;
    86.         }
    87.     }
    88. }
    Other scripts throughout the game can reference the static variable RemoteConfig to get data when they need it:

    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         if (globalFillMultiplier == -1f)
    4.             globalFillMultiplier = RemoteConfigManager.RemoteConfig.GetFloat("global_FillMultiplier", globalFillMultiplier_defaultValue);
    5. }
    I assume I am failing because I forgot something or misunderstood the documentation. Please let me know if you have any idea what might be wrong :)

    Specs & Versions:
    • Using Unity 2020.1.3f1
    • Using the verified Remote Config package (1.0.9)
    P.S.
    On a different note, I want to never use cached Remote Config data. How can I disable it?

    Thanks in advance!
     
  2. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    UPDATE -

    I figured out why I was unsuccessfully getting data - I was missing the FetchConfigs method.
    Since I don't want to use any optional flags, I needed to do two things:
    1. Add userAttributes and appAttributes structs to my project:
    Code (CSharp):
    1.     public struct userAttributes
    2.     {
    3.         // Optionally declare variables for any custom user attributes:
    4.     }
    5.  
    6.     public struct appAttributes
    7.     {
    8.         // Optionally declare variables for any custom app attributes:
    9.     }
    2. Alter my Awake method:

    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         if (RemoteConfig != null && RemoteConfig != this)
    4.         {
    5.             Destroy(this.gameObject);
    6.             return;
    7.         }
    8.         RemoteConfig = this;
    9.         DontDestroyOnLoad(this);
    10.  
    11.         ConfigManager.FetchCompleted += ApplyRemoteSettings;
    12.         ConfigManager.FetchConfigs<userAttributes, appAttributes>(new userAttributes(), new appAttributes());
    13.     }
    My second question still stands - I'd love to know how to avoid cache, and how to set default values for my requested data. Thanks!
     
    JeffDUnity3D likes this.