浏览代码

updated list only for serialization and dictionary for others

/4.1
Shan Jiang 4 年前
当前提交
801e8029
共有 4 个文件被更改,包括 61 次插入74 次删除
  1. 2
      Assets/Scenes/ImageTracking/Multi Prefab - Image Tracking/Multi-Image Tracking.unity
  2. 124
      Assets/Scenes/ImageTracking/Multi Prefab - Image Tracking/MultiTrackedImageInfoManager.cs
  3. 1
      ProjectSettings/GraphicsSettings.asset
  4. 8
      Assets/StreamingAssets.meta

2
Assets/Scenes/ImageTracking/Multi Prefab - Image Tracking/Multi-Image Tracking.unity


m_Script: {fileID: 11500000, guid: 467eefdf66a9b4c24b9d89f2f71b2ce9, type: 3}
m_Name:
m_EditorClassIdentifier:
m_PrefabList:
m_PrefabsList:
- m_ImageGuid: f0340be5-710e-49ff-bc70-45f1315b9202
m_Prefab: {fileID: 6916502901855112170, guid: e22b43a8fcdab413bb4f52f5076f759e,
type: 3}

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


/// <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>
public struct NamedPrefab
struct NamedPrefab
string m_ImageGuid;
public string imageGuid => m_ImageGuid;
public readonly string m_ImageGuid;
GameObject m_Prefab;
public GameObject m_Prefab;
public GameObject prefab
public NamedPrefab(XRReferenceImage image, GameObject prefab)
get => m_Prefab;
set => m_Prefab = value;
m_ImageGuid = image.guid.ToString();
m_Prefab = prefab;
public NamedPrefab(XRReferenceImage image, GameObject prefab)
public NamedPrefab(Guid guid, GameObject prefab)
m_ImageGuid = image.guid.ToString();
m_ImageGuid = guid.ToString();
m_Prefab = prefab;
}
}

List<NamedPrefab> m_PrefabList;
/// <summary>
/// Each prefab corresponds to the each image in the Image Library in the same order.
/// </summary>
List<NamedPrefab> prefabList
{
get => m_PrefabList;
set => m_PrefabList = value;
}
List<NamedPrefab> m_PrefabsList = new List<NamedPrefab>();
Dictionary<Guid, GameObject> m_PrefabsDictionary = new Dictionary<Guid, GameObject>();
Dictionary<Guid, GameObject> m_InstantiatedPrefabsDictionary = new Dictionary<Guid, GameObject>();

set => m_ImageLibrary = value;
}
public void OnBeforeSerialize() {}
public void OnBeforeSerialize()
{
m_PrefabsList.Clear();
foreach (var kvp in m_PrefabsDictionary)
{
m_PrefabsList.Add(new NamedPrefab(kvp.Key, kvp.Value));
}
}
for (int i = 0; i < prefabList.Count; i++)
{
m_PrefabsDictionary.Add(ImageLibrary[i].guid, prefabList[i].prefab);
}
m_PrefabsDictionary = new Dictionary<Guid, GameObject>();
for (int i = 0; i < m_PrefabsList.Count; i++)
{
m_PrefabsDictionary.Add(ImageLibrary[i].guid, m_PrefabsList[i].m_Prefab);
}
}
}

{
if (library)
{
if (prefabList == null)
var tempDictionary = new Dictionary<Guid, GameObject>();
foreach (var referenceImage in library)
prefabList = new List<NamedPrefab>();
foreach (var referenceImage in library)
{
prefabList.Add(new NamedPrefab(referenceImage, null));
}
}
else
{
var tempList = new List<NamedPrefab>();
foreach (var referenceImage in library)
{
var listIndex = prefabList.FindIndex(item => item.imageGuid == referenceImage.guid.ToString());
tempList.Add(new NamedPrefab(referenceImage, (listIndex != -1) ? prefabList[listIndex].prefab : null));
}
prefabList = tempList;
tempDictionary.Add(referenceImage.guid, (m_PrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject prefab)) ? prefab : null);
m_PrefabsDictionary = tempDictionary;
}
}

void RemoveInstantiatedPrefab(ARTrackedImage trackedImage)
{
if(m_InstantiatedPrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject instantiatedPrefab))
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))
if (m_InstantiatedPrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject instantiatedPrefab))
{
Destroy(instantiatedPrefab);
m_InstantiatedPrefabsDictionary.Remove(referenceImage.guid);

public GameObject GetPrefabForReferenceImage(XRReferenceImage referenceImage)
{
if (m_PrefabsDictionary.TryGetValue(referenceImage.guid, out GameObject prefab))
return prefab;
else
return null;
}
=> m_PrefabsDictionary.TryGetValue(referenceImage.guid, out var prefab) ? prefab : null;
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[referenceImage.guid] = alternativePrefab;
//remove instantiated prefab
RemoveInstantiatedPrefab(referenceImage);
}
}
m_PrefabsDictionary[referenceImage.guid] = alternativePrefab;
RemoveInstantiatedPrefab(referenceImage);
}
}
#if UNITY_EDITOR

{
List<XRReferenceImage> m_ReferenceImages = new List<XRReferenceImage>();
bool isExpanded = true;
bool HasLibraryChanged(XRReferenceImageLibrary library)
{

//customized inspector
var behaviour = serializedObject.targetObject as MultiTrackedImageInfoManager;
var library = serializedObject.FindProperty("m_ImageLibrary").objectReferenceValue as XRReferenceImageLibrary;
var list = serializedObject.FindProperty("m_PrefabList");
GUI.enabled = false;
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
GUI.enabled = true;
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"));
}
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ImageLibrary"));

}
//show prefab list
EditorGUILayout.PropertyField(list, false);
EditorGUI.indentLevel += 1;
if (list.isExpanded)
isExpanded = EditorGUILayout.Foldout(isExpanded, "Prefab List");
if (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;
foreach (var image in library)
{
if (behaviour.m_PrefabsDictionary.TryGetValue(image.guid, out GameObject prefab))
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(image.name, GUILayout.Width(200f));
EditorGUILayout.ObjectField(prefab, typeof(GameObject), false);
EditorGUILayout.EndHorizontal();
}
EditorGUI.indentLevel -= 1;
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}

8
Assets/StreamingAssets.meta


fileFormatVersion: 2
guid: deab931f1d58b4e8c8d0fe61e6c5ff42
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存