public void OnPreprocessBuild(BuildReport report)
        {
            CheckInstalledProvider();

            // Always remember to clean up preloaded assets after build to make sure we don't
            // dirty later builds with assets that may not be needed or are out of date.
            CleanOldSettings();

            AdaptivePerformanceGeneralSettingsPerBuildTarget buildTargetSettings = null;

            EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out buildTargetSettings);
            if (buildTargetSettings == null)
            {
                return;
            }

            AdaptivePerformanceGeneralSettings settings = buildTargetSettings.SettingsForBuildTarget(report.summary.platformGroup);

            if (settings == null)
            {
                return;
            }

            UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();

            if (!preloadedAssets.Contains(settings))
            {
                var assets = preloadedAssets.ToList();
                assets.Add(settings);
                PlayerSettings.SetPreloadedAssets(assets.ToArray());
            }
        }
        /// <summary>Given a build target, get the general settings container assigned to it.</summary>
        /// <param name="targetGroup">An enum that specifies which platform group this build is for.</param>
        /// <returns>The instance of <see cref="AdaptivePerformanceGeneralSettings"/> assigned to the key, or null if no instance is assigned.</returns>
        public static AdaptivePerformanceGeneralSettings AdaptivePerformanceGeneralSettingsForBuildTarget(BuildTargetGroup targetGroup)
        {
            AdaptivePerformanceGeneralSettingsPerBuildTarget buildTargetSettings = null;

            EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out buildTargetSettings);
            if (buildTargetSettings == null)
            {
                return(null);
            }

            return(buildTargetSettings.SettingsForBuildTarget(targetGroup));
        }
        static void PlayModeStateChanged(PlayModeStateChange state)
        {
            AdaptivePerformanceGeneralSettingsPerBuildTarget buildTargetSettings = null;

            EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out buildTargetSettings);
            if (buildTargetSettings == null)
            {
                return;
            }

            AdaptivePerformanceGeneralSettings instance = buildTargetSettings.SettingsForBuildTarget(BuildTargetGroup.Standalone);

            if (instance == null || !instance.InitManagerOnStart)
            {
                return;
            }

            instance.InternalPlayModeStateChanged(state);
        }
        internal static bool ContainsLoaderForAnyBuildTarget(string loaderTypeName)
        {
            AdaptivePerformanceGeneralSettingsPerBuildTarget buildTargetSettings = null;

            EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out buildTargetSettings);
            if (buildTargetSettings == null)
            {
                return(false);
            }

            foreach (var settings in buildTargetSettings.Settings.Values)
            {
                if (AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(settings.Manager, loaderTypeName))
                {
                    return(true);
                }
            }

            return(false);
        }
        internal bool OnGUI(BuildTargetGroup buildTargetGroup)
        {
            var settings = AdaptivePerformanceGeneralSettingsPerBuildTarget.AdaptivePerformanceGeneralSettingsForBuildTarget(buildTargetGroup);

            if (buildTargetGroup != CurrentBuildTargetGroup || m_LoaderMetadata == null)
            {
                CurrentBuildTargetGroup = buildTargetGroup;

                if (m_LoaderMetadata == null)
                {
                    m_LoaderMetadata = new List <LoaderInformation>();
                }
                else
                {
                    m_LoaderMetadata.Clear();
                }

                foreach (var pmd in AdaptivePerformancePackageMetadataStore.GetLoadersForBuildTarget(buildTargetGroup))
                {
                    m_LoaderMetadata.Add(new LoaderInformation()
                    {
                        packageName = pmd.packageName,
                        packageId   = pmd.packageId,
                        loaderName  = pmd.loaderName,
                        loaderType  = pmd.loaderType,
                        licenseURL  = pmd.licenseURL,
                        toggled     = AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(pmd.loaderType, buildTargetGroup)
                    });
                }

                if (settings != null)
                {
                    LoaderInformation li;
                    for (int i = 0; i < m_LoaderMetadata.Count; i++)
                    {
                        li = m_LoaderMetadata[i];
                        if (AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(settings.AssignedSettings, li.loaderType))
                        {
                            li.toggled          = true;
                            m_LoaderMetadata[i] = li;
                            break;
                        }
                    }
                }

                m_OrderedList = new ReorderableList(m_LoaderMetadata, typeof(LoaderInformation), false, true, false, false);
                m_OrderedList.drawHeaderCallback = (rect) =>
                {
                    var labelSize = EditorStyles.label.CalcSize(Content.k_LoaderUITitle);
                    var labelRect = new Rect(rect);
                    labelRect.width = labelSize.x;

                    labelSize = EditorStyles.label.CalcSize(Content.k_HelpContent);
                    var imageRect = new Rect(rect);
                    imageRect.xMin  = labelRect.xMax + 1;
                    imageRect.width = labelSize.x;

                    EditorGUI.LabelField(labelRect, Content.k_LoaderUITitle, EditorStyles.label);
                    EditorGUI.LabelField(imageRect, Content.k_HelpContent);
                };

                m_OrderedList.drawElementCallback   = (rect, index, isActive, isFocused) => DrawElementCallback(rect, index, isActive, isFocused);
                m_OrderedList.elementHeightCallback = (index) => GetElementHeight(index);
                m_OrderedList.drawFooterCallback    = (rect) =>
                {
                    var status = AdaptivePerformancePackageMetadataStore.GetCurrentStatusDisplayText();
                    GUI.Label(rect, status, EditorStyles.label);
                };
            }

            if (m_LoaderMetadata == null || m_LoaderMetadata.Count == 0)
            {
                EditorGUILayout.HelpBox(Content.k_AtNoLoaderInstance, MessageType.Info);
            }
            else
            {
                EditorGUILayout.HelpBox(Content.k_LicenseText, MessageType.Info);
                EditorGUILayout.Space();

                m_OrderedList.DoLayoutList();
                if (settings != null)
                {
                    LoaderInformation li;
                    for (int i = 0; i < m_LoaderMetadata.Count; i++)
                    {
                        li = m_LoaderMetadata[i];
                        if (li.stateChanged)
                        {
                            if (li.toggled)
                            {
                                AdaptivePerformancePackageMetadataStore.InstallPackageAndAssignLoaderForBuildTarget(li.packageId, li.loaderType, buildTargetGroup);
                            }
                            else
                            {
                                AdaptivePerformancePackageMetadataStore.RemoveLoader(settings.AssignedSettings, li.loaderType, buildTargetGroup);
                            }
                            li.stateChanged     = false;
                            m_LoaderMetadata[i] = li;
                        }
                    }
                }
            }

            return(false);
        }