XRReferenceObjectLibraryEditor.cs
5.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.XR.ARSubsystems;
namespace UnityEditor.XR.ARSubsystems
{
[CustomEditor(typeof(XRReferenceObjectLibrary))]
class XRReferenceObjectLibraryEditor : Editor
{
static class Content
{
static Content()
{
s_AddButtonContent = new GUIContent("Add Reference Object", "Adds a reference object to the library.");
s_RemoveButtonContent = new GUIContent(
string.Empty,
EditorGUIUtility.FindTexture("d_winbtn_win_close"),
"Remove this image from the library.");
}
public static readonly GUIContent name = new GUIContent(
"Name",
"The name assigned to this reference object.");
public static readonly GUIContent types = new GUIContent(
"Reference Object Assets",
"An asset for each object tracking provider representing this entry. The number of available entries depends on the number of supporting packages installed.");
public static bool addButton
{
get
{
return GUILayout.Button(s_AddButtonContent);
}
}
public static bool removeButton
{
get
{
return GUI.Button(
GUILayoutUtility.GetRect(s_RemoveButtonContent, GUI.skin.button, GUILayout.ExpandWidth(false)),
s_RemoveButtonContent,
GUI.skin.button);
}
}
static readonly GUIContent s_AddButtonContent;
static readonly GUIContent s_RemoveButtonContent;
}
class AssemblyHelper
{
public AssemblyHelper()
{
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
OnAfterAssemblyReload();
}
public List<Type> types { get { return m_Types; } }
void OnAfterAssemblyReload()
{
m_Types.Clear();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach(var type in assembly.GetTypes())
{
if (type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(XRReferenceObjectEntry)))
m_Types.Add(type);
}
}
}
List<Type> m_Types = new List<Type>();
}
static AssemblyHelper s_AssemblyHelper;
static SerializedProperty m_ReferenceObjects;
void OnEnable()
{
m_ReferenceObjects = serializedObject.FindProperty("m_ReferenceObjects");
}
public override void OnInspectorGUI()
{
if (s_AssemblyHelper == null)
s_AssemblyHelper = new AssemblyHelper();
serializedObject.Update();
var library = target as XRReferenceObjectLibrary;
int indexToRemove = -1;
for (int i = 0; i < m_ReferenceObjects.arraySize; ++i)
{
bool shouldRemove = ReferenceObjectField(i);
if (shouldRemove)
{
indexToRemove = i;
}
EditorGUILayout.Separator();
if (i < m_ReferenceObjects.arraySize - 1)
EditorGUILayout.LabelField(string.Empty, GUI.skin.horizontalSlider);
}
if (indexToRemove > -1)
m_ReferenceObjects.DeleteArrayElementAtIndex(indexToRemove);
serializedObject.ApplyModifiedProperties();
if (Content.addButton)
{
Undo.RecordObject(target, "Add reference object");
library.Add();
EditorUtility.SetDirty(target);
}
}
bool ReferenceObjectField(int index)
{
var library = target as XRReferenceObjectLibrary;
var referenceObject = library.m_ReferenceObjects[index];
var referenceObjectProperty = m_ReferenceObjects.GetArrayElementAtIndex(index);
var nameProperty = referenceObjectProperty.FindPropertyRelative("m_Name");
bool remove = false;
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PropertyField(nameProperty, Content.name);
remove = Content.removeButton;
}
EditorGUILayout.LabelField(Content.types);
EditorGUILayout.Separator();
foreach (var type in s_AssemblyHelper.types)
{
using (var changeCheck = new EditorGUI.ChangeCheckScope())
{
var entry = EditorGUILayout.ObjectField(referenceObject.FindEntry(type), type, false) as XRReferenceObjectEntry;
if (changeCheck.changed)
{
serializedObject.ApplyModifiedProperties();
Undo.RecordObject(target, "Change reference object entry");
library.SetReferenceObjectEntry(index, type, entry);
EditorUtility.SetDirty(target);
}
}
}
return remove;
}
}
}