Объект при созданииОбъект без обновлений после движения камеры
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class TrackingTest : MonoBehaviour
{
private ARTrackedImageManager manager;
private ARTrackedImage trackedImage = null;
private GameObject instantiatedScene;
bool tracking = false;
public Button refreshButton;
private AssetBundle materialsBundle;
private AssetBundle assetsBundle;
private List spawnedPrefabs = new List();
private ARAnchorManager anchorManager;
private ARAnchor anchor;
private void Awake()
{
manager = GetComponent();
anchorManager = FindObjectOfType();
if (anchorManager == null)
{
Debug.LogError("ARAnchorManager not found. Make sure it's added to the AR Session Origin.");
}
}
private void OnEnable()
{
MutableImageLibrary.OnlibraryComplete += OnlibraryComplete;
if (refreshButton != null)
{
refreshButton.onClick.AddListener(OnRefreshButtonClicked);
}
}
private void OnlibraryComplete()
{
//var arSessionOrigin = FindObjectOfType();
//arSessionOrigin.transform.position = Vector3.zero;
//arSessionOrigin.transform.rotation = Quaternion.identity;
Debug.Log("start tracking");
manager = FindObjectOfType();
manager.requestedMaxNumberOfMovingImages = 100;
manager.trackedImagesChanged -= OnTrackedImagesChanged;
manager.trackedImagesChanged += OnTrackedImagesChanged;
}
private void OnDisable()
{
MutableImageLibrary.OnlibraryComplete -= OnlibraryComplete;
manager.trackedImagesChanged -= OnTrackedImagesChanged;
}
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (var image in eventArgs.added)
{
OpenScene(image);
trackedImage = image;
}
foreach (var image in eventArgs.updated)
{
if (tracking)
{
if (instantiatedScene != null)
{
UpdateScene(image);
tracking = false;
}
else
{
if (image.trackingState == TrackingState.Tracking)
{
OpenScene(image);
trackedImage = image;
}
}
}
}
foreach (var image in eventArgs.removed)
{
RemoveScene(image);
}
}
private void OpenScene(ARTrackedImage image)
{
string imageName = image.referenceImage.name;
Debug.Log($"Open Scene: {imageName}");
string saveFolder = Config.instance.GetLocalPhysicsBundlesFolder() + "/" + imageName + "/";
string assetsPath = saveFolder + "assets";
string materialsPath = saveFolder + "materials";
try
{
if (File.Exists(materialsPath) && File.Exists(assetsPath))
{
// Load the materials asset bundle
materialsBundle = AssetBundle.LoadFromFile(materialsPath);
if (materialsBundle == null)
{
Debug.LogError("Failed to load materials bundle.");
return;
}
// Load the assets asset bundle
assetsBundle = AssetBundle.LoadFromFile(assetsPath);
if (assetsBundle == null)
{
Debug.LogError("Failed to load assets bundle.");
return;
}
// Call the method to spawn the scene from the assets bundle
Debug.Log("Spawning Scene");
// Load the main scene GameObject from the asset bundle
GameObject scenePrefab = assetsBundle.LoadAsset(imageName);
if (scenePrefab != null)
{
// Instantiate the prefab in the current scene
var imageTransform = image.transform;
instantiatedScene = Instantiate(scenePrefab, imageTransform.position, imageTransform.rotation);
// Trying to anchor the Object
anchor = anchorManager.AddAnchor(new Pose(imageTransform.position, imageTransform.rotation));
instantiatedScene.name = imageName;
spawnedPrefabs.Add(instantiatedScene);
Debug.Log("Name: " + instantiatedScene.name);
tracking = true;
if (anchor!= null)
{
instantiatedScene.transform.SetParent(anchor.transform, false);
instantiatedScene.transform.localRotation = imageTransform.rotation;
instantiatedScene.transform.localPosition = Vector3.zero;
Debug.Log("Object anchored successfully.");
}
else
{
Debug.LogError("Failed to create anchor.");
// Destroy object if anchor creation fails
Destroy(instantiatedScene);
}
}
else
{
Debug.LogError("Failed to load the scene prefab from the assets bundle.");
}
}
else
{
Debug.LogError("Materials or assets file does not exist.");
}
}
catch (Exception e)
{
Debug.LogError("Exception: " + e);
}
}
private void RemoveScene(ARTrackedImage image)
{
if (image == null)
{
Debug.Log("Remove Scene got called without an image");
}
else
{
Debug.Log($"Remove Scene: {image.referenceImage.name}");
string imageName = image.referenceImage.name;
if (instantiatedScene != null)
{
Destroy(instantiatedScene);
UnloadAssetBundles();
Debug.Log($"Scene removed: {imageName}");
}
else
{
if (GameObject.Find(imageName) != null)
{
instantiatedScene = GameObject.Find(imageName);
Destroy(instantiatedScene);
UnloadAssetBundles();
Debug.Log($"Scene removed: {imageName}");
}
else
{
Debug.LogError($"Scene not found: {imageName}");
}
}
trackedImage = null;
}
}
private void UpdateScene(ARTrackedImage image)
{
Debug.Log("Update Scene");
// Ensure the scene object is not null
if (instantiatedScene != null)
{
// Position the scene at the tracked image's location
var transform1 = image.transform;
if (image.trackingState == TrackingState.Tracking)
{
// Only update when the image is actively tracked
instantiatedScene.transform.position = transform1.position;
instantiatedScene.transform.rotation = transform1.rotation;
}
}
else
{
Debug.LogError("Instantiated scene object is null. Cannot relocate.");
}
}
private void OnRefreshButtonClicked()
{
RemoveScene(trackedImage);
tracking = true;
}
private void UnloadAssetBundles()
{
// Unload the materials and assets bundles here
if (materialsBundle != null)
{
materialsBundle.Unload(true);
materialsBundle = null;
}
if (assetsBundle != null)
{
assetsBundle.Unload(true);
assetsBundle = null;
}
}
}
Я ожидал, что мой объект вообще не будет двигаться, даже когда камера перемещается, но он все равно перемещался вместе с камерой.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ut-updates
Мобильная версия