浏览代码

updated README and code format

/4.1
Shan Jiang 4 年前
当前提交
3b1c0618
共有 4 个文件被更改,包括 71 次插入64 次删除
  1. 117
      Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/DynamicPrefab.cs
  2. 4
      Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/PrefabImagePairManager.cs
  3. 6
      README.md
  4. 8
      Assets/StreamingAssets.meta

117
Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/DynamicPrefab.cs


using System;
using System.Text;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

switch (m_State)
{
case State.OriginalPrefab:
{
if (GUILayout.Button($"Alternative Prefab for {GetComponent<PrefabImagePairManager>().imageLibrary[0].name}"))
if (GUILayout.Button($"Alternative Prefab for {GetComponent<PrefabImagePairManager>().imageLibrary[0].name}"))
{
m_State = State.ChangeToAlternativePrefab;
}
break;
m_State = State.ChangeToAlternativePrefab;
break;
}
{
if (GUILayout.Button($"Original Prefab for {GetComponent<PrefabImagePairManager>().imageLibrary[0].name}"))
if (GUILayout.Button($"Original Prefab for {GetComponent<PrefabImagePairManager>().imageLibrary[0].name}"))
{
m_State = State.ChangeToOriginalPrefab;
}
break;
m_State = State.ChangeToOriginalPrefab;
break;
}
{
GUILayout.Label(m_ErrorMessage);
break;
}
{
GUILayout.Label(m_ErrorMessage);
break;
}
GUILayout.EndArea();
}

switch (m_State)
{
case State.ChangeToAlternativePrefab:
{
if (!alternativePrefab)
if (!alternativePrefab)
{
SetError("No alternative prefab is given.");
break;
}
SetError("No alternative prefab is given.");
break;
}
var manager = GetComponent<PrefabImagePairManager>();
if (!manager)
{
SetError($"No {nameof(PrefabImagePairManager)} available.");
break;
}
var manager = GetComponent<PrefabImagePairManager>();
if (!manager)
{
SetError($"No {nameof(PrefabImagePairManager)} available.");
break;
}
var library = manager.imageLibrary;
if (!library)
{
SetError($"No image library available.");
break;
}
var library = manager.imageLibrary;
if (!library)
{
SetError($"No image library available.");
break;
}
if (!m_OriginalPrefab)
m_OriginalPrefab = manager.GetPrefabForReferenceImage(library[0]);
if (!m_OriginalPrefab)
m_OriginalPrefab = manager.GetPrefabForReferenceImage(library[0]);
manager.SetPrefabForReferenceImage(library[0], alternativePrefab);
m_State = State.AlternativePrefab;
break;
}
manager.SetPrefabForReferenceImage(library[0], alternativePrefab);
m_State = State.AlternativePrefab;
case State.ChangeToOriginalPrefab:
{
if (!m_OriginalPrefab)
{
SetError("No original prefab is given.");
case State.ChangeToOriginalPrefab:
var manager = GetComponent<PrefabImagePairManager>();
if (!manager)
if (!m_OriginalPrefab)
{
SetError("No original prefab is given.");
break;
}
SetError($"No {nameof(PrefabImagePairManager)} available.");
break;
}
var manager = GetComponent<PrefabImagePairManager>();
if (!manager)
{
SetError($"No {nameof(PrefabImagePairManager)} available.");
break;
}
var library = manager.imageLibrary;
if (!library)
{
SetError($"No image library available.");
break;
}
manager.SetPrefabForReferenceImage(library[0], m_OriginalPrefab);
m_State = State.OriginalPrefab;
var library = manager.imageLibrary;
if (!library)
{
SetError($"No image library available.");
manager.SetPrefabForReferenceImage(library[0], m_OriginalPrefab);
m_State = State.OriginalPrefab;
break;
}
}
}
}

4
Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/PrefabImagePairManager.cs


void AssignPrefab(ARTrackedImage trackedImage)
{
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out GameObject prefab))
if (m_PrefabsDictionary.TryGetValue(trackedImage.referenceImage.guid, out var prefab))
m_Instantiated[trackedImage.referenceImage.guid] = Instantiate(prefab, trackedImage.transform);
}

public void SetPrefabForReferenceImage(XRReferenceImage referenceImage, GameObject alternativePrefab)
{
m_PrefabsDictionary[referenceImage.guid] = alternativePrefab;
if (m_Instantiated.TryGetValue(referenceImage.guid, out GameObject instantiatedPrefab))
if (m_Instantiated.TryGetValue(referenceImage.guid, out var instantiatedPrefab))
{
m_Instantiated[referenceImage.guid] = Instantiate(alternativePrefab, instantiatedPrefab.transform.parent);
Destroy(instantiatedPrefab);

6
README.md


## ImageTracking
There are two samples demonstrating image tracking. Basic and Multi Image tracking are supported on ARCore, ARKit, and Magic Leap. To enable image tracking, you must first create an `XRReferenceImageLibrary`. This is the set of images to look for in the environment. [Click here](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@4.1/manual/image-tracking.html) for instructions on creating one.
There are two samples demonstrating image tracking. The image tracking samples are supported on ARCore, ARKit, and Magic Leap. To enable image tracking, you must first create an `XRReferenceImageLibrary`. This is the set of images to look for in the environment. [Click here](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@4.1/manual/image-tracking.html) for instructions on creating one.
You can also add images to the reference image library at runtime. This sample includes a button that adds the images `one.png` and `two.png` to the reference image library. See the script `DynamicLibrary.cs` for example code.

With [`PrefabImagePairManager.cs`](https://github.com/Unity-Technologies/arfoundation-samples/blob/master/Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/PrefabImagePairManager.cs) script, you can assign different prefabs for each image in the reference image library.
You can also change prefabs at runtime. This sample includes a button that switch between the original and alternative prefab for the first image in the reference image library. See the script `DynamicPrefab.cs` for example code.
You can also change prefabs at runtime. This sample includes a button that switch between the original and alternative prefab for the first image in the reference image library. See the script [`DynamicPrefab.cs`](https://github.com/Unity-Technologies/arfoundation-samples/blob/master/Assets/Scenes/ImageTracking/ImageTrackingWithMultiplePrefabs/DynamicPrefab.cs) for example code.
## ObjectTracking
Similar to the image tracking sample, this sample detects a 3D object from a set of reference objects in an `XRReferenceObjectLibrary`. [Click here](https://docs.unity3d.com/Packages/com.unity.xr.arsubsystems@4.1/manual/object-tracking.html) for instructions on creating one.

## Contributions and Pull Requests
We are not accepting pull requests at this time. If you find an issue with the samples, our would like to request a new sample, please [submit a GitHub issue](https://github.com/Unity-Technologies/arfoundation-samples/issues).
We are not accepting pull requests at this time. If you find an issue with the samples, or would like to request a new sample, please [submit a GitHub issue](https://github.com/Unity-Technologies/arfoundation-samples/issues).

8
Assets/StreamingAssets.meta


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