XRLoaderOrderUI.cs
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.XR.Management;
namespace UnityEditor.XR.Management
{
internal interface IXRLoaderOrderManager
{
List<XRLoaderInfo> AssignedLoaders { get; }
List<XRLoaderInfo> UnassignedLoaders { get; }
void AssignLoader(XRLoaderInfo assignedInfo);
void UnassignLoader(XRLoaderInfo unassignedInfo);
void Update();
}
internal class XRLoaderOrderUI
{
const string k_AtLeastOneLoaderInstance = "Must add at least one installed plugin provider to use this platform for XR.";
const string k_AtNoLoaderInstance = "There are no installed plugin providers available for this platform.";
IXRLoaderOrderManager m_Manager = null;
ReorderableList m_OrderedList = null;
internal XRLoaderOrderUI(IXRLoaderOrderManager manager = null)
{
m_Manager = manager;
}
void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
{
XRLoaderInfo info = index < m_Manager.AssignedLoaders.Count ? m_Manager.AssignedLoaders[index] : null;
var label = (info == null || info.instance == null) ? EditorGUIUtility.TrTextContent("Missing (XRLoader)") : EditorGUIUtility.TrTextContent(info.assetName);
EditorGUI.LabelField(rect, label);
}
float GetElementHeight(int index)
{
return m_OrderedList.elementHeight;
}
void ReorderLoaderList(ReorderableList list)
{
m_Manager.Update();
}
void DrawAddDropdown(Rect rect, ReorderableList list)
{
GenericMenu menu = new GenericMenu();
int index = 0;
if (m_Manager.UnassignedLoaders.Count > 0)
{
foreach (var info in m_Manager.UnassignedLoaders)
{
string name = info.assetName;
if (String.IsNullOrEmpty(name) && info.loaderType != null)
{
name = EditorUtilities.TypeNameToString(info.loaderType);
}
menu.AddItem(new GUIContent(string.Format("{0}. {1}", index + 1, name)), false, AddLoaderMenuSelected, index);
index++;
}
}
else
{
menu.AddDisabledItem(new GUIContent("No Assignable Providers"));
}
menu.ShowAsContext();
}
void AddLoaderMenuSelected(object data)
{
int selected = (int)data;
XRLoaderInfo info = m_Manager.UnassignedLoaders[selected];
AddLoaderMenu(info);
}
// TODO: Move out to manager
void AddLoaderMenu(XRLoaderInfo info)
{
if (info.instance == null)
{
string newAssetName = String.Format("{0}.asset", EditorUtilities.TypeNameToString(info.loaderType));
XRLoader loader = ScriptableObject.CreateInstance(info.loaderType) as XRLoader;
string assetPath = EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultLoaderPath);
if (string.IsNullOrEmpty(assetPath))
{
return;
}
assetPath = Path.Combine(assetPath, newAssetName);
info.instance = loader;
info.assetName = Path.GetFileNameWithoutExtension(assetPath);
AssetDatabase.CreateAsset(loader, assetPath);
}
m_Manager.AssignLoader(info);
}
void RemoveInstanceFromList(ReorderableList list)
{
XRLoaderInfo info = m_Manager.AssignedLoaders[list.index];
m_Manager.UnassignLoader(info);
}
internal bool OnGUI()
{
if (!m_Manager.AssignedLoaders.Any() && !m_Manager.UnassignedLoaders.Any())
{
EditorGUILayout.HelpBox(k_AtNoLoaderInstance, MessageType.Warning);
return false;
}
else if (!m_Manager.AssignedLoaders.Any())
{
EditorGUILayout.HelpBox(k_AtLeastOneLoaderInstance, MessageType.Warning);
}
if (m_OrderedList == null)
{
m_OrderedList = new ReorderableList(m_Manager.AssignedLoaders, typeof(XRLoader), true, true, true, true);
m_OrderedList.drawHeaderCallback = (rect) => GUI.Label(rect, EditorGUIUtility.TrTextContent("Plugin Providers"), EditorStyles.label);
m_OrderedList.drawElementCallback = (rect, index, isActive, isFocused) => DrawElementCallback(rect, index, isActive, isFocused);
m_OrderedList.elementHeightCallback = (index) => GetElementHeight(index);
m_OrderedList.onReorderCallback = (list) => ReorderLoaderList(list);
m_OrderedList.onAddDropdownCallback = (rect, list) => DrawAddDropdown(rect, list);
m_OrderedList.onRemoveCallback = (list) => RemoveInstanceFromList(list);
}
m_OrderedList.DoLayoutList();
return false;
}
}
}