XRLoaderOrderUI.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Collections.Generic;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using UnityEditor.XR.Management.Metadata;
  5. namespace UnityEditor.XR.Management
  6. {
  7. internal interface IXRLoaderOrderManager
  8. {
  9. List<XRLoaderInfo> AssignedLoaders { get; }
  10. List<XRLoaderInfo> UnassignedLoaders { get; }
  11. void AssignLoader(XRLoaderInfo assignedInfo);
  12. void UnassignLoader(XRLoaderInfo unassignedInfo);
  13. void Update();
  14. }
  15. internal class XRLoaderOrderUI
  16. {
  17. struct LoaderInformation
  18. {
  19. public string packageName;
  20. public string packageId;
  21. public string loaderName;
  22. public string loaderType;
  23. public bool toggled;
  24. public bool stateChanged;
  25. }
  26. const string k_AtNoLoaderInstance = "There are no XR plugins applicable to this platform.";
  27. private List<LoaderInformation> m_LoaderMetadata = null;
  28. ReorderableList m_OrderedList = null;
  29. public BuildTargetGroup CurrentBuildTargetGroup { get; set; }
  30. internal XRLoaderOrderUI()
  31. {
  32. }
  33. void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
  34. {
  35. var li = m_LoaderMetadata[index];
  36. li.toggled = XRPackageMetadataStore.IsLoaderAssigned(li.loaderType, CurrentBuildTargetGroup);
  37. bool preToggledState = li.toggled;
  38. li.toggled = EditorGUI.ToggleLeft(rect, li.loaderName, preToggledState);
  39. if (li.toggled != preToggledState)
  40. {
  41. li.stateChanged = true;
  42. m_LoaderMetadata[index] = li;
  43. }
  44. }
  45. float GetElementHeight(int index)
  46. {
  47. return m_OrderedList.elementHeight;
  48. }
  49. internal bool OnGUI(BuildTargetGroup buildTargetGroup)
  50. {
  51. var settings = XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(buildTargetGroup);
  52. if (buildTargetGroup != CurrentBuildTargetGroup || m_LoaderMetadata == null)
  53. {
  54. CurrentBuildTargetGroup = buildTargetGroup;
  55. if (m_LoaderMetadata == null)
  56. m_LoaderMetadata = new List<LoaderInformation>();
  57. else
  58. m_LoaderMetadata.Clear();
  59. foreach (var pmd in XRPackageMetadataStore.GetLoadersForBuildTarget(buildTargetGroup))
  60. {
  61. m_LoaderMetadata.Add(new LoaderInformation() {
  62. packageName = pmd.packageName,
  63. packageId = pmd.packageId,
  64. loaderName = pmd.loaderName,
  65. loaderType = pmd.loaderType,
  66. toggled = XRPackageMetadataStore.IsLoaderAssigned(pmd.loaderType, buildTargetGroup)
  67. });
  68. }
  69. if (settings != null)
  70. {
  71. LoaderInformation li;
  72. for (int i = 0; i < m_LoaderMetadata.Count; i++)
  73. {
  74. li = m_LoaderMetadata[i];
  75. if (XRPackageMetadataStore.IsLoaderAssigned(settings.AssignedSettings, li.loaderType))
  76. {
  77. li.toggled = true;
  78. m_LoaderMetadata[i] = li;
  79. break;
  80. }
  81. }
  82. }
  83. m_OrderedList = new ReorderableList(m_LoaderMetadata, typeof(LoaderInformation), false, true, false, false);
  84. m_OrderedList.drawHeaderCallback = (rect) => GUI.Label(rect, EditorGUIUtility.TrTextContent("Plug-in Providers"), EditorStyles.label);
  85. m_OrderedList.drawElementCallback = (rect, index, isActive, isFocused) => DrawElementCallback(rect, index, isActive, isFocused);
  86. m_OrderedList.drawFooterCallback = (rect) =>
  87. {
  88. var status = XRPackageMetadataStore.GetCurrentStatusDisplayText();
  89. GUI.Label(rect, EditorGUIUtility.TrTextContent(status), EditorStyles.label);
  90. };
  91. m_OrderedList.elementHeightCallback = (index) => GetElementHeight(index);
  92. }
  93. if (m_LoaderMetadata == null || m_LoaderMetadata.Count == 0)
  94. {
  95. EditorGUILayout.HelpBox(k_AtNoLoaderInstance, MessageType.Info);
  96. }
  97. else
  98. {
  99. m_OrderedList.DoLayoutList();
  100. if (settings != null)
  101. {
  102. LoaderInformation li;
  103. for (int i = 0; i < m_LoaderMetadata.Count; i++)
  104. {
  105. li = m_LoaderMetadata[i];
  106. if (li.stateChanged)
  107. {
  108. if (li.toggled)
  109. {
  110. XRPackageMetadataStore.InstallPackageAndAssignLoaderForBuildTarget(li.packageId, li.loaderType, buildTargetGroup);
  111. }
  112. else
  113. {
  114. XRPackageMetadataStore.RemoveLoader(settings.AssignedSettings, li.loaderType, buildTargetGroup);
  115. }
  116. li.stateChanged = false;
  117. m_LoaderMetadata[i] = li;
  118. }
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. }