我正在尝试扫描图像并在其上放置预制件,ARFoundation中给出的示例工作正常,但是我的目标是,对于要显示不同预制件的每张扫描图像,我都尝试如下操作但无法正常工作
void UpdateInfo(ARTrackedImage trackedImage)
{
// Set canvas camera
var canvas = trackedImage.GetComponentInChildren<Canvas>();
canvas.worldCamera = worldSpaceCanvasCamera;
var planeParentGo = trackedImage.transform.GetChild(0).gameObject;
var planeGo = planeParentGo.transform.GetChild(0).gameObject;
// Disable the visual plane if it is not being tracked
if (trackedImage.trackingState != TrackingState.None)
{
planeGo.SetActive(true);
GameObject myInstance = Instantiate(Resources.Load("ImageInfoPrefab"), new Vector3(trackedImage.size.x, 1f, trackedImage.size.y), Quaternion.identity) as GameObject;
Debug.Log("----------------> 1");
if (trackedImage.referenceImage.name == "10")
{
Debug.Log("----------------> 2");
Debug.Log(trackedImage.name);
Instantiate(myRoadInstance, transform.position, transform.rotation);
}
else if (trackedImage.referenceImage.name == "12")
{
Debug.Log(trackedImage.name);
Instantiate(myRoadInstance, transform.position, transform.rotation);
}
// The image extents is only valid when the image is being tracked
trackedImage.transform.localScale = new Vector3(trackedImage.size.x, 1f, trackedImage.size.y);
// Set the texture
var material = planeGo.GetComponentInChildren<MeshRenderer>().material;
material.mainTexture = (trackedImage.referenceImage.texture == null) ? defaultTexture : trackedImage.referenceImage.texture;
}
else
{
planeGo.SetActive(false);
planeParentGo.SetActive(false);
}
}
void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var trackedImage in eventArgs.added)
{
// Give the initial image a reasonable default scale
trackedImage.transform.localScale = new Vector3(0.01f, 1f, 0.01f);
UpdateInfo(trackedImage);
}
foreach (var trackedImage in eventArgs.updated)
UpdateInfo(trackedImage);
}
因此从以上代码中,您可以看到正在尝试根据trakedImage名称实例化预制件。但这不起作用。
GameObject myInstance = Instantiate(Resources.Load("ImageInfoPrefab"), new Vector3(trackedImage.size.x, 1f, trackedImage.size.y), Quaternion.identity) as GameObject;