浏览代码

added public API for dynamic prefabs and fixed bugs

/4.1
Shan Jiang 4 年前
当前提交
84839ad3
共有 2 个文件被更改,包括 81 次插入19 次删除
  1. 99
      Assets/Scenes/ImageTracking/Multi Prefab - Image Tracking/MultiTrackedImageInfoManager.cs
  2. 1
      ProjectSettings/GraphicsSettings.asset

99
Assets/Scenes/ImageTracking/Multi Prefab - Image Tracking/MultiTrackedImageInfoManager.cs


/// detected image.
/// </summary>
[RequireComponent(typeof(ARTrackedImageManager))]
public class MultiTrackedImageInfoManager : MonoBehaviour
public class MultiTrackedImageInfoManager : MonoBehaviour, ISerializationCallbackReceiver
/// It has an image guid and a GameObject for prefab.
/// Used to associate an `XRReferenceImage` with a Prefab by using the `XRReferenceImage`'s guid as a unique identifier for a particular reference image.
// System.Guid isn't serializable, so we store the Guid as a string. At runtime, this is converted back to a System.Guid
[SerializeField]
string m_ImageGuid;

/// <summary>
/// Each prefab corresponds to the each image in the Image Library in the same order.
/// </summary>
public List<NamedPrefab> prefabList
List<NamedPrefab> prefabList
{
get => m_PrefabList;
set => m_PrefabList = value;

Dictionary<Guid, GameObject> m_InstantiatedPrefabsDictionary = new Dictionary<Guid, GameObject>();
ARTrackedImageManager m_TrackedImageManager;
[SerializeField]

set => m_ImageLibrary = value;
}
void Awake()
public void OnBeforeSerialize() {}
//TODO: exception thrown
public void OnAfterDeserialize()
m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
}
void Awake()
{
m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
}
void OnEnable()

{
// Give the initial image a reasonable default scale
trackedImage.transform.localScale = new Vector3(trackedImage.size.x / 2, trackedImage.size.x / 2, trackedImage.size.x / 2);
foreach (var trackedImage in eventArgs.updated)
{
if (trackedImage.trackingState != TrackingState.Tracking)
RemoveInstantiatedPrefab(trackedImage);
else if (trackedImage.trackingState == TrackingState.Tracking)
AssignPrefab(trackedImage);
}
}
void AssignPrefab(ARTrackedImage trackedImage)

Instantiate(prefab, trackedImage.transform);
if (!m_InstantiatedPrefabsDictionary.ContainsKey(trackedImage.referenceImage.guid))
{
var instantiatedPrefab = Instantiate(prefab, trackedImage.transform);
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 UpdateInstantiatedPrefab(ARTrackedImage trackedImage)
{
RemoveInstantiatedPrefab(trackedImage);
AssignPrefab(trackedImage);
}
public GameObject GetPrefabForReferenceImage(ARTrackedImage trackedImage)
{
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject prefab))
return prefab;
else
return null;
}
public void SetPrefabForReferenceImage(ARTrackedImage trackedImage, GameObject alternativePrefab)
{
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject targetPrefabInDictionary))
{
var listIndex = prefabList.FindIndex(item => item.prefab == targetPrefabInDictionary);
if (listIndex!= -1)
{
//update prefab list
var targetPrefabInList = prefabList[listIndex];
targetPrefabInList.prefab = alternativePrefab;
prefabList[listIndex] = targetPrefabInList;
//update prefab dictionary
m_PrefabsDictionary[trackedImage.referenceImage.guid] = alternativePrefab;
//update instantiated prefab
UpdateInstantiatedPrefab(trackedImage);
}
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(MultiTrackedImageInfoManager))]

EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ImageLibrary"));
//show prefab list
EditorGUILayout.PropertyField(list, false);
EditorGUI.indentLevel += 1;
if (list.isExpanded)
{
for (int i = 0; i < list.arraySize; i++) {
EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i).FindPropertyRelative("m_Prefab"), new GUIContent(library[i].name));
}
}
EditorGUI.indentLevel -= 1;
//check library changes
EditorGUI.BeginChangeCheck();

m_ReferenceImages.Add(referenceImage);
}
}
//show prefab list
EditorGUILayout.PropertyField(list, false);
EditorGUI.indentLevel += 1;
if (list.isExpanded)
{
for (int i = 0; i < list.arraySize; i++) {
EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i).FindPropertyRelative("m_Prefab"), new GUIContent(library[i].name));
}
}
EditorGUI.indentLevel -= 1;
serializedObject.ApplyModifiedProperties();
}

1
ProjectSettings/GraphicsSettings.asset


- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16003, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}

正在加载...
取消
保存