IEnumerator QualitySettingsCheck()
        {
            int currentLevel = QualitySettings.GetQualityLevel();

            while (true)
            {
                yield return(new WaitForEndOfFrame());

                if (currentLevel != QualitySettings.GetQualityLevel())
                {
                    currentLevel = QualitySettings.GetQualityLevel();
                    TFidelity message = new TFidelity();
                    MessageUtil.SetQualityLevel(message, QualitySettings.GetQualityLevel());
                    m_AdditionalLibraryMethods.SetFidelityParameters(message);
                }
            }
        }
        IEnumerator QualitySettingsCheck()
        {
            int currentLevel = QualitySettings.GetQualityLevel();

            while (true)
            {
                yield return(new WaitForEndOfFrame());

                if (currentLevel != QualitySettings.GetQualityLevel())
                {
                    currentLevel = QualitySettings.GetQualityLevel();
                    TFidelity message = new TFidelity();
                    MessageUtil.SetQualityLevel(message, QualitySettings.GetQualityLevel());
                    var errorCode = m_AdditionalLibraryMethods.SetFidelityParameters(message);
                    if (errorCode != ErrorCode.Ok)
                    {
                        Debug.LogErrorFormat("SetFidelityParameters({0}) result is {1}", message, errorCode);
                    }
                }
            }
        }
        ErrorCode StartInternal()
        {
            m_SetupConfig = Resources.Load("SetupConfig") as SetupConfig;

            if (m_SetupConfig == null)
            {
                Debug.LogWarning(
                    "SetupConfig can not be loaded, open Google->Android Performance Tuner to setup the plugin.");
                return(ErrorCode.NoSettings);
            }

            if (!m_SetupConfig.pluginEnabled)
            {
                Debug.LogWarning(
                    "Android Performance Tuner plugin is not enabled, open Google->Android Performance Tuner to enable the plugin.");
                return(ErrorCode.TuningforkNotInitialized);
            }

            var annotationStatus = CheckAnnotationMessage(m_SetupConfig);
            var fidelityStatus   = CheckFidelityMessage(m_SetupConfig);

            if (annotationStatus != ErrorCode.Ok)
            {
                return(annotationStatus);
            }
            if (fidelityStatus != ErrorCode.Ok)
            {
                return(fidelityStatus);
            }


            IMessage defaultQualityParameters = null;

            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                defaultQualityParameters = new TFidelity();
                MessageUtil.SetQualityLevel(defaultQualityParameters, QualitySettings.GetQualityLevel());
            }

            var errorCode = m_AdditionalLibraryMethods.Init(Callbacks.FidelityParamsCallbackImpl,
                                                            defaultQualityParameters, m_endPoint);

            if (errorCode != ErrorCode.Ok)
            {
                m_AdditionalLibraryMethods.FreePointers();
                return(errorCode);
            }

            m_OnStop += () => m_AdditionalLibraryMethods.FreePointers();

            CreateSceneObject();
            m_SceneObject.StartCoroutine(CallbacksCheck());

            if (!SwappyIsEnabled())
            {
                EnableUnityFrameTicks();
            }
            if (!m_SetupConfig.useAdvancedAnnotations)
            {
                EnableDefaultAnnotationsMode();
            }
            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                EnableDefaultFidelityMode();
            }

            AddUploadCallback();
            AddAutoFlush();

            return(errorCode);
        }
        ErrorCode StartInternal()
        {
            m_SetupConfig = Resources.Load("SetupConfig") as SetupConfig;

            if (m_SetupConfig == null)
            {
                Debug.LogWarning(
                    "SetupConfig can not be loaded, open Google->Android Performance Tuner to setup the plugin.");
                return(ErrorCode.NoSettings);
            }

            if (!m_SetupConfig.pluginEnabled)
            {
                Debug.LogWarning(
                    "Android Performance Tuner plugin is not enabled, open Google->Android Performance Tuner to enable the plugin.");
                return(ErrorCode.TuningforkNotInitialized);
            }

            var annotationStatus = CheckAnnotationMessage(m_SetupConfig);
            var fidelityStatus   = CheckFidelityMessage(m_SetupConfig);

            if (annotationStatus != ErrorCode.Ok)
            {
                return(annotationStatus);
            }
            if (fidelityStatus != ErrorCode.Ok)
            {
                return(fidelityStatus);
            }


            InitializationSettings settings = new InitializationSettings();

            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                settings.trainingFidelityParams = new TFidelity();
                MessageUtil.SetQualityLevel(settings.trainingFidelityParams, QualitySettings.GetQualityLevel());
            }

            settings.endpointUriOverride = m_endPoint;
            if (m_SetupConfig.mode == TunerMode.Experiments)
            {
                settings.fidelityParamsCallback = Callbacks.FidelityParamsCallbackImpl;
            }
            settings.maxNumMetrics = m_MetricLimits;


            var errorCode = m_AdditionalLibraryMethods.InitWithSettings(settings);

            if (errorCode != ErrorCode.Ok)
            {
                m_AdditionalLibraryMethods.FreePointers();
                return(errorCode);
            }

            m_OnStop += () => m_AdditionalLibraryMethods.FreePointers();

            CreateSceneObject();
            m_SceneObject.StartCoroutine(CallbacksCheck());

            if (!SwappyIsEnabled())
            {
                EnableUnityFrameTicks();
            }
            if (!m_SetupConfig.useAdvancedAnnotations)
            {
                EnableDefaultAnnotationsMode();
            }
            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                EnableDefaultFidelityMode();
            }

            AddAutoFlush();
            AddAutoLifecycleUpdate();
            CheckNetworkReachability();

            return(errorCode);
        }