Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Accesing to Remote config from Build

Discussion in 'Unity Remote Config' started by CienartStudios, Jun 27, 2020.

  1. CienartStudios

    CienartStudios

    Joined:
    Jan 29, 2019
    Posts:
    2
    Hi,

    I'm trying to use RemoteConfig from a build, with the intention of creating a Level Editor Tool for the Game Designer, without having to enter to Unity to create and upload new levels.

    I managed to change the package in order to use it entirely from a build (outside Unity), but there's just a small detail that's not letting me achieve my goal, that is the "CloudProjectSettings.accessToken", is a UnityEditor namespace and is needed to make the Authorization in the UnityWebRequest.

    Is there a way that I could get this CloudProjectSettings from outside UnityEditor?

    Thank you in advance for your answer,

    Franco.
     
    Last edited: Jun 27, 2020
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you please share the code that you are using, and the error that you are receiving?
     
  3. CienartStudios

    CienartStudios

    Joined:
    Jan 29, 2019
    Posts:
    2
    Hi Jeff!

    Thank you for your answer. I think the code would be a bit too much to post here because I edit multiple scripts of the package, basically what I did was take the entire package and moved it into the project files, so I could edit it. Add an #if UNITY_EDITOR in all the lines that were related to the windows editor within Unity, and leave the lines related to the logic itself of RemoteConfig, so I could call this logic from a build.

    I had to change minor things that were using the UnityEditor namespace. For example, it was taking the reference of the RemoteConfigDataStoreAsset scriptable object with
    Code (CSharp):
    1.  AssetDatabase.LoadAssetAtPath(formattedPath, typeof(RemoteConfigDataStore)) as RemoteConfigDataStore
    And instead of that, I'm using Resources.Load.

    Another example could be that was using EditorApplication.update, and instead of that I'm using a Monobehaviour Update callback.

    The things is, the only line that I couldn't replace it was in the RemoteConfigWebApiClient script, in the method:

    Code (CSharp):
    1.         private static UnityWebRequest Authorize(UnityWebRequest request)
    2.         {
    3.             request.SetRequestHeader("Content-Type", "application/json");
    4.             request.SetRequestHeader("User-Agent", "Unity Editor " + Application.unityVersion + " RC " +  RemoteConfigEditorEnvConf.pluginVersion);
    5.             request.SetRequestHeader("Authorization", string.Format("Bearer {0}", CloudProjectSettings.accessToken));
    6.             CloudProjectSettings.RefreshAccessToken((accessTokenRefreshed) => { });
    7.             return request;
    8.         }
    9.  
    In particular CloudProjectSettings.accessToken is a string that seems that can only be obtained by UnityEditor scripts. A hack that I tried was copying the value "by hand" of it, like:

    Code (CSharp):
    1.     public const string CloudProjectSettingsAccessToken = "PL_qLXfEwy-8Xm7WaOQPlfMHBRemPtmlABT-LoedewA004f";
    And everything works well on a build, but it changes on time, so it only lasts for a while. I'm not exactly sure when changes, so I'd have to enter Unity anyway to get its value. Which is not ideal with my goal of using RemoteConfig from a build (push data to the cloud, from the build).

    That's why I concluded in asking if there was a way to get the value of CloudProjectSettings.accessToken without using the UnityEditor namespace.

    The error in particular that I'm having after the accessToken expires is:

    "Failed to push remote config: HTTP/1.1 403 Forbidden. Error message:
    {"message":"The supplied authentication is not authorized to access this resource","code":403}
    UnityEngine.Debug:LogWarning(Object)"

    Which makes sense since the accessToken expired.

    Hopefully, this gives you more context to my question!
     
    Last edited: Jun 30, 2020
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446