Пример #1
0
    /// <summary>
    /// The method makes an asynchronous request of the backend configuration data.
    /// When the request is answered, the <see cref="OnConfigurationAvailable"/>
    /// callback will be called.
    /// The <see cref="BackendConfiguration"/> is cached in order to improve subsequent calls.
    /// To clear cached configuration use the <see cref="ClearBackendConfig"/> method.
    /// </summary>
    /// <param name="onConfigurationAvailable">The callback called when the configuration is available</param>
    public void RequestBackendConfig(OnConfigurationAvailable onConfigurationAvailable)
    {
        if (_backendConfig != null)
        {
            onConfigurationAvailable(_backendConfig);
            return;
        }

        // Request
        UnityWebRequest www;

        // Check if other config request is running
        if (_backendConfigRequest != null)
        {
            www = _backendConfigRequest.webRequest;
        }
        else
        {
            www = UnityWebRequest.Get(server + "/api/backend/configuration");
            _backendConfigRequest = www.SendWebRequest();
        }

        _backendConfigRequest.completed += (_) => {
            _backendConfigRequest = null;
#if UNITY_EDITOR
            if (verbose)
            {
                Debug.Log("text: " + www.downloadHandler.text);
            }
#endif
            if (www.responseCode == 200)
            {
                _backendConfig = BackendConfiguration.CreateFromJSON(GetResultJSON(www.downloadHandler.text));
                onConfigurationAvailable(_backendConfig);
            }
            else
            {
                string responseCodeMessage = $"Response Code: {www.responseCode}";
                if (www.responseCode == 500)
                {
                    responseCodeMessage += " - Internal server error.";
                    if (!string.IsNullOrEmpty(apiTokenString))
                    {
                        responseCodeMessage += "\nIf you are using simulator, consider not to use apiTokenString.";
                    }
                }

                Debug.LogError(responseCodeMessage);
                throw new System.Exception(responseCodeMessage);
            }
        };
    }