-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PrefabImagePairManager.cs
217 lines (188 loc) · 8.15 KB
/
PrefabImagePairManager.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARFoundation;
namespace UnityEngine.XR.ARFoundation.Samples
{
/// <summary>
/// This component listens for images detected by the <c>XRImageTrackingSubsystem</c>
/// and overlays some prefabs on top of the detected image.
/// </summary>
[RequireComponent(typeof(ARTrackedImageManager))]
public class PrefabImagePairManager : MonoBehaviour, ISerializationCallbackReceiver
{
/// <summary>
/// Used to associate an `XRReferenceImage` with a Prefab by using the `XRReferenceImage`'s guid as a unique identifier for a particular reference image.
/// </summary>
[Serializable]
struct NamedPrefab
{
// System.Guid isn't serializable, so we store the Guid as a string. At runtime, this is converted back to a System.Guid
public string imageGuid;
public GameObject imagePrefab;
public NamedPrefab(Guid guid, GameObject prefab)
{
imageGuid = guid.ToString();
imagePrefab = prefab;
}
}
[SerializeField]
[HideInInspector]
List<NamedPrefab> m_PrefabsList = new List<NamedPrefab>();
Dictionary<Guid, GameObject> m_PrefabsDictionary = new Dictionary<Guid, GameObject>();
Dictionary<Guid, GameObject> m_Instantiated = new Dictionary<Guid, GameObject>();
ARTrackedImageManager m_TrackedImageManager;
[SerializeField]
[Tooltip("Reference Image Library")]
XRReferenceImageLibrary m_ImageLibrary;
/// <summary>
/// Get the <c>XRReferenceImageLibrary</c>
/// </summary>
public XRReferenceImageLibrary imageLibrary
{
get => m_ImageLibrary;
set => m_ImageLibrary = value;
}
public void OnBeforeSerialize()
{
m_PrefabsList.Clear();
foreach (var kvp in m_PrefabsDictionary)
{
m_PrefabsList.Add(new NamedPrefab(kvp.Key, kvp.Value));
}
}
public void OnAfterDeserialize()
{
m_PrefabsDictionary = new Dictionary<Guid, GameObject>();
foreach (var entry in m_PrefabsList)
{
m_PrefabsDictionary.Add(Guid.Parse(entry.imageGuid), entry.imagePrefab);
}
}
void Awake()
{
m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
}
void OnEnable()
{
m_TrackedImageManager.trackablesChanged.AddListener(OnTrackedImagesChanged);
}
void OnDisable()
{
m_TrackedImageManager.trackablesChanged.RemoveListener(OnTrackedImagesChanged);
}
void OnTrackedImagesChanged(ARTrackablesChangedEventArgs<ARTrackedImage> eventArgs)
{
foreach (var trackedImage in eventArgs.added)
{
// Give the initial image a reasonable default scale
var minLocalScalar = Mathf.Min(trackedImage.size.x, trackedImage.size.y) / 2;
trackedImage.transform.localScale = new Vector3(minLocalScalar, minLocalScalar, minLocalScalar);
AssignPrefab(trackedImage);
}
}
void AssignPrefab(ARTrackedImage trackedImage)
{
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out var prefab))
m_Instantiated[trackedImage.referenceImage.guid] = Instantiate(prefab, trackedImage.transform);
}
public GameObject GetPrefabForReferenceImage(XRReferenceImage referenceImage)
=> m_PrefabsDictionary.TryGetValue(referenceImage.guid, out var prefab) ? prefab : null;
public void SetPrefabForReferenceImage(XRReferenceImage referenceImage, GameObject alternativePrefab)
{
m_PrefabsDictionary[referenceImage.guid] = alternativePrefab;
if (m_Instantiated.TryGetValue(referenceImage.guid, out var instantiatedPrefab))
{
m_Instantiated[referenceImage.guid] = Instantiate(alternativePrefab, instantiatedPrefab.transform.parent);
Destroy(instantiatedPrefab);
}
}
#if UNITY_EDITOR
/// <summary>
/// This customizes the inspector component and updates the prefab list when
/// the reference image library is changed.
/// </summary>
[CustomEditor(typeof(PrefabImagePairManager))]
class PrefabImagePairManagerInspector : Editor
{
List<XRReferenceImage> m_ReferenceImages = new List<XRReferenceImage>();
bool m_IsExpanded = true;
bool HasLibraryChanged(XRReferenceImageLibrary library)
{
if (library == null)
return m_ReferenceImages.Count == 0;
if (m_ReferenceImages.Count != library.count)
return true;
for (int i = 0; i < library.count; i++)
{
if (m_ReferenceImages[i] != library[i])
return true;
}
return false;
}
public override void OnInspectorGUI()
{
//customized inspector
var behaviour = serializedObject.targetObject as PrefabImagePairManager;
serializedObject.Update();
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
}
var libraryProperty = serializedObject.FindProperty(nameof(m_ImageLibrary));
EditorGUILayout.PropertyField(libraryProperty);
var library = libraryProperty.objectReferenceValue as XRReferenceImageLibrary;
//check library changes
if (HasLibraryChanged(library))
{
if (library)
{
var tempDictionary = new Dictionary<Guid, GameObject>();
foreach (var referenceImage in library)
{
tempDictionary.Add(referenceImage.guid, behaviour.GetPrefabForReferenceImage(referenceImage));
}
behaviour.m_PrefabsDictionary = tempDictionary;
}
}
// update current
m_ReferenceImages.Clear();
if (library)
{
foreach (var referenceImage in library)
{
m_ReferenceImages.Add(referenceImage);
}
}
//show prefab list
m_IsExpanded = EditorGUILayout.Foldout(m_IsExpanded, "Prefab List");
if (m_IsExpanded)
{
using (new EditorGUI.IndentLevelScope())
{
EditorGUI.BeginChangeCheck();
var tempDictionary = new Dictionary<Guid, GameObject>();
foreach (var image in library)
{
var prefab = (GameObject) EditorGUILayout.ObjectField(image.name, behaviour.m_PrefabsDictionary[image.guid], typeof(GameObject), false);
tempDictionary.Add(image.guid, prefab);
}
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(target, "Update Prefab");
behaviour.m_PrefabsDictionary = tempDictionary;
EditorUtility.SetDirty(target);
}
}
}
serializedObject.ApplyModifiedProperties();
}
}
#endif
}
}