|
|
|
|
|
|
} |
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
[Tooltip("Each prefab corresponds to each image in the Image Library.")] |
|
|
|
[HideInInspector] |
|
|
|
List<NamedPrefab> m_PrefabsList = new List<NamedPrefab>(); |
|
|
|
|
|
|
|
Dictionary<Guid, GameObject> m_PrefabsDictionary = new Dictionary<Guid, GameObject>(); |
|
|
|
|
|
|
m_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged; |
|
|
|
} |
|
|
|
|
|
|
|
void OnLibraryChanged(XRReferenceImageLibrary library) |
|
|
|
{ |
|
|
|
if (library) |
|
|
|
{ |
|
|
|
var tempDictionary = new Dictionary<Guid, GameObject>(); |
|
|
|
foreach (var referenceImage in library) |
|
|
|
{ |
|
|
|
tempDictionary.Add(referenceImage.guid, (m_PrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject prefab)) ? prefab : null); |
|
|
|
} |
|
|
|
m_PrefabsDictionary = tempDictionary; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
trackedImage.transform.localScale = new Vector3(trackedImage.size.x / 2, trackedImage.size.x / 2, trackedImage.size.x / 2); |
|
|
|
AssignPrefab(trackedImage); |
|
|
|
var minLocalScalar = Mathf.Min(trackedImage.size.x, trackedImage.size.y); |
|
|
|
trackedImage.transform.localScale = new Vector3(minLocalScalar / 2, minLocalScalar / 2, minLocalScalar / 2); |
|
|
|
AssignOrShowPrefab(trackedImage); |
|
|
|
RemoveInstantiatedPrefab(trackedImage); |
|
|
|
else if (trackedImage.trackingState == TrackingState.Tracking) |
|
|
|
AssignPrefab(trackedImage); |
|
|
|
{ |
|
|
|
if (m_InstantiatedPrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject instantiatedPrefab)) |
|
|
|
instantiatedPrefab.SetActive(false); |
|
|
|
} |
|
|
|
else |
|
|
|
AssignOrShowPrefab(trackedImage); |
|
|
|
void AssignPrefab(ARTrackedImage trackedImage) |
|
|
|
void AssignOrShowPrefab(ARTrackedImage trackedImage) |
|
|
|
{ |
|
|
|
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject prefab)) |
|
|
|
{ |
|
|
|
|
|
|
m_InstantiatedPrefabsDictionary.Add(trackedImage.referenceImage.guid, instantiatedPrefab); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void RemoveInstantiatedPrefab(ARTrackedImage trackedImage) |
|
|
|
{ |
|
|
|
if (m_InstantiatedPrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject instantiatedPrefab)) |
|
|
|
{ |
|
|
|
Destroy(instantiatedPrefab); |
|
|
|
m_InstantiatedPrefabsDictionary.Remove(trackedImage.referenceImage.guid); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void RemoveInstantiatedPrefab(XRReferenceImage referenceImage) |
|
|
|
{ |
|
|
|
if (m_InstantiatedPrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject instantiatedPrefab)) |
|
|
|
{ |
|
|
|
Destroy(instantiatedPrefab); |
|
|
|
m_InstantiatedPrefabsDictionary.Remove(referenceImage.guid); |
|
|
|
else |
|
|
|
{ |
|
|
|
var instantiatedPrefab = m_InstantiatedPrefabsDictionary[trackedImage.referenceImage.guid]; |
|
|
|
instantiatedPrefab.SetActive(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
if (m_PrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject targetPrefabInDictionary)) |
|
|
|
{ |
|
|
|
m_PrefabsDictionary[referenceImage.guid] = alternativePrefab; |
|
|
|
RemoveInstantiatedPrefab(referenceImage); |
|
|
|
if (m_InstantiatedPrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject instantiatedPrefab)) |
|
|
|
{ |
|
|
|
Destroy(instantiatedPrefab); |
|
|
|
m_InstantiatedPrefabsDictionary.Remove(referenceImage.guid); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script")); |
|
|
|
} |
|
|
|
|
|
|
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ImageLibrary")); |
|
|
|
var library = serializedObject.FindProperty("m_ImageLibrary").objectReferenceValue as XRReferenceImageLibrary; |
|
|
|
var libraryProperty = serializedObject.FindProperty(nameof(m_ImageLibrary)); |
|
|
|
EditorGUILayout.PropertyField(libraryProperty); |
|
|
|
var library = libraryProperty.objectReferenceValue as XRReferenceImageLibrary; |
|
|
|
behaviour.OnLibraryChanged(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(); |
|
|
|