using Assets.Scripts.Furniture; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; public class FurnitureManager : MonoBehaviour { [SerializeField] private ARRaycastManager arRaycastManager; // Para detectar superficies [SerializeField] private List furniturePrefabs; // Lista de prefabs de muebles [SerializeField] private GameObject furniturePreviewPrefab; // Prefab para previsualización private GameObject _currentPreview; private GameObject _currentFurniturePrefab; private Vector3 _detectedPosition = Vector3.zero; private Quaternion _detectedRotation = Quaternion.identity; private bool _canAddFurniture = false; private void Start() { // Establece el primer mueble como predeterminado if (furniturePrefabs.Count > 0) { SetSelectedFurniture(furniturePrefabs[0]); } } private void Update() { DetectPlaneAndUpdatePreview(); } /// /// Método para detectar planos y actualizar la posición de previsualización. /// private void DetectPlaneAndUpdatePreview() { if (_currentPreview == null) return; var hits = new List(); var middleScreen = new Vector2(Screen.width / 2, Screen.height / 2); if (arRaycastManager.Raycast(middleScreen, hits, TrackableType.PlaneWithinPolygon)) { _detectedPosition = hits[0].pose.position; _detectedRotation = hits[0].pose.rotation; _currentPreview.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); _currentPreview.SetActive(true); _canAddFurniture = true; } else { _currentPreview.SetActive(false); _canAddFurniture = false; } } /// /// Método para colocar un mueble en la posición detectada. /// public void PlaceFurniture() { if (!_canAddFurniture || _currentFurniturePrefab == null) return; var newFurniture = Instantiate(_currentFurniturePrefab); newFurniture.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); // Añadir funcionalidad de manipulación (rotar, mover, etc.) newFurniture.AddComponent(); Debug.Log("Mueble colocado en la posición detectada."); } /// /// Cambiar el prefab del mueble seleccionado. /// /// Prefab del mueble seleccionado. public void SetSelectedFurniture(GameObject furniturePrefab) { _currentFurniturePrefab = furniturePrefab; // Actualizar el preview if (_currentPreview != null) { Destroy(_currentPreview); } _currentPreview = Instantiate(furniturePreviewPrefab ?? furniturePrefab); _currentPreview.SetActive(false); } }