No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdaptivePerformanceLoaderOrderUI.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System.Collections.Generic;
  2. using System;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. using UnityEditor.AdaptivePerformance.Editor.Metadata;
  6. namespace UnityEditor.AdaptivePerformance.Editor
  7. {
  8. internal interface IAdaptivePerformanceLoaderOrderManager
  9. {
  10. List<AdaptivePerformanceLoaderInfo> AssignedLoaders { get; }
  11. List<AdaptivePerformanceLoaderInfo> UnassignedLoaders { get; }
  12. void AssignLoader(AdaptivePerformanceLoaderInfo assignedInfo);
  13. void UnassignLoader(AdaptivePerformanceLoaderInfo unassignedInfo);
  14. void Update();
  15. }
  16. internal class AdaptivePerformanceLoaderOrderUI
  17. {
  18. struct LoaderInformation
  19. {
  20. public string packageName;
  21. public string packageId;
  22. public string loaderName;
  23. public string loaderType;
  24. public string licenseURL;
  25. public bool toggled;
  26. public bool stateChanged;
  27. }
  28. struct Content
  29. {
  30. public static readonly string k_AtNoLoaderInstance = L10n.Tr("There are no Adaptive Performance providers available for this platform.");
  31. public static readonly string k_LicenseText = L10n.Tr("By clicking the checkbox to install a provider, you acknowledge that you have read and agreed to the terms and conditions found under \"View licenses.\"");
  32. public static readonly GUIContent k_LicenseViewText = new GUIContent(L10n.Tr("View licenses"));
  33. public static readonly GUIContent k_LoaderUITitle = EditorGUIUtility.TrTextContent(L10n.Tr("Providers"));
  34. public static readonly GUIContent k_HelpContent = new GUIContent("", EditorGUIUtility.IconContent("_Help@2x").image, L10n.Tr("Selecting a provider installs that providers package. Packages can be managed through the Package Manager."));
  35. }
  36. private List<LoaderInformation> m_LoaderMetadata = null;
  37. ReorderableList m_OrderedList = null;
  38. public BuildTargetGroup CurrentBuildTargetGroup { get; set; }
  39. internal AdaptivePerformanceLoaderOrderUI()
  40. {
  41. }
  42. void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
  43. {
  44. var li = m_LoaderMetadata[index];
  45. li.toggled = AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(li.loaderType, CurrentBuildTargetGroup);
  46. bool preToggledState = li.toggled;
  47. rect.width *= 0.51f;
  48. EditorGUIUtility.labelWidth = 180;
  49. li.toggled = EditorGUI.Toggle(rect, li.loaderName, preToggledState);
  50. if (li.toggled != preToggledState)
  51. {
  52. li.stateChanged = true;
  53. m_LoaderMetadata[index] = li;
  54. }
  55. if (li.licenseURL != null)
  56. DisplayLink(Content.k_LicenseViewText, new Uri(li.licenseURL), 2, 80, rect);
  57. }
  58. private void DisplayLink(GUIContent text, Uri link, float leftMargin, float width, Rect rect)
  59. {
  60. var labelStyle = EditorStyles.linkLabel;
  61. var uriRect = rect;
  62. uriRect.x += uriRect.width * 2;
  63. uriRect.y -= 1;
  64. uriRect.x -= width + leftMargin;
  65. uriRect.width = width;
  66. if (GUI.Button(uriRect, text, labelStyle))
  67. {
  68. System.Diagnostics.Process.Start(link.AbsoluteUri);
  69. }
  70. EditorGUIUtility.AddCursorRect(uriRect, MouseCursor.Link);
  71. EditorGUI.DrawRect(new Rect(uriRect.x + 2, uriRect.y + uriRect.height - 3, uriRect.width - 3, 1), labelStyle.normal.textColor);
  72. }
  73. float GetElementHeight(int index)
  74. {
  75. return m_OrderedList.elementHeight;
  76. }
  77. internal bool OnGUI(BuildTargetGroup buildTargetGroup)
  78. {
  79. var settings = AdaptivePerformanceGeneralSettingsPerBuildTarget.AdaptivePerformanceGeneralSettingsForBuildTarget(buildTargetGroup);
  80. if (buildTargetGroup != CurrentBuildTargetGroup || m_LoaderMetadata == null)
  81. {
  82. CurrentBuildTargetGroup = buildTargetGroup;
  83. if (m_LoaderMetadata == null)
  84. m_LoaderMetadata = new List<LoaderInformation>();
  85. else
  86. m_LoaderMetadata.Clear();
  87. foreach (var pmd in AdaptivePerformancePackageMetadataStore.GetLoadersForBuildTarget(buildTargetGroup))
  88. {
  89. m_LoaderMetadata.Add(new LoaderInformation() {
  90. packageName = pmd.packageName,
  91. packageId = pmd.packageId,
  92. loaderName = pmd.loaderName,
  93. loaderType = pmd.loaderType,
  94. licenseURL = pmd.licenseURL,
  95. toggled = AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(pmd.loaderType, buildTargetGroup)
  96. });
  97. }
  98. if (settings != null)
  99. {
  100. LoaderInformation li;
  101. for (int i = 0; i < m_LoaderMetadata.Count; i++)
  102. {
  103. li = m_LoaderMetadata[i];
  104. if (AdaptivePerformancePackageMetadataStore.IsLoaderAssigned(settings.AssignedSettings, li.loaderType))
  105. {
  106. li.toggled = true;
  107. m_LoaderMetadata[i] = li;
  108. break;
  109. }
  110. }
  111. }
  112. m_OrderedList = new ReorderableList(m_LoaderMetadata, typeof(LoaderInformation), false, true, false, false);
  113. m_OrderedList.drawHeaderCallback = (rect) =>
  114. {
  115. var labelSize = EditorStyles.label.CalcSize(Content.k_LoaderUITitle);
  116. var labelRect = new Rect(rect);
  117. labelRect.width = labelSize.x;
  118. labelSize = EditorStyles.label.CalcSize(Content.k_HelpContent);
  119. var imageRect = new Rect(rect);
  120. imageRect.xMin = labelRect.xMax + 1;
  121. imageRect.width = labelSize.x;
  122. EditorGUI.LabelField(labelRect, Content.k_LoaderUITitle, EditorStyles.label);
  123. EditorGUI.LabelField(imageRect, Content.k_HelpContent);
  124. };
  125. m_OrderedList.drawElementCallback = (rect, index, isActive, isFocused) => DrawElementCallback(rect, index, isActive, isFocused);
  126. m_OrderedList.elementHeightCallback = (index) => GetElementHeight(index);
  127. m_OrderedList.drawFooterCallback = (rect) =>
  128. {
  129. var status = AdaptivePerformancePackageMetadataStore.GetCurrentStatusDisplayText();
  130. GUI.Label(rect, status, EditorStyles.label);
  131. };
  132. }
  133. if (m_LoaderMetadata == null || m_LoaderMetadata.Count == 0)
  134. {
  135. EditorGUILayout.HelpBox(Content.k_AtNoLoaderInstance, MessageType.Info);
  136. }
  137. else
  138. {
  139. EditorGUILayout.HelpBox(Content.k_LicenseText, MessageType.Info);
  140. EditorGUILayout.Space();
  141. m_OrderedList.DoLayoutList();
  142. if (settings != null)
  143. {
  144. LoaderInformation li;
  145. for (int i = 0; i < m_LoaderMetadata.Count; i++)
  146. {
  147. li = m_LoaderMetadata[i];
  148. if (li.stateChanged)
  149. {
  150. if (li.toggled)
  151. {
  152. AdaptivePerformancePackageMetadataStore.InstallPackageAndAssignLoaderForBuildTarget(li.packageId, li.loaderType, buildTargetGroup);
  153. }
  154. else
  155. {
  156. AdaptivePerformancePackageMetadataStore.RemoveLoader(settings.AssignedSettings, li.loaderType, buildTargetGroup);
  157. }
  158. li.stateChanged = false;
  159. m_LoaderMetadata[i] = li;
  160. }
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. }
  167. }